Prevent uninitialized warnings
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
PATCH 1/2 test-logging: Fix -Werror=maybe-uninitialized warning
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
@@ -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
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_writeandtest_logfile_lockfunctions.To suppress this warning, initialize varibles to NULL. This is safe change as result of
g_build_filenameis stored to them before any usage.Signed-off-by Miroslav Rezanina <[email protected]>--- tests/test-logging.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)tests/test-logging.c +2
-2@@ -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!
Robert Foley
Reviewed-byRe: [PATCH 1/2] test-logging: Fix -Werror=maybe-uninitialized warning
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_writeandtest_logfile_lockfunctions.To suppress this warning, initialize varibles to NULL. This is safe change as result of
g_build_filenameis stored to them before any usage.Signed-off-by Miroslav Rezanina <[email protected]>--- tests/test-logging.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)tests/test-logging.c +2
-2@@ -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 Thomas Huth <[email protected]>
PATCH 2/2 aspeed/i2c: Prevent uninitialized warning
In reply to [email protected]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
@@ -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
Re: [PATCH 2/2] aspeed/i2c: Prevent uninitialized warning
Quoted 16 lines · On 21/01/2020 10.28, [email protected] wrote:
From: Miroslav Rezanina <[email protected]>
Compiler reports uninitialized warning for
cmd_flagsvariable.Adding NULL initialization to prevent this warning.
Signed-off-by Miroslav Rezanina <[email protected]>--- hw/i2c/aspeed_i2c.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/i2c/aspeed_i2c.c +0
-0
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...
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_flagsvariable.Adding NULL initialization to prevent this warning.
Signed-off-by Miroslav Rezanina <[email protected]>--- hw/i2c/aspeed_i2c.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)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;
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
-0if (bus->cmd & (I2CD_RX_BUFF_ENABLE |I2CD_RX_BUFF_ENABLE )) {Reviewed-by Thomas Huth <[email protected]>... 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_autofreewithout initializing the variable...
weird. The cmd_flags variable is assigned just after and used in a trace.
C.
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 warningQuoted 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_flagsvariable.Adding NULL initialization to prevent this warning.
Signed-off-by Miroslav Rezanina <[email protected]>--- hw/i2c/aspeed_i2c.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/i2c/aspeed_i2c.c +0
-0hw/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 )) {Reviewed-by Thomas Huth <[email protected]>
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_autofreewithout initializing the variable...weird. The
cmd_flagsvariable 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
Re: [PATCH 2/2] aspeed/i2c: Prevent uninitialized warning
In reply to Cédric Le GoaterQuoted 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_flagsvariable.Adding NULL initialization to prevent this warning.
Signed-off-by Miroslav Rezanina <[email protected]>--- hw/i2c/aspeed_i2c.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/i2c/aspeed_i2c.c +0
-0hw/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 )) {Reviewed-by Thomas Huth <[email protected]>
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_autofreewithout initializing the variable...weird. The
cmd_flagsvariable 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
Laurent Vivier
AppliedRe: [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_flagsvariable.Adding NULL initialization to prevent this warning.
Signed-off-by Miroslav Rezanina <[email protected]>--- hw/i2c/aspeed_i2c.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/i2c/aspeed_i2c.c +0
-0
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