Skip to contentSkip to conversation

monitor: Fix warning from clang

PATCH In review Stefan Weil 4 participants 9 messages Aug 17, 2012 – Aug 24, 2012

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 monitor: Fix warning from clang

ccc-analyzer reports these warnings:

monitor.c:3532:21: warning: Division by zero

                val %= val2;
                    ^
monitor.c:3530:21: warning: Division by zero
                val /= val2;
                    ^

Rewriting the code fixes this (and also a style issue).

---
 monitor.c |    7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
monitor.c +4 -3
index 0c34934..0ea2c14 100644
@@ -3524,12 +3524,13 @@ static int64_t expr_prod(Monitor *mon)
             break;
         case '/':
         case '%':
-            if (val2 == 0)
+            if (val2 == 0) {
                 expr_error(mon, "division by zero");
-            if (op == '/')
+            } else if (op == '/') {
                 val /= val2;
-            else
+            } else {
                 val %= val2;
+            }
             break;
         }
     }
Signature · 1 line
1.7.10
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · On Fri, 17 Aug 2012 15:34:04 +0200 Stefan Weil <[email protected]> wrote:

ccc-analyzer reports these warnings:

monitor.c:3532:21: warning: Division by zero

                val %= val2;
                    ^
monitor.c:3530:21: warning: Division by zero
                val /= val2;
                    ^
Quoted 3 lines

Rewriting the code fixes this (and also a style issue).

Although I wonder how far we're going "fixing" clang warnings/false positives...

Quoted 25 lines
---
 monitor.c |    7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
monitor.c +4 -3
index 0c34934..0ea2c14 100644
@@ -3524,12 +3524,13 @@ static int64_t expr_prod(Monitor *mon)
             break;
         case '/':
         case '%':
-            if (val2 == 0)
+            if (val2 == 0) {
                 expr_error(mon, "division by zero");
-            if (op == '/')
+            } else if (op == '/') {
                 val /= val2;
-            else
+            } else {
                 val %= val2;
+            }
             break;
         }
     }
#2 lore raw
In reply to Stefan Weil
Quoted 10 lines · Stefan Weil

ccc-analyzer reports these warnings:

monitor.c:3532:21: warning: Division by zero

                val %= val2;
                    ^
monitor.c:3530:21: warning: Division by zero
                val /= val2;
                    ^

Rewriting the code fixes this (and also a style issue).

I'm afraid this doesn't actually fix anything, because...

Quoted 16 lines
---
 monitor.c |    7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
monitor.c +1 -1
index 0c34934..0ea2c14 100644
@@ -3524,12 +3524,13 @@ static int64_t expr_prod(Monitor *mon)
             break;
         case '/':
         case '%':
-            if (val2 == 0)
+            if (val2 == 0) {
                 expr_error(mon, "division by zero");

monitor.c static int64_t expr_prod(Monitor *mon) · line 3536

Quoted 10 lines
monitor.c +3 -2
-            if (op == '/')
+            } else if (op == '/') {
                 val /= val2;
-            else
+            } else {
                 val %= val2;
+            }
             break;
         }
     }

... expr_error() longjmp()s out. The expression evaluator commonly exploits that.

If expr_error() returned, the code would be just as wrong after your patch as before.

Perhaps the checker can be shut up by making expr_error() QEMU_NORETURN.

#3 lore raw
Quoted 36 lines · On Fri, 17 Aug 2012 16:10:12 +0200 Markus Armbruster <[email protected]> wrote:
Quoted 10 lines · Stefan Weil

ccc-analyzer reports these warnings:

monitor.c:3532:21: warning: Division by zero

                val %= val2;
                    ^
monitor.c:3530:21: warning: Division by zero
                val /= val2;
                    ^

Rewriting the code fixes this (and also a style issue).

I'm afraid this doesn't actually fix anything, because...

Quoted 21 lines
---
 monitor.c |    7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
monitor.c +3 -3
index 0c34934..0ea2c14 100644
@@ -3524,12 +3524,13 @@ static int64_t expr_prod(Monitor *mon)
             break;
         case '/':
         case '%':
-            if (val2 == 0)
+            if (val2 == 0) {
                 expr_error(mon, "division by zero");
-            if (op == '/')
+            } else if (op == '/') {
                 val /= val2;
-            else
+            } else {
Quoted 8 lines

monitor.c static int64_t expr_prod(Monitor *mon) · line 3536

Quoted 5 lines
monitor.c +1 -0
                 val %= val2;
+            }
             break;
         }
     }

... expr_error() longjmp()s out. The expression evaluator commonly exploits that.

And that's correct. As far far I understood it's fixing clang, not qemu.

Quoted 2 lines

If expr_error() returned, the code would be just as wrong after your patch as before.

Hmm, how? It checks for val2 == 0 first.

Quoted 1 line

Perhaps the checker can be shut up by making expr_error() QEMU_NORETURN.

That's indeed a better solution.

#4 lore raw
Quoted 48 lines · Luiz Capitulino
Quoted 36 lines · On Fri, 17 Aug 2012 16:10:12 +0200 Markus Armbruster <[email protected]> wrote:
Quoted 10 lines · Stefan Weil

ccc-analyzer reports these warnings:

monitor.c:3532:21: warning: Division by zero

                val %= val2;
                    ^
monitor.c:3530:21: warning: Division by zero
                val /= val2;
                    ^

Rewriting the code fixes this (and also a style issue).

I'm afraid this doesn't actually fix anything, because...

Quoted 21 lines
---
 monitor.c |    7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
monitor.c +3 -3
index 0c34934..0ea2c14 100644
@@ -3524,12 +3524,13 @@ static int64_t expr_prod(Monitor *mon)
             break;
         case '/':
         case '%':
-            if (val2 == 0)
+            if (val2 == 0) {
                 expr_error(mon, "division by zero");
-            if (op == '/')
+            } else if (op == '/') {
                 val /= val2;
-            else
+            } else {
Quoted 8 lines

monitor.c static int64_t expr_prod(Monitor *mon) · line 3536

Quoted 5 lines
monitor.c +1 -0
                 val %= val2;
+            }
             break;
         }
     }

... expr_error() longjmp()s out. The expression evaluator commonly exploits that.

Quoted 6 lines

And that's correct. As far far I understood it's fixing clang, not qemu.

Quoted 2 lines

If expr_error() returned, the code would be just as wrong after your patch as before.

Hmm, how? It checks for val2 == 0 first.

It would evaluate A % 0 into A, which is wrong.

Quoted 3 lines
Quoted 1 line

Perhaps the checker can be shut up by making expr_error() QEMU_NORETURN.

That's indeed a better solution.

Stefan, could you try that for us?

#5 lore raw
Quoted 50 lines · On Fri, 17 Aug 2012 16:41:34 +0200 Markus Armbruster <[email protected]> wrote:
Quoted 48 lines · Luiz Capitulino
Quoted 44 lines · On Fri, 17 Aug 2012 16:10:12 +0200 Markus Armbruster <[email protected]> wrote:
Quoted 10 lines · Stefan Weil

ccc-analyzer reports these warnings:

monitor.c:3532:21: warning: Division by zero

                val %= val2;
                    ^
monitor.c:3530:21: warning: Division by zero
                val /= val2;
                    ^

Rewriting the code fixes this (and also a style issue).

I'm afraid this doesn't actually fix anything, because...

Quoted 16 lines
---
 monitor.c |    7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
monitor.c +1 -1
index 0c34934..0ea2c14 100644
@@ -3524,12 +3524,13 @@ static int64_t expr_prod(Monitor *mon)
             break;
         case '/':
         case '%':
-            if (val2 == 0)
+            if (val2 == 0) {
                 expr_error(mon, "division by zero");

monitor.c static int64_t expr_prod(Monitor *mon) · line 3536

Quoted 10 lines
monitor.c +3 -2
-            if (op == '/')
+            } else if (op == '/') {
                 val /= val2;
-            else
+            } else {
                 val %= val2;
+            }
             break;
         }
     }

... expr_error() longjmp()s out. The expression evaluator commonly exploits that.

Quoted 8 lines
Quoted 6 lines

And that's correct. As far far I understood it's fixing clang, not qemu.

Quoted 2 lines

If expr_error() returned, the code would be just as wrong after your patch as before.

Hmm, how? It checks for val2 == 0 first.

It would evaluate A % 0 into A, which is wrong.

Oh, you're talking about the result that would be returned by expr_prod(). I thought you were saying that val2 == 0 was still possible.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Perhaps the checker can be shut up by making expr_error() QEMU_NORETURN.

That's indeed a better solution.

Stefan, could you try that for us?

#6 lore raw

Am 17.08.2012 17:02, schrieb Luiz Capitulino:

Quoted 58 lines
Quoted 55 lines · On Fri, 17 Aug 2012 16:41:34 +0200 Markus Armbruster <[email protected]> wrote:
Quoted 53 lines · Luiz Capitulino
Quoted 36 lines · On Fri, 17 Aug 2012 16:10:12 +0200 Markus Armbruster <[email protected]> wrote:
Quoted 10 lines · Stefan Weil

ccc-analyzer reports these warnings:

monitor.c:3532:21: warning: Division by zero

                 val %= val2;
                     ^
monitor.c:3530:21: warning: Division by zero
                 val /= val2;
                     ^

Rewriting the code fixes this (and also a style issue).

I'm afraid this doesn't actually fix anything, because...

Quoted 21 lines
---
  monitor.c |    7 ++++---
  1 file changed, 4 insertions(+), 3 deletions(-)
monitor.c +3 -3
index 0c34934..0ea2c14 100644
@@ -3524,12 +3524,13 @@ static int64_t expr_prod(Monitor *mon)
              break;
          case '/':
          case '%':
-            if (val2 == 0)
+            if (val2 == 0) {
                  expr_error(mon, "division by zero");
-            if (op == '/')
+            } else if (op == '/') {
                  val /= val2;
-            else
+            } else {
Quoted 8 lines

monitor.c static int64_t expr_prod(Monitor *mon) · line 3536

Quoted 5 lines
monitor.c +1 -0
                  val %= val2;
+            }
              break;
          }
      }

... expr_error() longjmp()s out. The expression evaluator commonly exploits that.

And that's correct. As far far I understood it's fixing clang, not qemu.

Quoted 2 lines

If expr_error() returned, the code would be just as wrong after your patch as before.

Quoted 13 lines
Quoted 3 lines
Quoted 1 line

Hmm, how? It checks for val2 == 0 first.

It would evaluate A % 0 into A, which is wrong.

Oh, you're talking about the result that would be returned by expr_prod(). I thought you were saying that val2 == 0 was still possible.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Perhaps the checker can be shut up by making expr_error() QEMU_NORETURN.

That's indeed a better solution.

Stefan, could you try that for us?

Adding QEMU_NORETURN to function expr_error also
fixes the warning from ccc-analyzer.

I'll send a patch series which adds this and some more QEMU_NORETURN attributes.

What about using above patch in addition? IMHO it improves readability, and it fixes the coding style.

Regards,

Stefan W.

#7 lore raw
Quoted 71 lines · Stefan Weil

Am 17.08.2012 17:02, schrieb Luiz Capitulino:

Quoted 70 lines
Quoted 50 lines · On Fri, 17 Aug 2012 16:41:34 +0200 Markus Armbruster <[email protected]> wrote:
Quoted 48 lines · Luiz Capitulino
Quoted 44 lines · On Fri, 17 Aug 2012 16:10:12 +0200 Markus Armbruster <[email protected]> wrote:
Quoted 10 lines · Stefan Weil

ccc-analyzer reports these warnings:

monitor.c:3532:21: warning: Division by zero

                 val %= val2;
                     ^
monitor.c:3530:21: warning: Division by zero
                 val /= val2;
                     ^

Rewriting the code fixes this (and also a style issue).

I'm afraid this doesn't actually fix anything, because...

Quoted 16 lines
---
  monitor.c |    7 ++++---
  1 file changed, 4 insertions(+), 3 deletions(-)
monitor.c +1 -1
index 0c34934..0ea2c14 100644
@@ -3524,12 +3524,13 @@ static int64_t expr_prod(Monitor *mon)
              break;
          case '/':
          case '%':
-            if (val2 == 0)
+            if (val2 == 0) {
                  expr_error(mon, "division by zero");

monitor.c static int64_t expr_prod(Monitor *mon) · line 3536

Quoted 10 lines
monitor.c +3 -2
-            if (op == '/')
+            } else if (op == '/') {
                  val /= val2;
-            else
+            } else {
                  val %= val2;
+            }
              break;
          }
      }

... expr_error() longjmp()s out. The expression evaluator commonly exploits that.

Quoted 8 lines
Quoted 6 lines

And that's correct. As far far I understood it's fixing clang, not qemu.

Quoted 2 lines

If expr_error() returned, the code would be just as wrong after your patch as before.

Hmm, how? It checks for val2 == 0 first.

It would evaluate A % 0 into A, which is wrong.

Oh, you're talking about the result that would be returned by expr_prod(). I thought you were saying that val2 == 0 was still possible.

Quoted 5 lines
Quoted 4 lines
Quoted 2 lines

Perhaps the checker can be shut up by making expr_error() QEMU_NORETURN.

That's indeed a better solution.

Quoted 8 lines
Quoted 1 line
Quoted 1 line

Stefan, could you try that for us?

Adding QEMU_NORETURN to function expr_error also
fixes the warning from ccc-analyzer.

I'll send a patch series which adds this and some more QEMU_NORETURN attributes.

Thanks!

Quoted 2 lines

What about using above patch in addition? IMHO it improves readability, and it fixes the coding style.

Readability: debatable. The code depends on expr_error() not returning. The current code makes that fairly obvious locally. I think your patch makes it less obvious. Moreover, it changes the way exp_error() is used in just one place, making it inconsistent with all the other places.

Coding style: we generally make coding style changes only to code we touch anyway, not just for the sake of it.

TL;DR: let's drop this patch.

#8 lore raw
Quoted 92 lines · Markus Armbruster
Quoted 71 lines · Stefan Weil

Am 17.08.2012 17:02, schrieb Luiz Capitulino:

Quoted 70 lines
Quoted 50 lines · On Fri, 17 Aug 2012 16:41:34 +0200 Markus Armbruster <[email protected]> wrote:
Quoted 48 lines · Luiz Capitulino
Quoted 44 lines · On Fri, 17 Aug 2012 16:10:12 +0200 Markus Armbruster <[email protected]> wrote:
Quoted 10 lines · Stefan Weil

ccc-analyzer reports these warnings:

monitor.c:3532:21: warning: Division by zero

                 val %= val2;
                     ^
monitor.c:3530:21: warning: Division by zero
                 val /= val2;
                     ^

Rewriting the code fixes this (and also a style issue).

I'm afraid this doesn't actually fix anything, because...

Quoted 16 lines
---
  monitor.c |    7 ++++---
  1 file changed, 4 insertions(+), 3 deletions(-)
monitor.c +1 -1
index 0c34934..0ea2c14 100644
@@ -3524,12 +3524,13 @@ static int64_t expr_prod(Monitor *mon)
              break;
          case '/':
          case '%':
-            if (val2 == 0)
+            if (val2 == 0) {
                  expr_error(mon, "division by zero");

monitor.c static int64_t expr_prod(Monitor *mon) · line 3536

Quoted 10 lines
monitor.c +3 -2
-            if (op == '/')
+            } else if (op == '/') {
                  val /= val2;
-            else
+            } else {
                  val %= val2;
+            }
              break;
          }
      }

... expr_error() longjmp()s out. The expression evaluator commonly exploits that.

Quoted 8 lines
Quoted 6 lines

And that's correct. As far far I understood it's fixing clang, not qemu.

Quoted 2 lines

If expr_error() returned, the code would be just as wrong after your patch as before.

Hmm, how? It checks for val2 == 0 first.

It would evaluate A % 0 into A, which is wrong.

Oh, you're talking about the result that would be returned by expr_prod(). I thought you were saying that val2 == 0 was still possible.

Quoted 5 lines
Quoted 4 lines
Quoted 2 lines

Perhaps the checker can be shut up by making expr_error() QEMU_NORETURN.

That's indeed a better solution.

Quoted 8 lines
Quoted 1 line
Quoted 1 line

Stefan, could you try that for us?

Adding QEMU_NORETURN to function expr_error also
fixes the warning from ccc-analyzer.

I'll send a patch series which adds this and some more QEMU_NORETURN attributes.

Thanks!

Quoted 2 lines

What about using above patch in addition? IMHO it improves readability, and it fixes the coding style.

Readability: debatable. The code depends on expr_error() not returning. The current code makes that fairly obvious locally. I think your patch makes it less obvious. Moreover, it changes the way exp_error() is used in just one place, making it inconsistent with all the other places.

Quoted 4 lines

Coding style: we generally make coding style changes only to code we touch anyway, not just for the sake of it.

TL;DR: let's drop this patch.

I agree. Let's add QEMU_NORETURN and leave this code as-is.

Stefan