Skip to contentSkip to conversation

nbd: fix uninitialized variable warning

PATCH Superseded Marc-André Lureau 3 participants 4 messages Jul 16, 2019 – Jul 17, 2019 Block layer

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nbd: fix uninitialized variable warning

../block/nbd.c: In function 'nbd_co_request':
../block/nbd.c:745:8: error: 'local_reply.type' may be used uninitialized in this function [-Werror=maybe-uninitialized]

     if (chunk->type == NBD_REPLY_TYPE_NONE) {
        ^
../block/nbd.c:710:14: note: 'local_reply.type' was declared here
     NBDReply local_reply;
              ^~~~~~~~~~~
../block/nbd.c:710:14: error: 'local_reply.flags' may be used uninitialized in this function [-Werror=maybe-uninitialized]
../block/nbd.c:738:8: error: 'local_reply.<U4be0>.magic' may be used uninitialized in this function [-Werror=maybe-uninitialized]
     if (nbd_reply_is_simple(reply) || s->quit) {
        ^
../block/nbd.c:710:14: note: 'local_reply.<U4be0>.magic' was declared here
     NBDReply local_reply;
              ^~~~~~~~~~~

cc1: all warnings being treated as errors

---
 block/nbd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
block/nbd.c +1 -1
index 81edabbf35..02eef09728 100644
@@ -707,7 +707,7 @@ static bool nbd_reply_chunk_iter_receive(BDRVNBDState *s,
                                          void **payload)
 {
     int ret, request_ret;
-    NBDReply local_reply;
+    NBDReply local_reply = { 0, };
     NBDStructuredReplyChunk *chunk;
     Error *local_err = NULL;
     if (s->quit) {
Signature · 1 line
2.22.0.428.g6d5b264208
Reviewed-by on v1 1/1
#1 lore raw

Hi Marc-André,

Quoted 14 lines · Marc-André Lureau

../block/nbd.c: In function 'nbd_co_request':
../block/nbd.c:745:8: error: 'local_reply.type' may be used uninitialized in this function [-Werror=maybe-uninitialized]

     if (chunk->type == NBD_REPLY_TYPE_NONE) {
        ^
../block/nbd.c:710:14: note: 'local_reply.type' was declared here
     NBDReply local_reply;
              ^~~~~~~~~~~
../block/nbd.c:710:14: error: 'local_reply.flags' may be used uninitialized in this function [-Werror=maybe-uninitialized]
../block/nbd.c:738:8: error: 'local_reply.<U4be0>.magic' may be used uninitialized in this function [-Werror=maybe-uninitialized]
     if (nbd_reply_is_simple(reply) || s->quit) {
        ^
../block/nbd.c:710:14: note: 'local_reply.<U4be0>.magic' was declared here
     NBDReply local_reply;
              ^~~~~~~~~~~
Quoted 1 line

cc1: all warnings being treated as errors

Thomas reported this error when compiling with -O3 few months ago [1]. Are you using that, or the latest GCC emit a warning even with no -O3?

Personally I'd add:

Fixes 86f8cdf3db8
Quoted 10 lines
---
 block/nbd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
block/nbd.c +0 -0
index 81edabbf35..02eef09728 100644

block/nbd.c static bool nbd_reply_chunk_iter_receive(BDRVNBDState *s, · line 710

Quoted 5 lines
block/nbd.c +1 -1
@@ -707,7 +707,7 @@ static bool nbd_reply_chunk_iter_receive(BDRVNBDState *s,
                                          void **payload)
 {
     int ret, request_ret;
-    NBDReply local_reply;
+    NBDReply local_reply = { 0, };

Yesterday [2] Peter said: "= {}" is our standard struct-zero-initializer so we should prefer that.

With {}:

block/nbd.c static bool nbd_reply_chunk_iter_receive(BDRVNBDState *s, · line 713

Quoted 4 lines
block/nbd.c +0 -0
     NBDStructuredReplyChunk *chunk;
     Error *local_err = NULL;
     if (s->quit) {

[1] https://lists.gnu.org/archive/html/qemu-devel/2019-02/msg07007.html
[2] https://lists.gnu.org/archive/html/qemu-devel/2019-07/msg03431.html

#2 lore raw

Hi

Quoted 20 lines · Philippe Mathieu-Daudé

Hi Marc-André,

Quoted 14 lines · Marc-André Lureau

../block/nbd.c: In function 'nbd_co_request':
../block/nbd.c:745:8: error: 'local_reply.type' may be used uninitialized in this function [-Werror=maybe-uninitialized]

     if (chunk->type == NBD_REPLY_TYPE_NONE) {
        ^
../block/nbd.c:710:14: note: 'local_reply.type' was declared here
     NBDReply local_reply;
              ^~~~~~~~~~~
../block/nbd.c:710:14: error: 'local_reply.flags' may be used uninitialized in this function [-Werror=maybe-uninitialized]
../block/nbd.c:738:8: error: 'local_reply.<U4be0>.magic' may be used uninitialized in this function [-Werror=maybe-uninitialized]
     if (nbd_reply_is_simple(reply) || s->quit) {
        ^
../block/nbd.c:710:14: note: 'local_reply.<U4be0>.magic' was declared here
     NBDReply local_reply;
              ^~~~~~~~~~~
Quoted 1 line

cc1: all warnings being treated as errors

Quoted 1 line

Thomas reported this error when compiling with -O3 few months ago [1].

Thanks, I couldn't find a previous report.

Quoted 1 line

Are you using that, or the latest GCC emit a warning even with no -O3?

Right, the warning seems to appear with -O3 (I happen to have it with mingw64-gcc-8.3.0-2.fc30.x86_64)

Quoted 4 lines

Personally I'd add:

Fixes 86f8cdf3db8

Actually, it was there before, so I'll skip that.

Quoted 21 lines
Quoted 10 lines
---
 block/nbd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
block/nbd.c +0 -0
index 81edabbf35..02eef09728 100644

block/nbd.c static bool nbd_reply_chunk_iter_receive(BDRVNBDState *s, · line 710

Quoted 5 lines
block/nbd.c +1 -1
@@ -707,7 +707,7 @@ static bool nbd_reply_chunk_iter_receive(BDRVNBDState *s,
                                          void **payload)
 {
     int ret, request_ret;
-    NBDReply local_reply;
+    NBDReply local_reply = { 0, };

Yesterday [2] Peter said: "= {}" is our standard struct-zero-initializer so we should prefer that.

Quoted 2 lines

With {}:

ok, thanks

Quoted 8 lines

block/nbd.c static bool nbd_reply_chunk_iter_receive(BDRVNBDState *s, · line 713

Quoted 5 lines
block/nbd.c +0 -0
     NBDStructuredReplyChunk *chunk;
     Error *local_err = NULL;
     if (s->quit) {

[1] https://lists.gnu.org/archive/html/qemu-devel/2019-02/msg07007.html
[2] https://lists.gnu.org/archive/html/qemu-devel/2019-07/msg03431.html

#3 lore raw
In reply to Marc-André Lureau
Patchew URL: https://patchew.org/QEMU/[email protected]/

Patchew URL: https://patchew.org/QEMU/[email protected]/

Hi,

This series failed the asan build test. Please find the testing commands and their output below. If you have Docker installed, you can probably reproduce it locally.

=== TEST SCRIPT BEGIN ===
#!/bin/bash
make docker-image-fedora V=1 NETWORK=1
time make docker-test-debug@fedora TARGET_LIST=x86_64-softmmu J=14 NETWORK=1 === TEST SCRIPT END ===

  CC      audio/noaudio.o
  CC      audio/wavaudio.o
  CC      audio/mixeng.o
/tmp/qemu-test/src/block/nbd.c:710:30: error: suggest braces around initialization of subobject [-Werror,-Wmissing-braces]
    NBDReply local_reply = { 0, };
                             ^
                             {}

The full log is available at
http://patchew.org/logs/[email protected]/testing.asan/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]