Skip to contentSkip to conversation

device_tree: Fix compiler error

PATCH In review Stefan Weil 5 participants 6 messages Nov 8, 2021 – Dec 17, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 device_tree: Fix compiler error

A build with gcc (Debian 10.2.1-6) 10.2.1 20210110 fails:

../../../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]

  560 |     int namelen, retval;
      |                  ^~~~~~

This is not a real error, but the compiler can be satisfied with a small change.

Fixes b863f0b75852 ("device_tree: Add qemu_fdt_add_path")
---
 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.30.2
#1 lore raw
Quoted 9 lines · Stefan Weil

A build with gcc (Debian 10.2.1-6) 10.2.1 20210110 fails:

../../../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]

  560 |     int namelen, retval;
      |                  ^~~~~~

This is not a real error, but the compiler can be satisfied with a small change.

Why don't we just initalise retval?

Alistair

Quoted 33 lines
Fixes b863f0b75852 ("device_tree: Add qemu_fdt_add_path")
---
 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.30.2

#2 lore raw

Am 08.11.21 um 23:43 schrieb Alistair Francis:

Quoted 8 lines · Stefan Weil
Quoted 7 lines

A build with gcc (Debian 10.2.1-6) 10.2.1 20210110 fails:

../../../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]

   560 |     int namelen, retval;
       |                  ^~~~~~
Quoted 4 lines
Quoted 1 line

This is not a real error, but the compiler can be satisfied with a small change.

Why don't we just initalise retval?

Alistair

retval is already set in the do ... while loop and was also set in the former while loop.

If we set it in the declaration (int retval = 0), a clever compiler might complain that we assign a value which is never used.

And changing from the while loop to the do ... while loop also avoids one compare statement, so depending on the compiler optimizations could make the code a little bit faster.

Stefan

Reviewed-by on v1 1/1
#3 lore raw
In reply to Stefan Weil
Quoted 11 lines · Stefan Weil

A build with gcc (Debian 10.2.1-6) 10.2.1 20210110 fails:

../../../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]

   560 |     int namelen, retval;
       |                  ^~~~~~

This is not a real error, but the compiler can be satisfied with a small change.

Fixes b863f0b75852 ("device_tree: Add qemu_fdt_add_path")

Though I think there's a good deal that could be cleaned up about this function:

(1a) Remove the unused return value?
The single use does not check the return.

(1b) Don't attempt to return a node, merely a success/failure code.
Certainly the local documentation here could be improved...

(1c) Return parent; make retval local to the loop.

(2) Merge p and path; there's no point retaining the unmodified parameter.

(3) Move name and namelen inside the loop.

r~

#4 lore raw
Quoted 23 lines · Richard Henderson
Quoted 8 lines · Stefan Weil

A build with gcc (Debian 10.2.1-6) 10.2.1 20210110 fails:

../../../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] 560 |     int namelen, retval;
       |                  ^~~~~~

Quoted 5 lines

This is not a real error, but the compiler can be satisfied with a small change.

Fixes b863f0b75852 ("device_tree: Add qemu_fdt_add_path")

Though I think there's a good deal that could be cleaned up about this function:

(1a) Remove the unused return value?
     The single use does not check the return.

Quoted 8 lines

(1b) Don't attempt to return a node, merely a success/failure code. Certainly the local documentation here could be improved...

(1c) Return parent; make retval local to the loop.

(2) Merge p and path; there's no point retaining the unmodified parameter.

(3) Move name and namelen inside the loop.

(4) swap if() bodies?

if (retval == -FDT_ERR_NOTFOUND) {
} else if (retval < 0) {
}

Michal

#5 lore raw
In reply to Stefan Weil

Le 08/11/2021 à 21:07, Stefan Weil a écrit :

Quoted 30 lines

A build with gcc (Debian 10.2.1-6) 10.2.1 20210110 fails:

../../../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]

   560 |     int namelen, retval;
       |                  ^~~~~~

This is not a real error, but the compiler can be satisfied with a small change.

Fixes b863f0b75852 ("device_tree: Add qemu_fdt_add_path")
---
  softmmu/device_tree.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
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 590

Quoted 8 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);
 
      return retval;
  }

I think to add a "g_assert_nonull(p);" before the loop would inform the compiler that we always go in the loop and remove the compiler error.

Thanks,
Laurent