public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* [BUG] -Wuninitialized: initialize variable with itself
@ 2022-11-13 18:34 Alejandro Colomar
  2022-11-13 18:40 ` Andrew Pinski
  0 siblings, 1 reply; 10+ messages in thread
From: Alejandro Colomar @ 2022-11-13 18:34 UTC (permalink / raw)
  To: gcc; +Cc: Martin Uecker, Joseph Myers


[-- Attachment #1.1: Type: text/plain, Size: 1522 bytes --]

Hi,

While discussing some idea for a new feature, I tested the following example 
program:


     int main(void)
     {
         int i = i;
         return i;
     }


It seems obvious that it should give a warning, and in Clang it does:


     $ clang --version | head -n1
     Debian clang version 14.0.6

     $ clang -Wall -Wextra foo.c
     foo.c:3:10: warning: variable 'i' is uninitialized when used within its own 
initialization [-Wuninitialized]
             int i = i;
                 ~   ^
     1 warning generated.


But for GCC it looks fine:

     $ gcc --version | head -n1
     gcc (Debian 12.2.0-9) 12.2.0

     $ gcc -Wall -Wextra foo.c
     $


Until you enable the analyzer, which catches the uninitialized use:


     $ gcc -fanalyzer foo.c
     foo.c: In function ‘main’:
     foo.c:3:13: warning: use of uninitialized value ‘i’ [CWE-457] 
[-Wanalyzer-use-of-uninitialized-value]
         3 |         int i = i;
           |             ^
       ‘main’: events 1-2
         |
         |    3 |         int i = i;
         |      |             ^
         |      |             |
         |      |             (1) region created on stack here
         |      |             (2) use of uninitialized value ‘i’ here
         |



I expect that GCC should be able to detect this bug with a simple warning.  The 
analyzer is quite unreadable compared to normal warnings.

Cheers,
Alex

-- 
<http://www.alejandro-colomar.es/>

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

* Re: [BUG] -Wuninitialized: initialize variable with itself
  2022-11-13 18:34 [BUG] -Wuninitialized: initialize variable with itself Alejandro Colomar
@ 2022-11-13 18:40 ` Andrew Pinski
  2022-11-13 18:41   ` Andrew Pinski
  0 siblings, 1 reply; 10+ messages in thread
From: Andrew Pinski @ 2022-11-13 18:40 UTC (permalink / raw)
  To: Alejandro Colomar; +Cc: gcc, Martin Uecker, Joseph Myers

On Sun, Nov 13, 2022 at 10:36 AM Alejandro Colomar via Gcc
<gcc@gcc.gnu.org> wrote:
>
> Hi,
>
> While discussing some idea for a new feature, I tested the following example
> program:
>
>
>      int main(void)
>      {
>          int i = i;
>          return i;
>      }

This is NOT a bug but a documented way of having the warning not being there.
See https://gcc.gnu.org/onlinedocs/gcc-12.2.0/gcc/Warning-Options.html#index-Winit-self
https://gcc.gnu.org/onlinedocs/gcc-12.2.0/gcc/Warning-Options.html#index-Wuninitialized
"If you want to warn about code that uses the uninitialized value of
the variable in its own initializer, use the -Winit-self option."

Thanks,
Andrew Pinski

>
>
> It seems obvious that it should give a warning, and in Clang it does:
>
>
>      $ clang --version | head -n1
>      Debian clang version 14.0.6
>
>      $ clang -Wall -Wextra foo.c
>      foo.c:3:10: warning: variable 'i' is uninitialized when used within its own
> initialization [-Wuninitialized]
>              int i = i;
>                  ~   ^
>      1 warning generated.
>
>
> But for GCC it looks fine:
>
>      $ gcc --version | head -n1
>      gcc (Debian 12.2.0-9) 12.2.0
>
>      $ gcc -Wall -Wextra foo.c
>      $
>
>
> Until you enable the analyzer, which catches the uninitialized use:
>
>
>      $ gcc -fanalyzer foo.c
>      foo.c: In function ‘main’:
>      foo.c:3:13: warning: use of uninitialized value ‘i’ [CWE-457]
> [-Wanalyzer-use-of-uninitialized-value]
>          3 |         int i = i;
>            |             ^
>        ‘main’: events 1-2
>          |
>          |    3 |         int i = i;
>          |      |             ^
>          |      |             |
>          |      |             (1) region created on stack here
>          |      |             (2) use of uninitialized value ‘i’ here
>          |
>
>
>
> I expect that GCC should be able to detect this bug with a simple warning.  The
> analyzer is quite unreadable compared to normal warnings.
>
> Cheers,
> Alex
>
> --
> <http://www.alejandro-colomar.es/>

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

* Re: [BUG] -Wuninitialized: initialize variable with itself
  2022-11-13 18:40 ` Andrew Pinski
@ 2022-11-13 18:41   ` Andrew Pinski
  2022-11-13 18:43     ` Alejandro Colomar
  2022-11-13 18:45     ` Andrew Pinski
  0 siblings, 2 replies; 10+ messages in thread
From: Andrew Pinski @ 2022-11-13 18:41 UTC (permalink / raw)
  To: Alejandro Colomar; +Cc: gcc, Martin Uecker, Joseph Myers

On Sun, Nov 13, 2022 at 10:40 AM Andrew Pinski <pinskia@gmail.com> wrote:
>
> On Sun, Nov 13, 2022 at 10:36 AM Alejandro Colomar via Gcc
> <gcc@gcc.gnu.org> wrote:
> >
> > Hi,
> >
> > While discussing some idea for a new feature, I tested the following example
> > program:
> >
> >
> >      int main(void)
> >      {
> >          int i = i;
> >          return i;
> >      }
>
> This is NOT a bug but a documented way of having the warning not being there.
> See https://gcc.gnu.org/onlinedocs/gcc-12.2.0/gcc/Warning-Options.html#index-Winit-self
> https://gcc.gnu.org/onlinedocs/gcc-12.2.0/gcc/Warning-Options.html#index-Wuninitialized
> "If you want to warn about code that uses the uninitialized value of
> the variable in its own initializer, use the -Winit-self option."

I should note the main reason why I Know about this is because I fixed
this feature years ago (at least for C front-end)
and added the option to disable the feature.

>
> Thanks,
> Andrew Pinski
>
> >
> >
> > It seems obvious that it should give a warning, and in Clang it does:
> >
> >
> >      $ clang --version | head -n1
> >      Debian clang version 14.0.6
> >
> >      $ clang -Wall -Wextra foo.c
> >      foo.c:3:10: warning: variable 'i' is uninitialized when used within its own
> > initialization [-Wuninitialized]
> >              int i = i;
> >                  ~   ^
> >      1 warning generated.
> >
> >
> > But for GCC it looks fine:
> >
> >      $ gcc --version | head -n1
> >      gcc (Debian 12.2.0-9) 12.2.0
> >
> >      $ gcc -Wall -Wextra foo.c
> >      $
> >
> >
> > Until you enable the analyzer, which catches the uninitialized use:
> >
> >
> >      $ gcc -fanalyzer foo.c
> >      foo.c: In function ‘main’:
> >      foo.c:3:13: warning: use of uninitialized value ‘i’ [CWE-457]
> > [-Wanalyzer-use-of-uninitialized-value]
> >          3 |         int i = i;
> >            |             ^
> >        ‘main’: events 1-2
> >          |
> >          |    3 |         int i = i;
> >          |      |             ^
> >          |      |             |
> >          |      |             (1) region created on stack here
> >          |      |             (2) use of uninitialized value ‘i’ here
> >          |
> >
> >
> >
> > I expect that GCC should be able to detect this bug with a simple warning.  The
> > analyzer is quite unreadable compared to normal warnings.
> >
> > Cheers,
> > Alex
> >
> > --
> > <http://www.alejandro-colomar.es/>

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

* Re: [BUG] -Wuninitialized: initialize variable with itself
  2022-11-13 18:41   ` Andrew Pinski
@ 2022-11-13 18:43     ` Alejandro Colomar
  2022-11-14  9:41       ` David Brown
  2022-11-13 18:45     ` Andrew Pinski
  1 sibling, 1 reply; 10+ messages in thread
From: Alejandro Colomar @ 2022-11-13 18:43 UTC (permalink / raw)
  To: Andrew Pinski; +Cc: gcc, Martin Uecker, Joseph Myers


[-- Attachment #1.1: Type: text/plain, Size: 1229 bytes --]

Hi Andrew!

On 11/13/22 19:41, Andrew Pinski wrote:
> On Sun, Nov 13, 2022 at 10:40 AM Andrew Pinski <pinskia@gmail.com> wrote:
>>
>> On Sun, Nov 13, 2022 at 10:36 AM Alejandro Colomar via Gcc
>> <gcc@gcc.gnu.org> wrote:
>>>
>>> Hi,
>>>
>>> While discussing some idea for a new feature, I tested the following example
>>> program:
>>>
>>>
>>>       int main(void)
>>>       {
>>>           int i = i;
>>>           return i;
>>>       }
>>
>> This is NOT a bug but a documented way of having the warning not being there.
>> See https://gcc.gnu.org/onlinedocs/gcc-12.2.0/gcc/Warning-Options.html#index-Winit-self
>> https://gcc.gnu.org/onlinedocs/gcc-12.2.0/gcc/Warning-Options.html#index-Wuninitialized
>> "If you want to warn about code that uses the uninitialized value of
>> the variable in its own initializer, use the -Winit-self option."
> 
> I should note the main reason why I Know about this is because I fixed
> this feature years ago (at least for C front-end)
> and added the option to disable the feature.

I'm curious: what are the reasons why one would want to disable such a warning?
Why is it not in -Wall or -Wextra?

Thanks,

Alex

-- 
<http://www.alejandro-colomar.es/>

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

* Re: [BUG] -Wuninitialized: initialize variable with itself
  2022-11-13 18:41   ` Andrew Pinski
  2022-11-13 18:43     ` Alejandro Colomar
@ 2022-11-13 18:45     ` Andrew Pinski
  1 sibling, 0 replies; 10+ messages in thread
From: Andrew Pinski @ 2022-11-13 18:45 UTC (permalink / raw)
  To: Alejandro Colomar; +Cc: gcc, Martin Uecker, Joseph Myers

On Sun, Nov 13, 2022 at 10:41 AM Andrew Pinski <pinskia@gmail.com> wrote:
>
> On Sun, Nov 13, 2022 at 10:40 AM Andrew Pinski <pinskia@gmail.com> wrote:
> >
> > On Sun, Nov 13, 2022 at 10:36 AM Alejandro Colomar via Gcc
> > <gcc@gcc.gnu.org> wrote:
> > >
> > > Hi,
> > >
> > > While discussing some idea for a new feature, I tested the following example
> > > program:
> > >
> > >
> > >      int main(void)
> > >      {
> > >          int i = i;
> > >          return i;
> > >      }
> >
> > This is NOT a bug but a documented way of having the warning not being there.
> > See https://gcc.gnu.org/onlinedocs/gcc-12.2.0/gcc/Warning-Options.html#index-Winit-self
> > https://gcc.gnu.org/onlinedocs/gcc-12.2.0/gcc/Warning-Options.html#index-Wuninitialized
> > "If you want to warn about code that uses the uninitialized value of
> > the variable in its own initializer, use the -Winit-self option."
>
> I should note the main reason why I Know about this is because I fixed
> this feature years ago (at least for C front-end)
> and added the option to disable the feature.

When I says years, it was 19 years ago even, see
https://gcc.gnu.org/PR5582
and
https://gcc.gnu.org/PR10538
and
https://gcc.gnu.org/r0-52301-g3390f9c9bef0be

Thanks,


>
> >
> > Thanks,
> > Andrew Pinski
> >
> > >
> > >
> > > It seems obvious that it should give a warning, and in Clang it does:
> > >
> > >
> > >      $ clang --version | head -n1
> > >      Debian clang version 14.0.6
> > >
> > >      $ clang -Wall -Wextra foo.c
> > >      foo.c:3:10: warning: variable 'i' is uninitialized when used within its own
> > > initialization [-Wuninitialized]
> > >              int i = i;
> > >                  ~   ^
> > >      1 warning generated.
> > >
> > >
> > > But for GCC it looks fine:
> > >
> > >      $ gcc --version | head -n1
> > >      gcc (Debian 12.2.0-9) 12.2.0
> > >
> > >      $ gcc -Wall -Wextra foo.c
> > >      $
> > >
> > >
> > > Until you enable the analyzer, which catches the uninitialized use:
> > >
> > >
> > >      $ gcc -fanalyzer foo.c
> > >      foo.c: In function ‘main’:
> > >      foo.c:3:13: warning: use of uninitialized value ‘i’ [CWE-457]
> > > [-Wanalyzer-use-of-uninitialized-value]
> > >          3 |         int i = i;
> > >            |             ^
> > >        ‘main’: events 1-2
> > >          |
> > >          |    3 |         int i = i;
> > >          |      |             ^
> > >          |      |             |
> > >          |      |             (1) region created on stack here
> > >          |      |             (2) use of uninitialized value ‘i’ here
> > >          |
> > >
> > >
> > >
> > > I expect that GCC should be able to detect this bug with a simple warning.  The
> > > analyzer is quite unreadable compared to normal warnings.
> > >
> > > Cheers,
> > > Alex
> > >
> > > --
> > > <http://www.alejandro-colomar.es/>

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

* Re: [BUG] -Wuninitialized: initialize variable with itself
  2022-11-13 18:43     ` Alejandro Colomar
@ 2022-11-14  9:41       ` David Brown
  2022-11-14 11:30         ` Alejandro Colomar
  2022-11-14 15:10         ` NightStrike
  0 siblings, 2 replies; 10+ messages in thread
From: David Brown @ 2022-11-14  9:41 UTC (permalink / raw)
  To: Alejandro Colomar, Andrew Pinski; +Cc: gcc, Martin Uecker, Joseph Myers

On 13/11/2022 19:43, Alejandro Colomar via Gcc wrote:
> Hi Andrew!
> 
> On 11/13/22 19:41, Andrew Pinski wrote:
>> On Sun, Nov 13, 2022 at 10:40 AM Andrew Pinski <pinskia@gmail.com> wrote:
>>>
>>> On Sun, Nov 13, 2022 at 10:36 AM Alejandro Colomar via Gcc
>>> <gcc@gcc.gnu.org> wrote:
>>>>
>>>> Hi,
>>>>
>>>> While discussing some idea for a new feature, I tested the following 
>>>> example
>>>> program:
>>>>
>>>>
>>>>       int main(void)
>>>>       {
>>>>           int i = i;
>>>>           return i;
>>>>       }
>>>
>>> This is NOT a bug but a documented way of having the warning not 
>>> being there.
>>> See 
>>> https://gcc.gnu.org/onlinedocs/gcc-12.2.0/gcc/Warning-Options.html#index-Winit-self 
>>>
>>> https://gcc.gnu.org/onlinedocs/gcc-12.2.0/gcc/Warning-Options.html#index-Wuninitialized 
>>>
>>> "If you want to warn about code that uses the uninitialized value of
>>> the variable in its own initializer, use the -Winit-self option."
>>
>> I should note the main reason why I Know about this is because I fixed
>> this feature years ago (at least for C front-end)
>> and added the option to disable the feature.
> 
> I'm curious: what are the reasons why one would want to disable such a 
> warning?
> Why is it not in -Wall or -Wextra?
> 
> Thanks,
> 
> Alex
> 

Warnings are not perfect - there is always the risk of false positives 
and false negatives.  And different people will have different ideas 
about what code is perfectly reasonable, and what code is risky and 
should trigger a warning.  Thus gcc has warning flag groups (-Wall, 
-Wextra) that try to match common consensus, and individual flags for 
personal fine-tuning.

Sometimes it is useful to have a simple way to override a warning in 
code, without going through "#pragma GCC diagnostic" lines (which are 
powerful, but not pretty).

So if you have :

	int i;
	if (a == 1) i = 1;
	if (b == 1) i = 2;
	if (c == 1) i = 3;
	return i;

the compiler will warn that "i" may not be initialised.  But if you 
/know/ that one of the three conditions will match (or you don't care 
what "i" is if it does not match), then you know your code is fine and 
don't want the warning.  Writing "int i = i;" is a way of telling the 
compiler "I know what I am doing, even though this code looks dodgy, 
because I know more than you do".

It's just like writing "while ((*p++ = *q++));", or using a cast to void 
to turn off an "unused parameter" warning.

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

* Re: [BUG] -Wuninitialized: initialize variable with itself
  2022-11-14  9:41       ` David Brown
@ 2022-11-14 11:30         ` Alejandro Colomar
  2022-11-14 15:10         ` NightStrike
  1 sibling, 0 replies; 10+ messages in thread
From: Alejandro Colomar @ 2022-11-14 11:30 UTC (permalink / raw)
  To: David Brown, Andrew Pinski; +Cc: gcc, Martin Uecker, Joseph Myers


[-- Attachment #1.1: Type: text/plain, Size: 2981 bytes --]

Hi David,

On 11/14/22 10:41, David Brown wrote:
> On 13/11/2022 19:43, Alejandro Colomar via Gcc wrote:
>> Hi Andrew!
>>
>> On 11/13/22 19:41, Andrew Pinski wrote:
>>> On Sun, Nov 13, 2022 at 10:40 AM Andrew Pinski <pinskia@gmail.com> wrote:
>>>>
>>>> On Sun, Nov 13, 2022 at 10:36 AM Alejandro Colomar via Gcc
>>>> <gcc@gcc.gnu.org> wrote:
>>>>>
>>>>> Hi,
>>>>>
>>>>> While discussing some idea for a new feature, I tested the following example
>>>>> program:
>>>>>
>>>>>
>>>>>       int main(void)
>>>>>       {
>>>>>           int i = i;
>>>>>           return i;
>>>>>       }
>>>>
>>>> This is NOT a bug but a documented way of having the warning not being there.
>>>> See 
>>>> https://gcc.gnu.org/onlinedocs/gcc-12.2.0/gcc/Warning-Options.html#index-Winit-self
>>>> https://gcc.gnu.org/onlinedocs/gcc-12.2.0/gcc/Warning-Options.html#index-Wuninitialized
>>>> "If you want to warn about code that uses the uninitialized value of
>>>> the variable in its own initializer, use the -Winit-self option."
>>>
>>> I should note the main reason why I Know about this is because I fixed
>>> this feature years ago (at least for C front-end)
>>> and added the option to disable the feature.
>>
>> I'm curious: what are the reasons why one would want to disable such a warning?
>> Why is it not in -Wall or -Wextra?
>>
>> Thanks,
>>
>> Alex
>>
> 
> Warnings are not perfect - there is always the risk of false positives and false 
> negatives.  And different people will have different ideas about what code is 
> perfectly reasonable, and what code is risky and should trigger a warning.  Thus 
> gcc has warning flag groups (-Wall, -Wextra) that try to match common consensus, 
> and individual flags for personal fine-tuning.
> 
> Sometimes it is useful to have a simple way to override a warning in code, 
> without going through "#pragma GCC diagnostic" lines (which are powerful, but 
> not pretty).
> 
> So if you have :
> 
>      int i;
>      if (a == 1) i = 1;
>      if (b == 1) i = 2;
>      if (c == 1) i = 3;
>      return i;
> 
> the compiler will warn that "i" may not be initialised.  But if you /know/ that 
> one of the three conditions will match (or you don't care what "i" is if it does 
> not match), then you know your code is fine and don't want the warning.  Writing 
> "int i = i;" is a way of telling the compiler "I know what I am doing, even 
> though this code looks dodgy, because I know more than you do".

Ahh, that makes sense.  Since the default warnings warn about 'int i=i+1;' it 
makes sense to me.  Writing 'int i=i;' is just too stupid that can be considered 
a reasonable way to tell the compiler we know better.

Thanks!

Cheers,

Alex

> 
> It's just like writing "while ((*p++ = *q++));", or using a cast to void to turn 
> off an "unused parameter" warning.

-- 
<http://www.alejandro-colomar.es/>

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

* Re: [BUG] -Wuninitialized: initialize variable with itself
  2022-11-14  9:41       ` David Brown
  2022-11-14 11:30         ` Alejandro Colomar
@ 2022-11-14 15:10         ` NightStrike
  2022-11-14 15:49           ` David Brown
  1 sibling, 1 reply; 10+ messages in thread
From: NightStrike @ 2022-11-14 15:10 UTC (permalink / raw)
  To: David Brown
  Cc: Alejandro Colomar, Andrew Pinski, gcc, Martin Uecker, Joseph Myers

[-- Attachment #1: Type: text/plain, Size: 2842 bytes --]

On Mon, Nov 14, 2022, 04:42 David Brown via Gcc <gcc@gcc.gnu.org> wrote:

> On 13/11/2022 19:43, Alejandro Colomar via Gcc wrote:
> > Hi Andrew!
> >
> > On 11/13/22 19:41, Andrew Pinski wrote:
> >> On Sun, Nov 13, 2022 at 10:40 AM Andrew Pinski <pinskia@gmail.com>
> wrote:
> >>>
> >>> On Sun, Nov 13, 2022 at 10:36 AM Alejandro Colomar via Gcc
> >>> <gcc@gcc.gnu.org> wrote:
> >>>>
> >>>> Hi,
> >>>>
> >>>> While discussing some idea for a new feature, I tested the following
> >>>> example
> >>>> program:
> >>>>
> >>>>
> >>>>       int main(void)
> >>>>       {
> >>>>           int i = i;
> >>>>           return i;
> >>>>       }
> >>>
> >>> This is NOT a bug but a documented way of having the warning not
> >>> being there.
> >>> See
> >>>
> https://gcc.gnu.org/onlinedocs/gcc-12.2.0/gcc/Warning-Options.html#index-Winit-self
> >>>
> >>>
> https://gcc.gnu.org/onlinedocs/gcc-12.2.0/gcc/Warning-Options.html#index-Wuninitialized
> >>>
> >>> "If you want to warn about code that uses the uninitialized value of
> >>> the variable in its own initializer, use the -Winit-self option."
> >>
> >> I should note the main reason why I Know about this is because I fixed
> >> this feature years ago (at least for C front-end)
> >> and added the option to disable the feature.
> >
> > I'm curious: what are the reasons why one would want to disable such a
> > warning?
> > Why is it not in -Wall or -Wextra?
> >
> > Thanks,
> >
> > Alex
> >
>
> Warnings are not perfect - there is always the risk of false positives
> and false negatives.  And different people will have different ideas
> about what code is perfectly reasonable, and what code is risky and
> should trigger a warning.  Thus gcc has warning flag groups (-Wall,
> -Wextra) that try to match common consensus, and individual flags for
> personal fine-tuning.
>
> Sometimes it is useful to have a simple way to override a warning in
> code, without going through "#pragma GCC diagnostic" lines (which are
> powerful, but not pretty).
>
> So if you have :
>
>         int i;
>         if (a == 1) i = 1;
>         if (b == 1) i = 2;
>         if (c == 1) i = 3;
>         return i;
>
> the compiler will warn that "i" may not be initialised.  But if you
> /know/ that one of the three conditions will match (or you don't care
> what "i" is if it does not match), then you know your code is fine and
> don't want the warning.  Writing "int i = i;" is a way of telling the
> compiler "I know what I am doing, even though this code looks dodgy,
> because I know more than you do".
>
> It's just like writing "while ((*p++ = *q++));", or using a cast to void
> to turn off an "unused parameter" warning.
>

Wouldn't it be easier, faster, and more obvious to the reader to just use
"int i = 0"? I'm curious what a real world use case is where you can't do
the more common thing if =0.

>

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

* Re: [BUG] -Wuninitialized: initialize variable with itself
  2022-11-14 15:10         ` NightStrike
@ 2022-11-14 15:49           ` David Brown
  2022-11-14 17:43             ` NightStrike
  0 siblings, 1 reply; 10+ messages in thread
From: David Brown @ 2022-11-14 15:49 UTC (permalink / raw)
  To: NightStrike
  Cc: Alejandro Colomar, Andrew Pinski, gcc, Martin Uecker, Joseph Myers



On 14/11/2022 16:10, NightStrike wrote:
> 
> 
> On Mon, Nov 14, 2022, 04:42 David Brown via Gcc <gcc@gcc.gnu.org 

> 
>     Warnings are not perfect - there is always the risk of false positives
>     and false negatives.  And different people will have different ideas
>     about what code is perfectly reasonable, and what code is risky and
>     should trigger a warning.  Thus gcc has warning flag groups (-Wall,
>     -Wextra) that try to match common consensus, and individual flags for
>     personal fine-tuning.
> 
>     Sometimes it is useful to have a simple way to override a warning in
>     code, without going through "#pragma GCC diagnostic" lines (which are
>     powerful, but not pretty).
> 
>     So if you have :
> 
>              int i;
>              if (a == 1) i = 1;
>              if (b == 1) i = 2;
>              if (c == 1) i = 3;
>              return i;
> 
>     the compiler will warn that "i" may not be initialised.  But if you
>     /know/ that one of the three conditions will match (or you don't care
>     what "i" is if it does not match), then you know your code is fine and
>     don't want the warning.  Writing "int i = i;" is a way of telling the
>     compiler "I know what I am doing, even though this code looks dodgy,
>     because I know more than you do".
> 
>     It's just like writing "while ((*p++ = *q++));", or using a cast to
>     void
>     to turn off an "unused parameter" warning.
> 
> 
> Wouldn't it be easier, faster, and more obvious to the reader to just 
> use "int i = 0"? I'm curious what a real world use case is where you 
> can't do the more common thing if =0.
> 

You can write "int i = 0;" if you prefer.  I would not, because IMHO 
doing so would be wrong, unclear to the reader, less efficient, and 
harder to debug.

In the code above, the value returned should never be 0.  So why should 
"i" be set to 0 at any point?  That's just an extra instruction the 
compiler must generate (in my line of work, my code often needs to be 
efficient).  More importantly, perhaps, it means that if you use 
diagnostic tools such as sanitizers you are hiding bugs from them 
instead of catching them - a sanitizer could catch the case of "return 
i;" when "i" is not set.

(I don't know if current sanitizers will do that or not, and haven't 
tested it, but they /could/.)

But I'm quite happy with :

	int i = i;	// Self-initialise to silence warning

I don't think there is a "perfect" solution to cases like this, and 
opinions will always differ, but self-initialisation seems a good choice 
to me.  Regardless of the pros and cons in this particular example, the 
handling of self-initialisation warnings in gcc is, AFAIUI, to allow 
such code for those that want to use it.



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

* Re: [BUG] -Wuninitialized: initialize variable with itself
  2022-11-14 15:49           ` David Brown
@ 2022-11-14 17:43             ` NightStrike
  0 siblings, 0 replies; 10+ messages in thread
From: NightStrike @ 2022-11-14 17:43 UTC (permalink / raw)
  To: David Brown
  Cc: Alejandro Colomar, Andrew Pinski, gcc, Martin Uecker, Joseph Myers

[-- Attachment #1: Type: text/plain, Size: 2999 bytes --]

On Mon, Nov 14, 2022, 10:49 David Brown <david@westcontrol.com> wrote:

>
>
> On 14/11/2022 16:10, NightStrike wrote:
> >
> >
> > On Mon, Nov 14, 2022, 04:42 David Brown via Gcc <gcc@gcc.gnu.org
>
> >
> >     Warnings are not perfect - there is always the risk of false
> positives
> >     and false negatives.  And different people will have different ideas
> >     about what code is perfectly reasonable, and what code is risky and
> >     should trigger a warning.  Thus gcc has warning flag groups (-Wall,
> >     -Wextra) that try to match common consensus, and individual flags for
> >     personal fine-tuning.
> >
> >     Sometimes it is useful to have a simple way to override a warning in
> >     code, without going through "#pragma GCC diagnostic" lines (which are
> >     powerful, but not pretty).
> >
> >     So if you have :
> >
> >              int i;
> >              if (a == 1) i = 1;
> >              if (b == 1) i = 2;
> >              if (c == 1) i = 3;
> >              return i;
> >
> >     the compiler will warn that "i" may not be initialised.  But if you
> >     /know/ that one of the three conditions will match (or you don't care
> >     what "i" is if it does not match), then you know your code is fine
> and
> >     don't want the warning.  Writing "int i = i;" is a way of telling the
> >     compiler "I know what I am doing, even though this code looks dodgy,
> >     because I know more than you do".
> >
> >     It's just like writing "while ((*p++ = *q++));", or using a cast to
> >     void
> >     to turn off an "unused parameter" warning.
> >
> >
> > Wouldn't it be easier, faster, and more obvious to the reader to just
> > use "int i = 0"? I'm curious what a real world use case is where you
> > can't do the more common thing if =0.
> >
>
> You can write "int i = 0;" if you prefer.  I would not, because IMHO
> doing so would be wrong, unclear to the reader, less efficient, and
> harder to debug.
>
> In the code above, the value returned should never be 0.  So why should
> "i" be set to 0 at any point?  That's just an extra instruction the
> compiler must generate (in my line of work, my code often needs to be
> efficient).  More importantly, perhaps, it means that if you use
> diagnostic tools such as sanitizers you are hiding bugs from them
> instead of catching them - a sanitizer could catch the case of "return
> i;" when "i" is not set.
>
> (I don't know if current sanitizers will do that or not, and haven't
> tested it, but they /could/.)
>
> But I'm quite happy with :
>
>         int i = i;      // Self-initialise to silence warning
>
> I don't think there is a "perfect" solution to cases like this, and
> opinions will always differ, but self-initialisation seems a good choice
> to me.  Regardless of the pros and cons in this particular example, the
> handling of self-initialisation warnings in gcc is, AFAIUI, to allow
> such code for those that want to use it.


Thanks for the extended explanation, insight,  and detail!

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

end of thread, other threads:[~2022-11-14 17:43 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-13 18:34 [BUG] -Wuninitialized: initialize variable with itself Alejandro Colomar
2022-11-13 18:40 ` Andrew Pinski
2022-11-13 18:41   ` Andrew Pinski
2022-11-13 18:43     ` Alejandro Colomar
2022-11-14  9:41       ` David Brown
2022-11-14 11:30         ` Alejandro Colomar
2022-11-14 15:10         ` NightStrike
2022-11-14 15:49           ` David Brown
2022-11-14 17:43             ` NightStrike
2022-11-13 18:45     ` Andrew Pinski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).