Skip to contentSkip to conversation

Six coverity fixes and a cleanup

PATCH Applied Paolo Bonzini 4 participants 15 messages Jan 26, 2015 – Feb 9, 2015

lore
Share & tools
lore
#0 lore raw

cover Six coverity fixes and a cleanup

Patch 2 is the cleanup. The other six patches make the tcg, utils and migration components clean.

Paolo Bonzini (7):
cpu-exec: drop dead assignment
cpu-exec: simplify icount code
uri: avoid NULL arguments to strcmp
qemu-sockets: improve error reporting in unix_listen_opts
cutils: refine strtol error handling in parse_debug_env
aes: remove a dead return statement
migration: do floating-point division

 cpu-exec.c            | 12 +++---------
 migration/migration.c |  2 +-
 util/aes.c            |  2 +-
 util/cutils.c         |  4 ++--
 util/qemu-sockets.c   | 24 ++++++++++++++++++------
 util/uri.c            |  4 +++-
 6 files changed, 28 insertions(+), 20 deletions(-)
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH 1/7 cpu-exec: drop dead assignment

All uses of TB inside cpu_exec are dominated by "tb = tb_find_fast(env)", and there are no uses after the switch statement. So the assignment is dead, as reported by Coverity.

---
 cpu-exec.c | 1 -
 1 file changed, 1 deletion(-)
cpu-exec.c +0 -1
index a4f0eff..7aab86d 100644
@@ -497,7 +497,6 @@ int cpu_exec(CPUArchState *env)
                          * interrupt_request) which we will handle
                          * next time around the loop.
                          */
-                        tb = (TranslationBlock *)(next_tb & ~TB_EXIT_MASK);
                         next_tb = 0;
                         break;
                     case TB_EXIT_ICOUNT_EXPIRED:
Signature · 1 line
1.8.3.1
#2 lore raw

PATCH 2/7 cpu-exec: simplify icount code

In reply to Paolo Bonzini

Use MIN instead of an "if" statement. Move "tb" assignment where the value is actually used.

---
 cpu-exec.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
cpu-exec.c +3 -8
index 7aab86d..af56a34 100644
@@ -502,22 +502,17 @@ int cpu_exec(CPUArchState *env)
                     case TB_EXIT_ICOUNT_EXPIRED:
                     {
                         /* Instruction counter expired.  */
-                        int insns_left;
-                        tb = (TranslationBlock *)(next_tb & ~TB_EXIT_MASK);
-                        insns_left = cpu->icount_decr.u32;
+                        int insns_left = cpu->icount_decr.u32;
                         if (cpu->icount_extra && insns_left >= 0) {
                             /* Refill decrementer and continue execution.  */
                             cpu->icount_extra += insns_left;
-                            if (cpu->icount_extra > 0xffff) {
-                                insns_left = 0xffff;
-                            } else {
-                                insns_left = cpu->icount_extra;
-                            }
+                            insns_left = MIN(0xffff, cpu->icount_extra);
                             cpu->icount_extra -= insns_left;
                             cpu->icount_decr.u16.low = insns_left;
                         } else {
                             if (insns_left > 0) {
                                 /* Execute remaining instructions.  */
+                                tb = (TranslationBlock *)(next_tb & ~TB_EXIT_MASK);
                                 cpu_exec_nocache(env, insns_left, tb);
                                 align_clocks(&sc, cpu);
                             }
Signature · 1 line
1.8.3.1
#3 lore raw

PATCH 3/7 uri: avoid NULL arguments to strcmp

In reply to Paolo Bonzini

Detected by Coverity.

---
 util/uri.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
util/uri.c +3 -1
index 918d235..014d40d 100644
@@ -1971,7 +1971,9 @@ uri_resolve_relative (const char *uri, const char * base)
 	val = g_strdup (uri);
 	goto done;
     }
-    if (!strcmp(bas->path, ref->path)) {
+    if (bas->path == ref->path ||
+        (bas->path != NULL && ref->path != NULL &&
+         !strcmp(bas->path, ref->path))) {
 	val = g_strdup("");
 	goto done;
     }
Signature · 1 line
1.8.3.1
#4 lore raw

PATCH 4/7 qemu-sockets: improve error reporting in unix_listen_opts

In reply to Paolo Bonzini

Coverity complains about not checking the returned value of mkstemp. While at it, also improve error checking for snprintf, and refine error messages in general.

---
 util/qemu-sockets.c | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)
util/qemu-sockets.c +18 -6
index a76bb3c..cf4b91f 100644
@@ -694,7 +694,7 @@ int unix_listen_opts(QemuOpts *opts, Error **errp)
 
     sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
     if (sock < 0) {
-        error_setg_errno(errp, errno, "Failed to create socket");
+        error_setg_errno(errp, errno, "Failed to create Unix socket");
         return -1;
     }
 
@@ -703,9 +703,15 @@ int unix_listen_opts(QemuOpts *opts, Error **errp)
     if (path && strlen(path)) {
         snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
     } else {
-        char *tmpdir = getenv("TMPDIR");
-        snprintf(un.sun_path, sizeof(un.sun_path), "%s/qemu-socket-XXXXXX",
-                 tmpdir ? tmpdir : "/tmp");
+        const char *tmpdir = getenv("TMPDIR");
+        tmpdir = tmpdir ? tmpdir : "/tmp";
+        if (snprintf(un.sun_path, sizeof(un.sun_path), "%s/qemu-socket-XXXXXX",
+                     tmpdir) >= sizeof(un.sun_path)) {
+            error_setg_errno(errp, errno,
+                             "TMPDIR environment variable (%s) too large", tmpdir);
+            goto err;
+        }
+
         /*
          * This dummy fd usage silences the mktemp() unsecure warning.
          * Using mkstemp() doesn't make things more secure here
@@ -713,13 +719,19 @@ int unix_listen_opts(QemuOpts *opts, Error **errp)
          * to unlink first and thus re-open the race window.  The
          * worst case possible is bind() failing, i.e. a DoS attack.
          */
-        fd = mkstemp(un.sun_path); close(fd);
+        fd = mkstemp(un.sun_path);
+        if (fd < 0) {
+            error_setg_errno(errp, errno,
+                             "Failed to make a temporary socket name in %s", tmpdir);
+            goto err;
+        }
+        close(fd);
         qemu_opt_set(opts, "path", un.sun_path);
     }
 
     unlink(un.sun_path);
     if (bind(sock, (struct sockaddr*) &un, sizeof(un)) < 0) {
-        error_setg_errno(errp, errno, "Failed to bind socket");
+        error_setg_errno(errp, errno, "Failed to bind socket to %s", un.sun_path);
         goto err;
     }
     if (listen(sock, 1) < 0) {
Signature · 1 line
1.8.3.1
#5 lore raw

PATCH 5/7 cutils: refine strtol error handling in parse_debug_env

In reply to Paolo Bonzini

Avoid truncation of a 64-bit long to a 32-bit int, and check for errno (especially ERANGE).

---
 util/cutils.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
util/cutils.c +2 -2
index dbe7412..f227064 100644
@@ -523,7 +523,7 @@ int parse_debug_env(const char *name, int max, int initial)
 {
     char *debug_env = getenv(name);
     char *inv = NULL;
-    int debug;
+    long debug;
 
     if (!debug_env) {
         return initial;
@@ -532,7 +532,7 @@ int parse_debug_env(const char *name, int max, int initial)
     if (inv == debug_env) {
         return initial;
     }
-    if (debug < 0 || debug > max) {
+    if (debug < 0 || debug > max || errno != 0) {
         fprintf(stderr, "warning: %s not in [0, %d]", name, max);
         return initial;
     }
Signature · 1 line
1.8.3.1
#6 lore raw

Re: [Qemu-devel] [Qemu-trivial] [PATCH 5/7] cutils: refine strtol error handling in parse_debug_env

Quoted 22 lines · Paolo Bonzini

Avoid truncation of a 64-bit long to a 32-bit int, and check for errno (especially ERANGE).

---
 util/cutils.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
util/cutils.c +1 -1
index dbe7412..f227064 100644
@@ -523,7 +523,7 @@ int parse_debug_env(const char *name, int max, int initial)
 {
     char *debug_env = getenv(name);
     char *inv = NULL;
-    int debug;
+    long debug;
 
     if (!debug_env) {
         return initial;

util/cutils.c int parse_debug_env(const char *name, int max, int initial) · line 535

Quoted 5 lines
util/cutils.c +1 -1
@@ -532,7 +532,7 @@ int parse_debug_env(const char *name, int max, int initial)
     if (inv == debug_env) {
         return initial;
     }
-    if (debug < 0 || debug > max) {
+    if (debug < 0 || debug > max || errno != 0) {

It is not really right to check errno without (re)setting it before call to strtol().

Thanks,

/mjt

#7 lore raw

PATCH 6/7 aes: remove a dead return statement

In reply to Paolo Bonzini

bits is checked to be 128, 192 or 256 at the beginning of the function.

---
 util/aes.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/aes.c +1 -1
index 6058f19..f42126a 100644
@@ -1161,7 +1161,7 @@ int AES_set_encrypt_key(const unsigned char *userKey, const int bits,
 			rk += 8;
         	}
 	}
-	return 0;
+	abort();
 }
 
 /**
Signature · 1 line
1.8.3.1
#8 lore raw

PATCH 7/7 migration: do floating-point division

In reply to Paolo Bonzini

Dividing integer expressions transferred_bytes and time_spent, and then converting the integer quotient to type double. Any remainder, or fractional part of the quotient, is ignored. Fix this.

---
 migration/migration.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
migration/migration.c +1 -1
index b3adbc6..6db75b8 100644
@@ -646,7 +646,7 @@ static void *migration_thread(void *opaque)
         if (current_time >= initial_time + BUFFER_DELAY) {
             uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
             uint64_t time_spent = current_time - initial_time;
-            double bandwidth = transferred_bytes / time_spent;
+            double bandwidth = (double)transferred_bytes / time_spent;
             max_size = bandwidth * migrate_max_downtime() / 1000000;
 
             s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
Signature · 1 line
1.8.3.1
#9 lore raw

Re: [Qemu-devel] [PATCH 7/7] migration: do floating-point division

Quoted 14 lines · * Paolo Bonzini ([email protected]) wrote:

Dividing integer expressions transferred_bytes and time_spent, and then converting the integer quotient to type double. Any remainder, or fractional part of the quotient, is ignored. Fix this.

---
 migration/migration.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
migration/migration.c +0 -0
index b3adbc6..6db75b8 100644

migration/migration.c static void *migration_thread(void *opaque) · line 652

Quoted 8 lines
migration/migration.c +1 -1
@@ -646,7 +646,7 @@ static void *migration_thread(void *opaque)
         if (current_time >= initial_time + BUFFER_DELAY) {
             uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
             uint64_t time_spent = current_time - initial_time;
-            double bandwidth = transferred_bytes / time_spent;
+            double bandwidth = (double)transferred_bytes / time_spent;
             max_size = bandwidth * migrate_max_downtime() / 1000000;
 
             s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /

This feels like it would be better to fix this by merging it into the s->mbps calculation just off the bottom; we currently have:

            uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
            uint64_t time_spent = current_time - initial_time;
            double bandwidth = transferred_bytes / time_spent;
            max_size = bandwidth * migrate_max_downtime() / 1000000;

            s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
                    ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;

Note that the mbps has a check for time_spent being 0 - if that can ever happen, how come 'bandwidth' has never triggered it?

  transferred_bytes    -    bytes
  time_spent           -    ms
  bandwidth            -    bytes/ms
  migrate_max_downtime -    in ns
  s->mbps              -    mbit/s

giving

max_size = bytes/ms * time-in-ns / 1000000
= bytes/ms * time-in-ms
= bytes

so how about something like:

            uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
            uint64_t time_spent = current_time - initial_time;
            double   bytes_s    = (double) transferred_bytes / ((double) time_spent / 1000.0));
            s->mbps = (bytes_s * 8.0) / 1000000.0;
            max_size = bytes_s * (migrate_max_downtime() / 1000000000.0);

that also needs the trace fixing and the line a few lines below, I *think* we have
dirty_bytes_rate is in bytes/second ? (arch_init.c) expected_downtime in ms ?

                s->expected_downtime = s->dirty_bytes_rate / bandwidth;

so, bytes/s / bytes/ms erm that's supposed to come out as time in ms

                s->expected_downtime = (int64_t)(1000 * (double)s->dirty_bytes_rate / bytes_s);

Yeuch.

Dave

Quoted 4 lines
Signature · 1 line
1.8.3.1

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

Reviewed-by
#10 lore raw

Re: [Qemu-devel] [PATCH 7/7] migration: do floating-point division

Quoted 22 lines · Dr. David Alan Gilbert
Quoted 21 lines · * Paolo Bonzini ([email protected]) wrote:

Dividing integer expressions transferred_bytes and time_spent, and then converting the integer quotient to type double. Any remainder, or fractional part of the quotient, is ignored. Fix this.

---
 migration/migration.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
migration/migration.c +1 -1
index b3adbc6..6db75b8 100644
@@ -646,7 +646,7 @@ static void *migration_thread(void *opaque)
         if (current_time >= initial_time + BUFFER_DELAY) {
             uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
             uint64_t time_spent = current_time - initial_time;
-            double bandwidth = transferred_bytes / time_spent;
+            double bandwidth = (double)transferred_bytes / time_spent;
             max_size = bandwidth * migrate_max_downtime() / 1000000;
 
Quoted 1 line

migration/migration.c static void *migration_thread(void *opaque) · line 652

Quoted 1 line
migration/migration.c +0 -0
             s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /

we had that fix on RHEL, but I forgot to push it upstream (it was not needed as it optimized the calculations at the time).

Quoted 28 lines

This feels like it would be better to fix this by merging it into the s->mbps calculation just off the bottom; we currently have:

            uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
            uint64_t time_spent = current_time - initial_time;
            double bandwidth = transferred_bytes / time_spent;
            max_size = bandwidth * migrate_max_downtime() / 1000000;

            s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
                    ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;

Note that the mbps has a check for time_spent being 0 - if that can ever happen, how come 'bandwidth' has never triggered it?

  transferred_bytes    -    bytes
  time_spent           -    ms
  bandwidth            -    bytes/ms
  migrate_max_downtime -    in ns
  s->mbps              -    mbit/s

giving

max_size = bytes/ms * time-in-ns / 1000000
= bytes/ms * time-in-ms
= bytes

Quoted 6 lines

so how about something like:

            uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
            uint64_t time_spent = current_time - initial_time;
            double   bytes_s    = (double) transferred_bytes / ((double) time_spent / 1000.0));
            s->mbps = (bytes_s * 8.0) / 1000000.0;
            max_size = bytes_s * (migrate_max_downtime() / 1000000000.0);

I think we can do something like this on the normal tree, or just go for sane units left and right?

Quoted 18 lines

that also needs the trace fixing and the line a few lines below, I *think* we have
dirty_bytes_rate is in bytes/second ? (arch_init.c) expected_downtime in ms ?

                s->expected_downtime = s->dirty_bytes_rate / bandwidth;

so, bytes/s / bytes/ms erm that's supposed to come out as time in ms

                s->expected_downtime = (int64_t)(1000 * (double)s->dirty_bytes_rate / bytes_s);

Yeuch.

Dave

Quoted 4 lines
Signature · 1 line
1.8.3.1

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#11 lore raw
In reply to Paolo Bonzini
Quoted 2 lines · Paolo Bonzini

Patch 2 is the cleanup. The other six patches make the tcg, utils and migration components clean.

Applied to -trivial patches 1,2,3,4 and 6, not applied 5 (due to questionable errno checking addition) and 7, due to a discussion.

Thanks,

/mjt

Quoted 18 lines

Paolo Bonzini (7):
cpu-exec: drop dead assignment
cpu-exec: simplify icount code
uri: avoid NULL arguments to strcmp
qemu-sockets: improve error reporting in unix_listen_opts
cutils: refine strtol error handling in parse_debug_env
aes: remove a dead return statement
migration: do floating-point division

 cpu-exec.c            | 12 +++---------
 migration/migration.c |  2 +-
 util/aes.c            |  2 +-
 util/cutils.c         |  4 ++--
 util/qemu-sockets.c   | 24 ++++++++++++++++++------
 util/uri.c            |  4 +++-
 6 files changed, 28 insertions(+), 20 deletions(-)
#12 lore raw
Quoted 4 lines · Michael Tokarev
Quoted 2 lines
Quoted 2 lines

Patch 2 is the cleanup. The other six patches make the tcg, utils and migration components clean.

Applied to -trivial patches 1,2,3,4 and 6, not applied 5 (due to questionable errno checking addition) and 7, due to a discussion.

Thanks, will resubmit 5.

Juan/Amit, can you pick up 7 which Juan has already reviewed?

Paolo

#13 lore raw
Quoted 9 lines · Paolo Bonzini
Quoted 4 lines · Michael Tokarev
Quoted 2 lines
Quoted 2 lines

Patch 2 is the cleanup. The other six patches make the tcg, utils and migration components clean.

Applied to -trivial patches 1,2,3,4 and 6, not applied 5 (due to questionable errno checking addition) and 7, due to a discussion.

Thanks, will resubmit 5.

Juan/Amit, can you pick up 7 which Juan has already reviewed?

I've no prob with 7, but please let's agree which way we're taking there -- whenever subsequent fixes are needed, should be made on the top of this change, or whole thing should be done differently.

Thanks.

/mjt

#14 lore raw
In reply to Paolo Bonzini
Quoted 9 lines · Paolo Bonzini
Quoted 4 lines · Michael Tokarev
Quoted 2 lines
Quoted 2 lines

Patch 2 is the cleanup. The other six patches make the tcg, utils and migration components clean.

Applied to -trivial patches 1,2,3,4 and 6, not applied 5 (due to questionable errno checking addition) and 7, due to a discussion.

Thanks, will resubmit 5.

Juan/Amit, can you pick up 7 which Juan has already reviewed?

I pick 7.

Thanks, Juan.