Skip to contentSkip to conversation

softmmu/device_tree: Silence compiler warning with --enable-sanitizers

PATCH Applied Thomas Huth 6 participants 8 messages Jan 7, 2022 – Jan 10, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 softmmu/device_tree: Silence compiler warning with --enable-sanitizers

If I configure my build with --enable-sanitizers, my GCC (v8.5.0) complains:

.../softmmu/device_tree.c: In function ‘qemu_fdt_add_path’:
.../softmmu/device_tree.c:560:18: error: ‘retval’ may be used uninitialized
in this function [-Werror=maybe-uninitialized]

     int namelen, retval;
                  ^~~~~~

It's a false warning since the while loop is always executed at least once (p has to be non-NULL, otherwise the derefence in the if-statement earlier will crash). Thus let's switch to a do-while loop here instead to make the compiler happy in all cases.

---
 softmmu/device_tree.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
softmmu/device_tree.c +2 -2
index 3965c834ca..9e96f5ecd5 100644
@@ -564,7 +564,7 @@ int qemu_fdt_add_path(void *fdt, const char *path)
         return -1;
     }
 
-    while (p) {
+    do {
         name = p + 1;
         p = strchr(name, '/');
         namelen = p != NULL ? p - name : strlen(name);
@@ -584,7 +584,7 @@ int qemu_fdt_add_path(void *fdt, const char *path)
         }
 
         parent = retval;
-    }
+    } while (p);
 
     return retval;
 }
Signature · 1 line
2.27.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 44 lines · Thomas Huth

If I configure my build with --enable-sanitizers, my GCC (v8.5.0) complains:

.../softmmu/device_tree.c: In function ‘qemu_fdt_add_path’:
.../softmmu/device_tree.c:560:18: error: ‘retval’ may be used uninitialized
in this function [-Werror=maybe-uninitialized]

     int namelen, retval;
                  ^~~~~~

It's a false warning since the while loop is always executed at least once (p has to be non-NULL, otherwise the derefence in the if-statement earlier will crash). Thus let's switch to a do-while loop here instead to make the compiler happy in all cases.

---
 softmmu/device_tree.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
softmmu/device_tree.c +2 -2
index 3965c834ca..9e96f5ecd5 100644
@@ -564,7 +564,7 @@ int qemu_fdt_add_path(void *fdt, const char *path)
         return -1;
     }
 
-    while (p) {
+    do {
         name = p + 1;
         p = strchr(name, '/');
         namelen = p != NULL ? p - name : strlen(name);
@@ -584,7 +584,7 @@ int qemu_fdt_add_path(void *fdt, const char *path)
         }
 
         parent = retval;
-    }
+    } while (p);
 
     return retval;
 }
Signature · 1 line
2.27.0
Reviewed-by on v1 1/1
#2 lore raw
In reply to Thomas Huth
Quoted 14 lines · Thomas Huth

If I configure my build with --enable-sanitizers, my GCC (v8.5.0) complains:

.../softmmu/device_tree.c: In function ‘qemu_fdt_add_path’:
.../softmmu/device_tree.c:560:18: error: ‘retval’ may be used uninitialized
in this function [-Werror=maybe-uninitialized]

      int namelen, retval;
                   ^~~~~~

It's a false warning since the while loop is always executed at least once (p has to be non-NULL, otherwise the derefence in the if-statement earlier will crash). Thus let's switch to a do-while loop here instead to make the compiler happy in all cases.

Quoted 4 lines
---
  softmmu/device_tree.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
Reviewed-by on v1 1/1
#3 lore raw
In reply to Thomas Huth
Quoted 14 lines · Thomas Huth
softmmu/device_tree.c +1 -1
index 3965c834ca..9e96f5ecd5 100644
@@ -564,7 +564,7 @@ int qemu_fdt_add_path(void *fdt, const char *path)
          return -1;
      }
 
-    while (p) {
+    do {
          name = p + 1;
          p = strchr(name, '/');
          namelen = p != NULL ? p - name : strlen(name);

softmmu/device_tree.c int qemu_fdt_add_path(void *fdt, const char *path) · line 587

Quoted 5 lines
softmmu/device_tree.c +1 -1
@@ -584,7 +584,7 @@ int qemu_fdt_add_path(void *fdt, const char *path)
          }
 
          parent = retval;
-    }
+    } while (p);

In addition, the copy between 'path' and 'p' is unnecessary -- one of the variables should be removed. Either rename the parameter to 'p', or rename all uses to 'path'.

r~

Reviewed-by on v1 1/1
#5 lore raw
In reply to Thomas Huth
Quoted 10 lines · Thomas Huth

If I configure my build with --enable-sanitizers, my GCC (v8.5.0) complains:

.../softmmu/device_tree.c: In function ‘qemu_fdt_add_path’:
.../softmmu/device_tree.c:560:18: error: ‘retval’ may be used uninitialized
in this function [-Werror=maybe-uninitialized]

     int namelen, retval;
                  ^~~~~~
Quoted 6 lines

It's a false warning since the while loop is always executed at least once (p has to be non-NULL, otherwise the derefence in the if-statement earlier will crash). Thus let's switch to a do-while loop here instead to make the compiler happy in all cases.

Alistair

Quoted 30 lines
---
 softmmu/device_tree.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
softmmu/device_tree.c +2 -3
index 3965c834ca..9e96f5ecd5 100644
@@ -564,7 +564,7 @@ int qemu_fdt_add_path(void *fdt, const char *path)
         return -1;
     }
 
-    while (p) {
+    do {
         name = p + 1;
         p = strchr(name, '/');
         namelen = p != NULL ? p - name : strlen(name);
@@ -584,7 +584,7 @@ int qemu_fdt_add_path(void *fdt, const char *path)
         }
 
         parent = retval;
-    }
+    } while (p);
 
     return retval;
 }
--

2.27.0

Reviewed-by Yanan Wang on v1 1/1
#6 lore raw
In reply to Thomas Huth
Quoted 14 lines · Thomas Huth

If I configure my build with --enable-sanitizers, my GCC (v8.5.0) complains:

.../softmmu/device_tree.c: In function ‘qemu_fdt_add_path’:
.../softmmu/device_tree.c:560:18: error: ‘retval’ may be used uninitialized
in this function [-Werror=maybe-uninitialized]

      int namelen, retval;
                   ^~~~~~

It's a false warning since the while loop is always executed at least once (p has to be non-NULL, otherwise the derefence in the if-statement earlier will crash). Thus let's switch to a do-while loop here instead to make the compiler happy in all cases.

Quoted 4 lines
---
  softmmu/device_tree.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 22 lines
softmmu/device_tree.c +2 -2
index 3965c834ca..9e96f5ecd5 100644
@@ -564,7 +564,7 @@ int qemu_fdt_add_path(void *fdt, const char *path)
          return -1;
      }
 
-    while (p) {
+    do {
          name = p + 1;
          p = strchr(name, '/');
          namelen = p != NULL ? p - name : strlen(name);
@@ -584,7 +584,7 @@ int qemu_fdt_add_path(void *fdt, const char *path)
          }
 
          parent = retval;
-    }
+    } while (p);
 
      return retval;
  }
#4 lore raw
In reply to Richard Henderson
Quoted 21 lines · Richard Henderson
Quoted 13 lines · Thomas Huth
softmmu/device_tree.c +1 -0
index 3965c834ca..9e96f5ecd5 100644
@@ -564,7 +564,7 @@ int qemu_fdt_add_path(void *fdt, const char *path)
           return -1;
       }
   -    while (p) {
+    do {
           name = p + 1;
           p = strchr(name, '/');
           namelen = p != NULL ? p - name : strlen(name);

softmmu/device_tree.c int qemu_fdt_add_path(void *fdt, const char *path) · line 586

Quoted 4 lines
softmmu/device_tree.c +1 -1
@@ -584,7 +584,7 @@ int qemu_fdt_add_path(void *fdt, const char *path)
           }
             parent = retval;
-    }
+    } while (p);
Quoted 2 lines

In addition, the copy between 'path' and 'p' is unnecessary -- one of the variables should be removed.  Either rename the parameter to 'p', or rename all uses to 'path'.

Yes. We can simply remove "const char *p = path;" and directly using the parameter "path" passed in would be fine enough. Thanks for catching this.

Thanks,
Yanan

Quoted 3 lines

r~
.

#7 lore raw
In reply to Thomas Huth
Quoted 10 lines · Thomas Huth

If I configure my build with --enable-sanitizers, my GCC (v8.5.0) complains:

.../softmmu/device_tree.c: In function ‘qemu_fdt_add_path’:
.../softmmu/device_tree.c:560:18: error: ‘retval’ may be used uninitialized
in this function [-Werror=maybe-uninitialized]

     int namelen, retval;
                  ^~~~~~
Quoted 6 lines

It's a false warning since the while loop is always executed at least once (p has to be non-NULL, otherwise the derefence in the if-statement earlier will crash). Thus let's switch to a do-while loop here instead to make the compiler happy in all cases.

Thanks!

Applied to riscv-to-apply.next

If anyone else (trivial maybe?) wants to take this feel free to as well, otherwise I'll just take this via the RISC-V tree.

Alistair

Quoted 30 lines
---
 softmmu/device_tree.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
softmmu/device_tree.c +2 -3
index 3965c834ca..9e96f5ecd5 100644
@@ -564,7 +564,7 @@ int qemu_fdt_add_path(void *fdt, const char *path)
         return -1;
     }
 
-    while (p) {
+    do {
         name = p + 1;
         p = strchr(name, '/');
         namelen = p != NULL ? p - name : strlen(name);
@@ -584,7 +584,7 @@ int qemu_fdt_add_path(void *fdt, const char *path)
         }
 
         parent = retval;
-    }
+    } while (p);
 
     return retval;
 }
--

2.27.0