[BUG] -Wuninitialized: initialize variable with itself

Alejandro Colomar alx.manpages@gmail.com
Sun Nov 13 18:34:45 GMT 2022


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/>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenPGP_signature
Type: application/pgp-signature
Size: 833 bytes
Desc: OpenPGP digital signature
URL: <https://gcc.gnu.org/pipermail/gcc/attachments/20221113/2c3fd1b2/attachment-0001.sig>


More information about the Gcc mailing list