softmmu/device_tree: Silence compiler warning with --enable-sanitizers
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
@@ -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
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.
Signed-off-by Thomas Huth <[email protected]>--- softmmu/device_tree.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)softmmu/device_tree.c +2
-2@@ -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
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
Signed-off-by Thomas Huth <[email protected]>--- softmmu/device_tree.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 14 lines · Thomas Huth
softmmu/device_tree.c +1
-1@@ -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~
Quoted 21 lines · Richard Henderson
Quoted 13 lines · Thomas Huth
softmmu/device_tree.c +1
-0@@ -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);Reviewed-by Richard Henderson <[email protected]>
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~
.
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.
Signed-off-by Thomas Huth <[email protected]>
Alistair
Quoted 30 lines
--- softmmu/device_tree.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)softmmu/device_tree.c +2
-3@@ -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
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
Signed-off-by Thomas Huth <[email protected]>--- softmmu/device_tree.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 22 lines
softmmu/device_tree.c +2
-2@@ -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; }
Alistair Francis
AppliedQuoted 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.
Signed-off-by Thomas Huth <[email protected]>
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@@ -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