Skip to contentSkip to conversation

Prevent uninitialized warnings

PATCH Applied [email protected] 5 participants 10 messages Jan 21, 2020 – Feb 6, 2020 Arm/Aspeed machine support

lore
Share & tools
lore
#0 lore raw

cover Prevent uninitialized warnings

From: Miroslav Rezanina <[email protected]>

Using -Wmaybe-uninitialized when optimalization is enabled can cause several warnings during build. This will break build in case -Werror is used.

This series fixes two cases of this warnings that can happen during build of QEMU.

Miroslav Rezanina (2):
test-logging: Fix -Werror=maybe-uninitialized warning aspeed/i2c: Prevent uninitialized warning

 hw/i2c/aspeed_i2c.c  | 2 +-
 tests/test-logging.c | 6 +++---
 2 files changed, 4 insertions(+), 4 deletions(-)
Signature · 1 line
1.8.3.1
#4 lore raw

PATCH 2/2 aspeed/i2c: Prevent uninitialized warning

From: Miroslav Rezanina <[email protected]>

Compiler reports uninitialized warning for cmd_flags variable.

Adding NULL initialization to prevent this warning.

---
 hw/i2c/aspeed_i2c.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/i2c/aspeed_i2c.c +1 -1
index 2da04a4..445182a 100644
@@ -400,7 +400,7 @@ static bool aspeed_i2c_check_sram(AspeedI2CBus *bus)
 
 static void aspeed_i2c_bus_cmd_dump(AspeedI2CBus *bus)
 {
-    g_autofree char *cmd_flags;
+    g_autofree char *cmd_flags = NULL;
     uint32_t count;
 
     if (bus->cmd & (I2CD_RX_BUFF_ENABLE | I2CD_RX_BUFF_ENABLE)) {
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH 1/2 test-logging: Fix -Werror=maybe-uninitialized warning

In reply to [email protected]

From: Miroslav Rezanina <[email protected]>

Checking for uninitialized variables raises warning for file path variables in test_logfile_write and test_logfile_lock functions.

To suppress this warning, initialize varibles to NULL. This is safe change as result of g_build_filename is stored to them before any usage.

---
 tests/test-logging.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tests/test-logging.c +3 -3
index 1e646f0..6387e49 100644
@@ -114,8 +114,8 @@ static void test_logfile_write(gconstpointer data)
     QemuLogFile *logfile2;
     gchar const *dir = data;
     Error *err = NULL;
-    g_autofree gchar *file_path;
-    g_autofree gchar *file_path1;
+    g_autofree gchar *file_path = NULL;
+    g_autofree gchar *file_path1 = NULL;
     FILE *orig_fd;
 
     /*
@@ -157,7 +157,7 @@ static void test_logfile_lock(gconstpointer data)
     FILE *logfile;
     gchar const *dir = data;
     Error *err = NULL;
-    g_autofree gchar *file_path;
+    g_autofree gchar *file_path = NULL;
 
     file_path = g_build_filename(dir, "qemu_test_logfile_lock0.log", NULL);
 
Signature · 1 line
1.8.3.1
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] test-logging: Fix -Werror=maybe-uninitialized warning

Quoted 29 lines · On 21/01/2020 10.28, [email protected] wrote:

From: Miroslav Rezanina <[email protected]>

Checking for uninitialized variables raises warning for file path variables in test_logfile_write and test_logfile_lock functions.

To suppress this warning, initialize varibles to NULL. This is safe change as result of g_build_filename is stored to them before any usage.

---
 tests/test-logging.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tests/test-logging.c +2 -2
index 1e646f0..6387e49 100644
@@ -114,8 +114,8 @@ static void test_logfile_write(gconstpointer data)
     QemuLogFile *logfile2;
     gchar const *dir = data;
     Error *err = NULL;
-    g_autofree gchar *file_path;
-    g_autofree gchar *file_path1;
+    g_autofree gchar *file_path = NULL;
+    g_autofree gchar *file_path1 = NULL;
     FILE *orig_fd;
 
     /*

tests/test-logging.c static void test_logfile_lock(gconstpointer data) · line 162

Quoted 7 lines
tests/test-logging.c +1 -1
@@ -157,7 +157,7 @@ static void test_logfile_lock(gconstpointer data)
     FILE *logfile;
     gchar const *dir = data;
     Error *err = NULL;
-    g_autofree gchar *file_path;
+    g_autofree gchar *file_path = NULL;
 
     file_path = g_build_filename(dir, "qemu_test_logfile_lock0.log", NULL);

Right. The glib documentation clearly states that "the variable must be initialized", see:

https://developer.gnome.org/glib/stable/glib-Miscellaneous-Macros.html#g-autofree

So this is the right thing to do here!

Reviewed-by on v1 2/2
#5 lore raw

Re: [PATCH 2/2] aspeed/i2c: Prevent uninitialized warning

In reply to [email protected]
Quoted 16 lines · On 21/01/2020 10.28, [email protected] wrote:

From: Miroslav Rezanina <[email protected]>

Compiler reports uninitialized warning for cmd_flags variable.

Adding NULL initialization to prevent this warning.

---
 hw/i2c/aspeed_i2c.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/i2c/aspeed_i2c.c +0 -0
index 2da04a4..445182a 100644

hw/i2c/aspeed_i2c.c static bool aspeed_i2c_check_sram(AspeedI2CBus *bus) · line 406

Quoted 8 lines
hw/i2c/aspeed_i2c.c +1 -1
@@ -400,7 +400,7 @@ static bool aspeed_i2c_check_sram(AspeedI2CBus *bus)
 
 static void aspeed_i2c_bus_cmd_dump(AspeedI2CBus *bus)
 {
-    g_autofree char *cmd_flags;
+    g_autofree char *cmd_flags = NULL;
     uint32_t count;
 
     if (bus->cmd & (I2CD_RX_BUFF_ENABLE | I2CD_RX_BUFF_ENABLE)) {

... maybe someone with enough Perl-foo (i.e. not me ;-)) should add a check to our check_patch.pl script so that it complains when new code is introduced that uses g_autofree without initializing the variable...

#6 lore raw

Re: [PATCH 2/2] aspeed/i2c: Prevent uninitialized warning

Quoted 24 lines · Thomas Huth
Quoted 23 lines · On 21/01/2020 10.28, [email protected] wrote:

From: Miroslav Rezanina <[email protected]>

Compiler reports uninitialized warning for cmd_flags variable.

Adding NULL initialization to prevent this warning.

---
 hw/i2c/aspeed_i2c.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/i2c/aspeed_i2c.c +1 -1
index 2da04a4..445182a 100644
@@ -400,7 +400,7 @@ static bool aspeed_i2c_check_sram(AspeedI2CBus *bus)
 
 static void aspeed_i2c_bus_cmd_dump(AspeedI2CBus *bus)
 {
-    g_autofree char *cmd_flags;
+    g_autofree char *cmd_flags = NULL;
     uint32_t count;
 
Quoted 7 lines

hw/i2c/aspeed_i2c.c static bool aspeed_i2c_check_sram(AspeedI2CBus *bus) · line 406

Quoted 1 line
hw/i2c/aspeed_i2c.c +0 -0
     if (bus->cmd & (I2CD_RX_BUFF_ENABLE | I2CD_RX_BUFF_ENABLE)) {

... maybe someone with enough Perl-foo (i.e. not me ;-)) should add a check to our check_patch.pl script so that it complains when new code is introduced that uses g_autofree without initializing the variable...

weird. The cmd_flags variable is assigned just after and used in a trace.

C.

#7 lore raw

Re: [PATCH 2/2] aspeed/i2c: Prevent uninitialized warning

----- Original Message -----

Quoted 36 lines

From: "Cédric Le Goater" <[email protected]>
To: "Thomas Huth" <[email protected]>, [email protected], [email protected]
Cc: "peter maydell" <[email protected]>, "Andrew Jeffery" <[email protected]>, "Joel Stanley" <[email protected]>, [email protected]
Sent: Tuesday, January 21, 2020 11:44:14 AM
Subject: Re: [PATCH 2/2] aspeed/i2c: Prevent uninitialized warning

Quoted 28 lines · Thomas Huth
Quoted 16 lines · On 21/01/2020 10.28, [email protected] wrote:

From: Miroslav Rezanina <[email protected]>

Compiler reports uninitialized warning for cmd_flags variable.

Adding NULL initialization to prevent this warning.

---
 hw/i2c/aspeed_i2c.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/i2c/aspeed_i2c.c +0 -0
index 2da04a4..445182a 100644

hw/i2c/aspeed_i2c.c static bool aspeed_i2c_check_sram(AspeedI2CBus *bus) · line 406

Quoted 8 lines
hw/i2c/aspeed_i2c.c +1 -1
@@ -400,7 +400,7 @@ static bool aspeed_i2c_check_sram(AspeedI2CBus *bus)
 
 static void aspeed_i2c_bus_cmd_dump(AspeedI2CBus *bus)
 {
-    g_autofree char *cmd_flags;
+    g_autofree char *cmd_flags = NULL;
     uint32_t count;
 
     if (bus->cmd & (I2CD_RX_BUFF_ENABLE | I2CD_RX_BUFF_ENABLE)) {
Quoted 7 lines
Quoted 3 lines

... maybe someone with enough Perl-foo (i.e. not me ;-)) should add a check to our check_patch.pl script so that it complains when new code is introduced that uses g_autofree without initializing the variable...

weird. The cmd_flags variable is assigned just after and used in a trace.

As g_autofree is used, variable has to be initialized otherwise will compiler complain even in the case we write to variable immediately after.

Mirek

Quoted 3 lines

C.

Signature · 2 lines
Miroslav Rezanina
Software Engineer - Virtualization Team Maintainer
#8 lore raw

Re: [PATCH 2/2] aspeed/i2c: Prevent uninitialized warning

In reply to Cédric Le Goater
Quoted 29 lines · Cédric Le Goater
Quoted 28 lines · Thomas Huth
Quoted 16 lines · On 21/01/2020 10.28, [email protected] wrote:

From: Miroslav Rezanina <[email protected]>

Compiler reports uninitialized warning for cmd_flags variable.

Adding NULL initialization to prevent this warning.

---
 hw/i2c/aspeed_i2c.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/i2c/aspeed_i2c.c +0 -0
index 2da04a4..445182a 100644

hw/i2c/aspeed_i2c.c static bool aspeed_i2c_check_sram(AspeedI2CBus *bus) · line 406

Quoted 8 lines
hw/i2c/aspeed_i2c.c +1 -1
@@ -400,7 +400,7 @@ static bool aspeed_i2c_check_sram(AspeedI2CBus *bus)
 
 static void aspeed_i2c_bus_cmd_dump(AspeedI2CBus *bus)
 {
-    g_autofree char *cmd_flags;
+    g_autofree char *cmd_flags = NULL;
     uint32_t count;
 
     if (bus->cmd & (I2CD_RX_BUFF_ENABLE | I2CD_RX_BUFF_ENABLE)) {
Quoted 6 lines
Quoted 3 lines

... maybe someone with enough Perl-foo (i.e. not me ;-)) should add a check to our check_patch.pl script so that it complains when new code is introduced that uses g_autofree without initializing the variable...

weird. The cmd_flags variable is assigned just after and used in a trace.

I don't know, but my guess is that you could compile with tracing disabled - in that case gcc might maybe optimize the assignment away, too... ?

Thomas

Reviewed-by
#3 lore raw

Re: [PATCH 1/2] test-logging: Fix -Werror=maybe-uninitialized warning

In reply to Thomas Huth

Good catch.

Quoted 48 lines · Thomas Huth
Quoted 29 lines · On 21/01/2020 10.28, [email protected] wrote:

From: Miroslav Rezanina <[email protected]>

Checking for uninitialized variables raises warning for file path variables in test_logfile_write and test_logfile_lock functions.

To suppress this warning, initialize varibles to NULL. This is safe change as result of g_build_filename is stored to them before any usage.

---
 tests/test-logging.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tests/test-logging.c +2 -2
index 1e646f0..6387e49 100644
@@ -114,8 +114,8 @@ static void test_logfile_write(gconstpointer data)
     QemuLogFile *logfile2;
     gchar const *dir = data;
     Error *err = NULL;
-    g_autofree gchar *file_path;
-    g_autofree gchar *file_path1;
+    g_autofree gchar *file_path = NULL;
+    g_autofree gchar *file_path1 = NULL;
     FILE *orig_fd;
 
     /*

tests/test-logging.c static void test_logfile_lock(gconstpointer data) · line 162

Quoted 7 lines
tests/test-logging.c +1 -1
@@ -157,7 +157,7 @@ static void test_logfile_lock(gconstpointer data)
     FILE *logfile;
     gchar const *dir = data;
     Error *err = NULL;
-    g_autofree gchar *file_path;
+    g_autofree gchar *file_path = NULL;
 
     file_path = g_build_filename(dir, "qemu_test_logfile_lock0.log", NULL);

Right. The glib documentation clearly states that "the variable must be initialized", see:

https://developer.gnome.org/glib/stable/glib-Miscellaneous-Macros.html#g-autofree

So this is the right thing to do here!

#9 lore raw

Re: [PATCH 2/2] aspeed/i2c: Prevent uninitialized warning

In reply to [email protected]

Le 21/01/2020 à 10:28, [email protected] a écrit :

Quoted 17 lines

From: Miroslav Rezanina <[email protected]>

Compiler reports uninitialized warning for cmd_flags variable.

Adding NULL initialization to prevent this warning.

---
 hw/i2c/aspeed_i2c.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/i2c/aspeed_i2c.c +0 -0
index 2da04a4..445182a 100644

hw/i2c/aspeed_i2c.c static bool aspeed_i2c_check_sram(AspeedI2CBus *bus) · line 406

Quoted 8 lines
hw/i2c/aspeed_i2c.c +1 -1
@@ -400,7 +400,7 @@ static bool aspeed_i2c_check_sram(AspeedI2CBus *bus)
 
 static void aspeed_i2c_bus_cmd_dump(AspeedI2CBus *bus)
 {
-    g_autofree char *cmd_flags;
+    g_autofree char *cmd_flags = NULL;
     uint32_t count;
 
     if (bus->cmd & (I2CD_RX_BUFF_ENABLE | I2CD_RX_BUFF_ENABLE)) {

Applied to my trivial-patches branch.

Thanks,
Laurent