mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* GCC fails to spot uninitialized variable
@ 2022-07-15 20:09 Petko Manolov
  2022-07-15 22:03 ` Nathan Chancellor
  0 siblings, 1 reply; 3+ messages in thread
From: Petko Manolov @ 2022-07-15 20:09 UTC (permalink / raw)
  To: linux-kernel

	Guys,

Today i was bitten by a stupid bug that i introduced myself while writing some
v4l2 code.  Looking at it a bit more carefully i was surprised that GCC didn't
catch this one, as it was something that should definitely emit a warning.

When included into the driver, this particular code:

int blah(int a, int *b)
{
	int ret;

	switch (a) {
	case 0:
		ret = a;
		break;
	case 1:
		ret = *b;
		break;
	case 2:
		*b = a;
		break;
	default:
		ret = 0;
	}

	return ret;
}

somehow managed to defeat GCC checks.  Compiling it as a standalone .c file
with:

	gcc -Wall -O2 -c t.c

gives me nice:

t.c:19:16: warning: 'ret' may be used uninitialized in this function [-Wmaybe-uninitialized]
   19 |         return ret;
      |                ^~~

Any idea what might have gone wrong?


cheers,
Petko

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: GCC fails to spot uninitialized variable
  2022-07-15 20:09 GCC fails to spot uninitialized variable Petko Manolov
@ 2022-07-15 22:03 ` Nathan Chancellor
  2022-07-16  6:25   ` Petko Manolov
  0 siblings, 1 reply; 3+ messages in thread
From: Nathan Chancellor @ 2022-07-15 22:03 UTC (permalink / raw)
  To: Petko Manolov; +Cc: linux-kernel

On Fri, Jul 15, 2022 at 11:09:17PM +0300, Petko Manolov wrote:
> 	Guys,
> 
> Today i was bitten by a stupid bug that i introduced myself while writing some
> v4l2 code.  Looking at it a bit more carefully i was surprised that GCC didn't
> catch this one, as it was something that should definitely emit a warning.
> 
> When included into the driver, this particular code:
> 
> int blah(int a, int *b)
> {
> 	int ret;
> 
> 	switch (a) {
> 	case 0:
> 		ret = a;
> 		break;
> 	case 1:
> 		ret = *b;
> 		break;
> 	case 2:
> 		*b = a;
> 		break;
> 	default:
> 		ret = 0;
> 	}
> 
> 	return ret;
> }
> 
> somehow managed to defeat GCC checks.  Compiling it as a standalone .c file
> with:
> 
> 	gcc -Wall -O2 -c t.c
> 
> gives me nice:
> 
> t.c:19:16: warning: 'ret' may be used uninitialized in this function [-Wmaybe-uninitialized]
>    19 |         return ret;
>       |                ^~~
> 
> Any idea what might have gone wrong?

See commit 78a5255ffb6a ("Stop the ad-hoc games with
-Wno-maybe-initialized") in 5.7, which disabled that warning for a
default kernel build. You have to either include 'W=2' (which will
introduce other warnings which might be noisy) or
'KCFLAGS=-Wmaybe-uninitialized' (which will just add that warning) in
your make command to see those warnings.

As an aside, your mailer adds a "Mail-Followup-To:" header that was set
to LKML, meaning that you would not have seen this reply unless you were
subscribed to LKML. Might be something worth looking into.

Cheers,
Nathan

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: GCC fails to spot uninitialized variable
  2022-07-15 22:03 ` Nathan Chancellor
@ 2022-07-16  6:25   ` Petko Manolov
  0 siblings, 0 replies; 3+ messages in thread
From: Petko Manolov @ 2022-07-16  6:25 UTC (permalink / raw)
  To: Nathan Chancellor; +Cc: linux-kernel

On 22-07-15 15:03:37, Nathan Chancellor wrote:
> On Fri, Jul 15, 2022 at 11:09:17PM +0300, Petko Manolov wrote:
> > 	Guys,
> > 
> > Today i was bitten by a stupid bug that i introduced myself while writing
> > some v4l2 code.  Looking at it a bit more carefully i was surprised that GCC
> > didn't catch this one, as it was something that should definitely emit a
> > warning.
> > 
> > When included into the driver, this particular code:
> > 
> > int blah(int a, int *b)
> > {
> > 	int ret;
> > 
> > 	switch (a) {
> > 	case 0:
> > 		ret = a;
> > 		break;
> > 	case 1:
> > 		ret = *b;
> > 		break;
> > 	case 2:
> > 		*b = a;
> > 		break;
> > 	default:
> > 		ret = 0;
> > 	}
> > 
> > 	return ret;
> > }
> > 
> > somehow managed to defeat GCC checks.  Compiling it as a standalone .c file
> > with:
> > 
> > 	gcc -Wall -O2 -c t.c
> > 
> > gives me nice:
> > 
> > t.c:19:16: warning: 'ret' may be used uninitialized in this function [-Wmaybe-uninitialized]
> >    19 |         return ret;
> >       |                ^~~
> > 
> > Any idea what might have gone wrong?
> 
> See commit 78a5255ffb6a ("Stop the ad-hoc games with -Wno-maybe-initialized")
> in 5.7, which disabled that warning for a default kernel build. You have to
> either include 'W=2' (which will introduce other warnings which might be
> noisy) or 'KCFLAGS=-Wmaybe-uninitialized' (which will just add that warning)
> in your make command to see those warnings.

I see.  I guess i'll end up enabling W=2 only for this particular driver and
only while in development.

> As an aside, your mailer adds a "Mail-Followup-To:" header that was set to
> LKML, meaning that you would not have seen this reply unless you were
> subscribed to LKML. Might be something worth looking into.

That would be "set followup_to=no" in mutt speak.  Thanks for looking into this.
I am subscribed to all list i'm replying to but, if i understand this properly,
with the old setup non-subscribers may not get my messages.


cheers,
Petko

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-07-16  6:26 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-15 20:09 GCC fails to spot uninitialized variable Petko Manolov
2022-07-15 22:03 ` Nathan Chancellor
2022-07-16  6:25   ` Petko Manolov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®