Skip to contentSkip to conversation

migration/block-dirty-bitmap: fix uninitialized variable warning

PATCH Superseded Chen Qun 2 participants 5 messages Oct 13, 2020 – Oct 14, 2020

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Chen Qun

author
#0 lore raw

PATCH v2 1/1 migration/block-dirty-bitmap: fix uninitialized variable warning

A default value is provided for the variable 'bitmap_name' to avoid compiler warning.

The compiler show warning:
migration/block-dirty-bitmap.c:1090:13: warning: ‘bitmap_name’ may be used uninitialized in this function [-Wmaybe-uninitialized]

       g_strlcpy(s->bitmap_name, bitmap_name, sizeof(s->bitmap_name));
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---
Cc: Vladimir Sementsov-Ogievskiy <[email protected]>
Cc: Laurent Vivier <[email protected]>
Cc: Li Qiang <[email protected]>

---
 migration/block-dirty-bitmap.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
migration/block-dirty-bitmap.c +2 -7
index 5bef793ac0..bcb79c04ce 100644
@@ -1064,15 +1064,13 @@ static int dirty_bitmap_load_header(QEMUFile *f, DBMLoadState *s,
     assert(nothing || s->cancelled || !!alias_map == !!bitmap_alias_map);
 
     if (s->flags & DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME) {
-        const char *bitmap_name;
-
         if (!qemu_get_counted_string(f, s->bitmap_alias)) {
             error_report("Unable to read bitmap alias string");
             return -EINVAL;
         }
 
-        if (!s->cancelled) {
-            if (bitmap_alias_map) {
+        const char *bitmap_name = s->bitmap_alias;
+        if (!s->cancelled && bitmap_alias_map) {
                 bitmap_name = g_hash_table_lookup(bitmap_alias_map,
                                                   s->bitmap_alias);
                 if (!bitmap_name) {
@@ -1081,9 +1079,6 @@ static int dirty_bitmap_load_header(QEMUFile *f, DBMLoadState *s,
                                  s->bs->node_name, s->node_alias);
                     cancel_incoming_locked(s);
                 }
-            } else {
-                bitmap_name = s->bitmap_alias;
-            }
         }
 
         if (!s->cancelled) {
Signature · 1 line
2.23.0
#1 lore raw
Quoted 10 lines · Chen Qun

A default value is provided for the variable 'bitmap_name' to avoid compiler warning.

The compiler show warning:
migration/block-dirty-bitmap.c:1090:13: warning: ‘bitmap_name’ may be used uninitialized in this function [-Wmaybe-uninitialized]

       g_strlcpy(s->bitmap_name, bitmap_name, sizeof(s->bitmap_name));
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Quoted 7 lines

---
Cc: Vladimir Sementsov-Ogievskiy <[email protected]>
Cc: Laurent Vivier <[email protected]>
Cc: Li Qiang <[email protected]>

---
 migration/block-dirty-bitmap.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)

No objections, semantically, but...

Quoted 8 lines
migration/block-dirty-bitmap.c +0 -0
index 5bef793ac0..bcb79c04ce 100644
@@ -1064,15 +1064,13 @@ static int dirty_bitmap_load_header(QEMUFile *f, DBMLoadState *s,
     assert(nothing || s->cancelled || !!alias_map == !!bitmap_alias_map);
 
     if (s->flags & DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME) {

migration/block-dirty-bitmap.c static int dirty_bitmap_load_header(QEMUFile *f, DBMLoadState *s, · line 1072

Quoted 10 lines
migration/block-dirty-bitmap.c +1 -4
-        const char *bitmap_name;
-
         if (!qemu_get_counted_string(f, s->bitmap_alias)) {
             error_report("Unable to read bitmap alias string");
             return -EINVAL;
         }
 
-        if (!s->cancelled) {
-            if (bitmap_alias_map) {
+        const char *bitmap_name = s->bitmap_alias;

qemu’s coding style mandates declarations to be placed at the beginning of their block, so the declaration has to stay where it is. (Putting the assignment here looks reasonable.)

migration/block-dirty-bitmap.c static int dirty_bitmap_load_header(QEMUFile *f, DBMLoadState *s, · line 1075

Quoted 3 lines
migration/block-dirty-bitmap.c +1 -0
+        if (!s->cancelled && bitmap_alias_map) {
                 bitmap_name = g_hash_table_lookup(bitmap_alias_map,
                                                   s->bitmap_alias);

This block of course needs to be re-indented.

Max

Quoted 12 lines
migration/block-dirty-bitmap.c +0 -3
                 if (!bitmap_name) {
@@ -1081,9 +1079,6 @@ static int dirty_bitmap_load_header(QEMUFile *f, DBMLoadState *s,
                                  s->bs->node_name, s->node_alias);
                     cancel_incoming_locked(s);
                 }
-            } else {
-                bitmap_name = s->bitmap_alias;
-            }
         }
 
         if (!s->cancelled) {
#2 lore raw
Quoted 57 lines

-----Original Message-----
From: Max Reitz [mailto:[email protected]]
Sent: Tuesday, October 13, 2020 10:47 PM
To: Chenqun (kuhn) <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Zhanghailiang <[email protected]>; ganqixin <[email protected]>; [email protected]; Euler Robot <[email protected]>; Laurent Vivier <[email protected]>; Li Qiang <[email protected]>
Subject: Re: [PATCH v2] migration/block-dirty-bitmap: fix uninitialized variable warning

Quoted 1 line · Chen Qun

A default value is provided for the variable 'bitmap_name' to avoid compiler

warning.

Quoted 5 lines

The compiler show warning:
migration/block-dirty-bitmap.c:1090:13: warning: ‘bitmap_name’ may be used uninitialized in this function [-Wmaybe-uninitialized]

       g_strlcpy(s->bitmap_name, bitmap_name,

sizeof(s->bitmap_name));

Quoted 1 line

^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Quoted 10 lines

---
Cc: Vladimir Sementsov-Ogievskiy <[email protected]>
Cc: Laurent Vivier <[email protected]>
Cc: Li Qiang <[email protected]>

---
 migration/block-dirty-bitmap.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)

No objections, semantically, but...

migration/block-dirty-bitmap.c static int dirty_bitmap_load_header(QEMUFile

Quoted 5 lines
(quoted diff) +0 -0

b/migration/block-dirty-bitmap.c index 5bef793ac0..bcb79c04ce 100644

migration/block-dirty-bitmap.c +0 -0
@@ -1064,15 +1064,13 @@ static int dirty_bitmap_load_header(QEMUFile

*f, DBMLoadState *s,

Quoted 11 lines
migration/block-dirty-bitmap.c +0 -2
     assert(nothing || s->cancelled || !!alias_map ==
 !!bitmap_alias_map);
 
     if (s->flags & DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME) {
-        const char *bitmap_name;
-
         if (!qemu_get_counted_string(f, s->bitmap_alias)) {
             error_report("Unable to read bitmap alias string");
             return -EINVAL;
         }
 
Quoted 8 lines

migration/block-dirty-bitmap.c static int dirty_bitmap_load_header(QEMUFile *f, DBMLoadState *s, · line 1072

Quoted 3 lines
migration/block-dirty-bitmap.c +1 -2
-        if (!s->cancelled) {
-            if (bitmap_alias_map) {
+        const char *bitmap_name = s->bitmap_alias;

qemu’s coding style mandates declarations to be placed at the beginning of their block, so the declaration has to stay where it is. (Putting the assignment here looks reasonable.)

Hi Max,
Declaration variables here to ensure that the above exceptions(Unable to read bitmap alias string) are avoided.
If the declaration has to stay where it is, is there a possibility that the assignment fails?

Quoted 8 lines

migration/block-dirty-bitmap.c static int dirty_bitmap_load_header(QEMUFile · line 1075

Quoted 2 lines
migration/block-dirty-bitmap.c +1 -0
+        if (!s->cancelled && bitmap_alias_map) {
                 bitmap_name =

g_hash_table_lookup(bitmap_alias_map,

Quoted 1 line

s->bitmap_alias);

This block of course needs to be re-indented.

I forgot this. I will update it later.

Thanks,
ChenQun

Quoted 16 lines

migration/block-dirty-bitmap.c static int dirty_bitmap_load_header(QEMUFile *f,

Quoted 3 lines
                 if (!bitmap_name) {
migration/block-dirty-bitmap.c +0 -0
@@ -1081,9 +1079,6 @@ static int dirty_bitmap_load_header(QEMUFile *f,

DBMLoadState *s,

migration/block-dirty-bitmap.c static int dirty_bitmap_load_header(QEMUFile *f, · line 1079

Quoted 1 line
migration/block-dirty-bitmap.c +0 -0
                                  s->bs->node_name,

s->node_alias);

Quoted 9 lines
migration/block-dirty-bitmap.c +0 -3
                     cancel_incoming_locked(s);
                 }
-            } else {
-                bitmap_name = s->bitmap_alias;
-            }
         }
 
         if (!s->cancelled) {
#3 lore raw
Quoted 61 lines · Chenqun (kuhn)
Quoted 61 lines

-----Original Message-----
From: Max Reitz [mailto:[email protected]]
Sent: Tuesday, October 13, 2020 10:47 PM
To: Chenqun (kuhn) <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Zhanghailiang <[email protected]>; ganqixin <[email protected]>; [email protected]; Euler Robot <[email protected]>; Laurent Vivier <[email protected]>; Li Qiang <[email protected]>
Subject: Re: [PATCH v2] migration/block-dirty-bitmap: fix uninitialized variable warning

Quoted 1 line · Chen Qun

A default value is provided for the variable 'bitmap_name' to avoid compiler

warning.

Quoted 5 lines

The compiler show warning:
migration/block-dirty-bitmap.c:1090:13: warning: ‘bitmap_name’ may be used uninitialized in this function [-Wmaybe-uninitialized]

       g_strlcpy(s->bitmap_name, bitmap_name,

sizeof(s->bitmap_name));

Quoted 1 line

^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Quoted 10 lines

---
Cc: Vladimir Sementsov-Ogievskiy <[email protected]>
Cc: Laurent Vivier <[email protected]>
Cc: Li Qiang <[email protected]>

---
 migration/block-dirty-bitmap.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)

No objections, semantically, but...

migration/block-dirty-bitmap.c static int dirty_bitmap_load_header(QEMUFile

Quoted 5 lines
(quoted diff) +0 -0

b/migration/block-dirty-bitmap.c index 5bef793ac0..bcb79c04ce 100644

migration/block-dirty-bitmap.c +0 -0
@@ -1064,15 +1064,13 @@ static int dirty_bitmap_load_header(QEMUFile

*f, DBMLoadState *s,

Quoted 14 lines
migration/block-dirty-bitmap.c +1 -4
     assert(nothing || s->cancelled || !!alias_map ==
 !!bitmap_alias_map);
 
     if (s->flags & DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME) {
-        const char *bitmap_name;
-
         if (!qemu_get_counted_string(f, s->bitmap_alias)) {
             error_report("Unable to read bitmap alias string");
             return -EINVAL;
         }
 
-        if (!s->cancelled) {
-            if (bitmap_alias_map) {
+        const char *bitmap_name = s->bitmap_alias;
Quoted 7 lines
Quoted 4 lines

qemu’s coding style mandates declarations to be placed at the beginning of their block, so the declaration has to stay where it is. (Putting the assignment here looks reasonable.)

Hi Max,
Declaration variables here to ensure that the above exceptions(Unable to read bitmap alias string) are avoided.
If the declaration has to stay where it is, is there a possibility that the assignment fails?

I don’t understand what you mean. A declaration without initialization isn’t and doesn’t contain an expression, it isn’t even a statement, so it has no side effects.[1]

Placing the declaration (without an initialization) at the top of the block makes no semantic difference.

(As I said, I’d keep the assignment “bitmap_name = s->bitmap_alias” where you put it. I think it would technically actually be correct to put it into the declaration at the start of the block as an initializer, but that would look weird.)

Max

[1] I suppose exceptions apply for types with constructors, but those don’t exist in plain C.

#4 lore raw
Quoted 86 lines

-----Original Message-----
From: Qemu-devel [mailto:[email protected]] On Behalf Of Max Reitz
Sent: Wednesday, October 14, 2020 5:36 PM
To: Chenqun (kuhn) <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; ganqixin <[email protected]>; [email protected]; Zhanghailiang <[email protected]>; [email protected]; [email protected]; Li Qiang <[email protected]>; [email protected]; Laurent Vivier <[email protected]>; [email protected]; Euler Robot <[email protected]>; [email protected]
Subject: Re: [PATCH v2] migration/block-dirty-bitmap: fix uninitialized variable warning

Quoted 6 lines · Chenqun (kuhn)
Quoted 6 lines

-----Original Message-----
From: Max Reitz [mailto:[email protected]]
Sent: Tuesday, October 13, 2020 10:47 PM
To: Chenqun (kuhn) <[email protected]>;

[email protected];

Quoted 15 lines
Quoted 15 lines

[email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Zhanghailiang <[email protected]>; ganqixin <[email protected]>; [email protected]; Euler Robot <[email protected]>; Laurent Vivier <[email protected]>; Li Qiang <[email protected]>
Subject: Re: [PATCH v2] migration/block-dirty-bitmap: fix uninitialized variable warning

Quoted 2 lines · Chen Qun

A default value is provided for the variable 'bitmap_name' to avoid compiler

warning.

Quoted 1 line
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines

The compiler show warning:
migration/block-dirty-bitmap.c:1090:13: warning: ‘bitmap_name’ may be used uninitialized in this function [-Wmaybe-uninitialized]

       g_strlcpy(s->bitmap_name, bitmap_name,

sizeof(s->bitmap_name));

Quoted 1 line

^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines

---
Cc: Vladimir Sementsov-Ogievskiy <[email protected]>
Cc: Laurent Vivier <[email protected]>
Cc: Li Qiang <[email protected]>

---
 migration/block-dirty-bitmap.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
Quoted 7 lines
Quoted 7 lines

No objections, semantically, but...

migration/block-dirty-bitmap.c static int

Quoted 5 lines
(quoted diff) +0 -0

b/migration/block-dirty-bitmap.c index 5bef793ac0..bcb79c04ce 100644

migration/block-dirty-bitmap.c +0 -0
@@ -1064,15 +1064,13 @@ static int

dirty_bitmap_load_header(QEMUFile

Quoted 20 lines
Quoted 20 lines

*f, DBMLoadState *s,

Quoted 4 lines
migration/block-dirty-bitmap.c +0 -0
     assert(nothing || s->cancelled || !!alias_map ==
 !!bitmap_alias_map);
 
     if (s->flags & DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME) {

migration/block-dirty-bitmap.c static int dirty_bitmap_load_header(QEMUFile *f, DBMLoadState *s, · line 1072

Quoted 10 lines
migration/block-dirty-bitmap.c +1 -4
-        const char *bitmap_name;
-
         if (!qemu_get_counted_string(f, s->bitmap_alias)) {
             error_report("Unable to read bitmap alias string");
             return -EINVAL;
         }
 
-        if (!s->cancelled) {
-            if (bitmap_alias_map) {
+        const char *bitmap_name = s->bitmap_alias;

qemu’s coding style mandates declarations to be placed at the beginning of their block, so the declaration has to stay where it is. (Putting the assignment here looks reasonable.)

Quoted 7 lines
Quoted 2 lines

Hi Max,
Declaration variables here to ensure that the above exceptions(Unable to

read bitmap alias string) are avoided.

Quoted 1 line

If the declaration has to stay where it is, is there a possibility that the

assignment fails?

I don’t understand what you mean.

I think my description is not accurate. Forgive me for being a non-native English speaker.
The variable 'bitmap_name' assignment maybe failed at the beginning of the block, because reading the 's->bitmap_alias' maybe failed.

Quoted 7 lines

A declaration without initialization isn’t
and doesn’t contain an expression, it isn’t even a statement, so it has no side effects.[1]

Placing the declaration (without an initialization) at the top of the block makes no semantic difference.

I see what you mean. Separate variable declarations from variable assignments. You're right! I will update it later.

Thanks,
Chen Qun

Quoted 9 lines

(As I said, I’d keep the assignment “bitmap_name = s->bitmap_alias” where you put it. I think it would technically actually be correct to put it into the declaration at the start of the block as an initializer, but that would look weird.)

Max

[1] I suppose exceptions apply for types with constructors, but those don’t exist in plain C.