diff mbox

kconfig/menu.c: fix uninitialized variable warning

Message ID 1379575697-6740-1-git-send-email-maddy@linux.vnet.ibm.com (mailing list archive)
State New, archived
Headers show

Commit Message

Madhavan Srinivasan Sept. 19, 2013, 7:28 a.m. UTC
In file included from scripts/kconfig/zconf.tab.c:2537:0:
scripts/kconfig/menu.c: In function ‘get_symbol_str’:
scripts/kconfig/menu.c:586:18: warning: ‘jump’ may be used uninitialized in
this function [-Wmaybe-uninitialized]
     jump->offset = r->len - 1;
                  ^
scripts/kconfig/menu.c:547:19: note: ‘jump’ was declared here
  struct jump_key *jump;
                   ^
Signed-off-by: Madhavan Srinivasan <maddy@linux.vnet.ibm.com>
---
 scripts/kconfig/menu.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

--
1.7.10.4

--
To unsubscribe from this list: send the line "unsubscribe linux-kbuild" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Comments

Benjamin Poirier Sept. 19, 2013, 3:22 p.m. UTC | #1
On 2013/09/19 12:58, Madhavan Srinivasan wrote:
> In file included from scripts/kconfig/zconf.tab.c:2537:0:
> scripts/kconfig/menu.c: In function ‘get_symbol_str’:
> scripts/kconfig/menu.c:586:18: warning: ‘jump’ may be used uninitialized in
> this function [-Wmaybe-uninitialized]
                   ^^^^^

from gcc(1):
	"These warnings are made optional because GCC is not smart
	enough to see all the reasons why the code might be correct
	despite appearing to have an error."

I do not see this warning with gcc 4.7.2 or 4.8.1. Which version are you
using?

The code does:

static void get_prompt_str(struct gstr *r, struct property *prop,
[...]
	if (head && location) {
		jump = xmalloc(sizeof(struct jump_key));
[... different basic block ...]
			if (head && location && menu == location)
				jump->offset = r->len - 1;

If the second "if" is true, then the first one was true as well, and
"jump" is initilized.

>      jump->offset = r->len - 1;
>                   ^
> scripts/kconfig/menu.c:547:19: note: ‘jump’ was declared here
>   struct jump_key *jump;
>                    ^
> Signed-off-by: Madhavan Srinivasan <maddy@linux.vnet.ibm.com>
> ---
>  scripts/kconfig/menu.c |    2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/scripts/kconfig/menu.c b/scripts/kconfig/menu.c
> index c1d5320..23b1827 100644
> --- a/scripts/kconfig/menu.c
> +++ b/scripts/kconfig/menu.c
> @@ -544,7 +544,7 @@ static void get_prompt_str(struct gstr *r, struct property *prop,
>  {
>  	int i, j;
>  	struct menu *submenu[8], *menu, *location = NULL;
> -	struct jump_key *jump;
> +	struct jump_key *jump = NULL;
> 
>  	str_printf(r, _("Prompt: %s\n"), _(prop->text));
>  	menu = prop->menu->parent;
> --
> 1.7.10.4
> 
--
To unsubscribe from this list: send the line "unsubscribe linux-kbuild" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Yann E. MORIN Sept. 19, 2013, 5:27 p.m. UTC | #2
Benjamin, Madhavan, All,

On 2013-09-19 11:22 -0400, Benjamin Poirier spake thusly:
> On 2013/09/19 12:58, Madhavan Srinivasan wrote:
> > In file included from scripts/kconfig/zconf.tab.c:2537:0:
> > scripts/kconfig/menu.c: In function ‘get_symbol_str’:
> > scripts/kconfig/menu.c:586:18: warning: ‘jump’ may be used uninitialized in
> > this function [-Wmaybe-uninitialized]
>                    ^^^^^
> 
> from gcc(1):
> 	"These warnings are made optional because GCC is not smart
> 	enough to see all the reasons why the code might be correct
> 	despite appearing to have an error."
> 
> I do not see this warning with gcc 4.7.2 or 4.8.1. Which version are you
> using?
> 
> The code does:
> 
> static void get_prompt_str(struct gstr *r, struct property *prop,
> [...]
> 	if (head && location) {
> 		jump = xmalloc(sizeof(struct jump_key));
> [... different basic block ...]
> 			if (head && location && menu == location)
> 				jump->offset = r->len - 1;
> 
> If the second "if" is true, then the first one was true as well, and
> "jump" is initilized.

Exactly what I was going to say.

However:
  - I believe we should strive for a warning-free code whenever possible
  - while still getting help from gcc to pinpoint potential issues.

Clearly, gcc is wrong here. Setting jump to NULL will cause fault if we
try to dereference it. Since this should never happen given the code as
it currently is, I'm slightly in favour of acking this patch.

Any other reason not to apply it?

> >      jump->offset = r->len - 1;
> >                   ^
> > scripts/kconfig/menu.c:547:19: note: ‘jump’ was declared here
> >   struct jump_key *jump;
> >                    ^
> > Signed-off-by: Madhavan Srinivasan <maddy@linux.vnet.ibm.com>
> > ---
> >  scripts/kconfig/menu.c |    2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > diff --git a/scripts/kconfig/menu.c b/scripts/kconfig/menu.c
> > index c1d5320..23b1827 100644
> > --- a/scripts/kconfig/menu.c
> > +++ b/scripts/kconfig/menu.c
> > @@ -544,7 +544,7 @@ static void get_prompt_str(struct gstr *r, struct property *prop,
> >  {
> >  	int i, j;
> >  	struct menu *submenu[8], *menu, *location = NULL;
> > -	struct jump_key *jump;
> > +	struct jump_key *jump = NULL;
> > 
> >  	str_printf(r, _("Prompt: %s\n"), _(prop->text));
> >  	menu = prop->menu->parent;
> > --
> > 1.7.10.4
> > 

Regards,
Yann E. MORIN.
Benjamin Poirier Sept. 19, 2013, 7:13 p.m. UTC | #3
On 2013/09/19 19:27, Yann E. MORIN wrote:
> Benjamin, Madhavan, All,
> 
> On 2013-09-19 11:22 -0400, Benjamin Poirier spake thusly:
> > On 2013/09/19 12:58, Madhavan Srinivasan wrote:
> > > In file included from scripts/kconfig/zconf.tab.c:2537:0:
> > > scripts/kconfig/menu.c: In function ‘get_symbol_str’:
> > > scripts/kconfig/menu.c:586:18: warning: ‘jump’ may be used uninitialized in
> > > this function [-Wmaybe-uninitialized]
> >                    ^^^^^
> > 
> > from gcc(1):
> > 	"These warnings are made optional because GCC is not smart
> > 	enough to see all the reasons why the code might be correct
> > 	despite appearing to have an error."
> > 
> > I do not see this warning with gcc 4.7.2 or 4.8.1. Which version are you
> > using?
> > 
> > The code does:
> > 
> > static void get_prompt_str(struct gstr *r, struct property *prop,
> > [...]
> > 	if (head && location) {
> > 		jump = xmalloc(sizeof(struct jump_key));
> > [... different basic block ...]
> > 			if (head && location && menu == location)
> > 				jump->offset = r->len - 1;
> > 
> > If the second "if" is true, then the first one was true as well, and
> > "jump" is initilized.
> 
> Exactly what I was going to say.
> 
> However:
>   - I believe we should strive for a warning-free code whenever possible
>   - while still getting help from gcc to pinpoint potential issues.
> 
> Clearly, gcc is wrong here. Setting jump to NULL will cause fault if we
> try to dereference it. Since this should never happen given the code as
> it currently is, I'm slightly in favour of acking this patch.
> 
> Any other reason not to apply it?

Did you manage to get that warning as well? I didn't. If it's only from
old versions of gcc I'd feel better leaving the warning there than
masking it, in case a future code change does really introduce a use
before initialize.

> 
> > >      jump->offset = r->len - 1;
> > >                   ^
> > > scripts/kconfig/menu.c:547:19: note: ‘jump’ was declared here
> > >   struct jump_key *jump;
> > >                    ^
> > > Signed-off-by: Madhavan Srinivasan <maddy@linux.vnet.ibm.com>
> > > ---
> > >  scripts/kconfig/menu.c |    2 +-
> > >  1 file changed, 1 insertion(+), 1 deletion(-)
> > > 
> > > diff --git a/scripts/kconfig/menu.c b/scripts/kconfig/menu.c
> > > index c1d5320..23b1827 100644
> > > --- a/scripts/kconfig/menu.c
> > > +++ b/scripts/kconfig/menu.c
> > > @@ -544,7 +544,7 @@ static void get_prompt_str(struct gstr *r, struct property *prop,
> > >  {
> > >  	int i, j;
> > >  	struct menu *submenu[8], *menu, *location = NULL;
> > > -	struct jump_key *jump;
> > > +	struct jump_key *jump = NULL;
> > > 
> > >  	str_printf(r, _("Prompt: %s\n"), _(prop->text));
> > >  	menu = prop->menu->parent;
> > > --
> > > 1.7.10.4
> > > 
> 
> Regards,
> Yann E. MORIN.
> 
> -- 
> .-----------------.--------------------.------------------.--------------------.
> |  Yann E. MORIN  | Real-Time Embedded | /"\ ASCII RIBBON | Erics' conspiracy: |
> | +33 662 376 056 | Software  Designer | \ / CAMPAIGN     |  ___               |
> | +33 223 225 172 `------------.-------:  X  AGAINST      |  \e/  There is no  |
> | http://ymorin.is-a-geek.org/ | _/*\_ | / \ HTML MAIL    |   v   conspiracy.  |
> '------------------------------^-------^------------------^--------------------'
--
To unsubscribe from this list: send the line "unsubscribe linux-kbuild" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Yann E. MORIN Sept. 19, 2013, 8:04 p.m. UTC | #4
Benjamin, All,

On 2013-09-19 15:13 -0400, Benjamin Poirier spake thusly:
> On 2013/09/19 19:27, Yann E. MORIN wrote:
> > Benjamin, Madhavan, All,
> > 
> > On 2013-09-19 11:22 -0400, Benjamin Poirier spake thusly:
> > > On 2013/09/19 12:58, Madhavan Srinivasan wrote:
> > > > In file included from scripts/kconfig/zconf.tab.c:2537:0:
> > > > scripts/kconfig/menu.c: In function ‘get_symbol_str’:
> > > > scripts/kconfig/menu.c:586:18: warning: ‘jump’ may be used uninitialized in
> > > > this function [-Wmaybe-uninitialized]
> > >                    ^^^^^
> > > 
> > > from gcc(1):
> > > 	"These warnings are made optional because GCC is not smart
> > > 	enough to see all the reasons why the code might be correct
> > > 	despite appearing to have an error."
> > > 
> > > I do not see this warning with gcc 4.7.2 or 4.8.1. Which version are you
> > > using?
> > > 
> > > The code does:
> > > 
> > > static void get_prompt_str(struct gstr *r, struct property *prop,
> > > [...]
> > > 	if (head && location) {
> > > 		jump = xmalloc(sizeof(struct jump_key));
> > > [... different basic block ...]
> > > 			if (head && location && menu == location)
> > > 				jump->offset = r->len - 1;
> > > 
> > > If the second "if" is true, then the first one was true as well, and
> > > "jump" is initilized.
> > 
> > Exactly what I was going to say.
> > 
> > However:
> >   - I believe we should strive for a warning-free code whenever possible
> >   - while still getting help from gcc to pinpoint potential issues.
> > 
> > Clearly, gcc is wrong here. Setting jump to NULL will cause fault if we
> > try to dereference it. Since this should never happen given the code as
> > it currently is, I'm slightly in favour of acking this patch.
> > 
> > Any other reason not to apply it?
> 
> Did you manage to get that warning as well? I didn't. If it's only from
> old versions of gcc I'd feel better leaving the warning there than
> masking it, in case a future code change does really introduce a use
> before initialize.

No, I was not able to reproduce it with either gcc-4.4.7, gcc-4.6.4 or
gcc-4.7.3 on my Ubuntu 13.04. Neither gcc-4.5 nor 4.8 are packaged, so
I could not test them.

Anyway, -Wmaybe-uninitialized is new with 4.7. So, the warning can't
happen with gcc < 4.7.

Madhavan, what gcc version are you using?

Regards,
Yann E. MORIN.
Madhavan Srinivasan Sept. 20, 2013, 2:35 a.m. UTC | #5
On Friday 20 September 2013 01:34 AM, Yann E. MORIN wrote:
> Benjamin, All,
> 
> On 2013-09-19 15:13 -0400, Benjamin Poirier spake thusly:
>> On 2013/09/19 19:27, Yann E. MORIN wrote:
>>> Benjamin, Madhavan, All,
>>>
>>> On 2013-09-19 11:22 -0400, Benjamin Poirier spake thusly:
>>>> On 2013/09/19 12:58, Madhavan Srinivasan wrote:
>>>>> In file included from scripts/kconfig/zconf.tab.c:2537:0:
>>>>> scripts/kconfig/menu.c: In function ‘get_symbol_str’:
>>>>> scripts/kconfig/menu.c:586:18: warning: ‘jump’ may be used uninitialized in
>>>>> this function [-Wmaybe-uninitialized]
>>>>                    ^^^^^
>>>>
>>>> from gcc(1):
>>>> 	"These warnings are made optional because GCC is not smart
>>>> 	enough to see all the reasons why the code might be correct
>>>> 	despite appearing to have an error."
>>>>
>>>> I do not see this warning with gcc 4.7.2 or 4.8.1. Which version are you
>>>> using?
>>>>
>>>> The code does:
>>>>
>>>> static void get_prompt_str(struct gstr *r, struct property *prop,
>>>> [...]
>>>> 	if (head && location) {
>>>> 		jump = xmalloc(sizeof(struct jump_key));
>>>> [... different basic block ...]
>>>> 			if (head && location && menu == location)
>>>> 				jump->offset = r->len - 1;
>>>>
>>>> If the second "if" is true, then the first one was true as well, and
>>>> "jump" is initilized.
>>>
>>> Exactly what I was going to say.
>>>
>>> However:
>>>   - I believe we should strive for a warning-free code whenever possible
>>>   - while still getting help from gcc to pinpoint potential issues.
>>>
>>> Clearly, gcc is wrong here. Setting jump to NULL will cause fault if we
>>> try to dereference it. Since this should never happen given the code as
>>> it currently is, I'm slightly in favour of acking this patch.
>>>
>>> Any other reason not to apply it?
>>
>> Did you manage to get that warning as well? I didn't. If it's only from
>> old versions of gcc I'd feel better leaving the warning there than
>> masking it, in case a future code change does really introduce a use
>> before initialize.
> 
> No, I was not able to reproduce it with either gcc-4.4.7, gcc-4.6.4 or
> gcc-4.7.3 on my Ubuntu 13.04. Neither gcc-4.5 nor 4.8 are packaged, so
> I could not test them.
> 
> Anyway, -Wmaybe-uninitialized is new with 4.7. So, the warning can't
> happen with gcc < 4.7.
> 
> Madhavan, what gcc version are you using?
gcc i have is gcc version 4.8.0 20130526
> 
> Regards,
> Yann E. MORIN.
> 

--
To unsubscribe from this list: send the line "unsubscribe linux-kbuild" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Benjamin Poirier Sept. 20, 2013, 5:58 p.m. UTC | #6
On 2013/09/20 08:05, Madhavan Srinivasan wrote:
> On Friday 20 September 2013 01:34 AM, Yann E. MORIN wrote:
> > Benjamin, All,
> > 
> > On 2013-09-19 15:13 -0400, Benjamin Poirier spake thusly:
> >> On 2013/09/19 19:27, Yann E. MORIN wrote:
> >>> Benjamin, Madhavan, All,
> >>>
> >>> On 2013-09-19 11:22 -0400, Benjamin Poirier spake thusly:
> >>>> On 2013/09/19 12:58, Madhavan Srinivasan wrote:
> >>>>> In file included from scripts/kconfig/zconf.tab.c:2537:0:
> >>>>> scripts/kconfig/menu.c: In function ‘get_symbol_str’:
> >>>>> scripts/kconfig/menu.c:586:18: warning: ‘jump’ may be used uninitialized in
> >>>>> this function [-Wmaybe-uninitialized]
[...]
> >>>>
> >>>> I do not see this warning with gcc 4.7.2 or 4.8.1. Which version are you
> >>>> using?
> >>>>
[...]
> > 
> > No, I was not able to reproduce it with either gcc-4.4.7, gcc-4.6.4 or
> > gcc-4.7.3 on my Ubuntu 13.04. Neither gcc-4.5 nor 4.8 are packaged, so
> > I could not test them.
> > 
> > Anyway, -Wmaybe-uninitialized is new with 4.7. So, the warning can't
> > happen with gcc < 4.7.
> > 
> > Madhavan, what gcc version are you using?
> gcc i have is gcc version 4.8.0 20130526

In that case, I don't have a strong opinion whether the patch should be
merged or not, but if it is merged, I think a comment should be added to
the code or the changelog stating that this is to work around a
deficiency observed in gcc 4.8.0.
--
To unsubscribe from this list: send the line "unsubscribe linux-kbuild" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Yann E. MORIN Sept. 20, 2013, 7:02 p.m. UTC | #7
Benjamin, Madhavan, All,

On 2013-09-20 13:58 -0400, Benjamin Poirier spake thusly:
> On 2013/09/20 08:05, Madhavan Srinivasan wrote:
> > On Friday 20 September 2013 01:34 AM, Yann E. MORIN wrote:
> > > Benjamin, All,
> > > 
> > > On 2013-09-19 15:13 -0400, Benjamin Poirier spake thusly:
> > >> On 2013/09/19 19:27, Yann E. MORIN wrote:
> > >>> Benjamin, Madhavan, All,
> > >>>
> > >>> On 2013-09-19 11:22 -0400, Benjamin Poirier spake thusly:
> > >>>> On 2013/09/19 12:58, Madhavan Srinivasan wrote:
> > >>>>> In file included from scripts/kconfig/zconf.tab.c:2537:0:
> > >>>>> scripts/kconfig/menu.c: In function ‘get_symbol_str’:
> > >>>>> scripts/kconfig/menu.c:586:18: warning: ‘jump’ may be used uninitialized in
> > >>>>> this function [-Wmaybe-uninitialized]
> [...]
> > >>>>
> > >>>> I do not see this warning with gcc 4.7.2 or 4.8.1. Which version are you
> > >>>> using?
> > >>>>
> [...]
> > > 
> > > No, I was not able to reproduce it with either gcc-4.4.7, gcc-4.6.4 or
> > > gcc-4.7.3 on my Ubuntu 13.04. Neither gcc-4.5 nor 4.8 are packaged, so
> > > I could not test them.
> > > 
> > > Anyway, -Wmaybe-uninitialized is new with 4.7. So, the warning can't
> > > happen with gcc < 4.7.
> > > 
> > > Madhavan, what gcc version are you using?
> > gcc i have is gcc version 4.8.0 20130526
> 
> In that case, I don't have a strong opinion whether the patch should be
> merged or not, but if it is merged, I think a comment should be added to
> the code or the changelog stating that this is to work around a
> deficiency observed in gcc 4.8.0.

If it is /fixed/ in gcc 4.8.1 (as you imply since you tested it), I
don't see a reason to apply this patch, in the end.

Rather, I would just put a comment in the code stating this is a
spurious warning, and keep the code as-is.

Madhavan, care to resend your patch with such a comment instead of
setting jump=NULL, please?

Regards,
Yann E. MORIN.
diff mbox

Patch

diff --git a/scripts/kconfig/menu.c b/scripts/kconfig/menu.c
index c1d5320..23b1827 100644
--- a/scripts/kconfig/menu.c
+++ b/scripts/kconfig/menu.c
@@ -544,7 +544,7 @@  static void get_prompt_str(struct gstr *r, struct property *prop,
 {
 	int i, j;
 	struct menu *submenu[8], *menu, *location = NULL;
-	struct jump_key *jump;
+	struct jump_key *jump = NULL;

 	str_printf(r, _("Prompt: %s\n"), _(prop->text));
 	menu = prop->menu->parent;