www.digitalmars.com         C & C++   DMDScript  

D.gnu - GCC analyzer false positive

reply Zachary Yedidia <zyedidia gmail.com> writes:
The following code generates a warning when using GCC analyzer, 
but I don't think the warning is valid.

```
struct Guard {
     ~this() {}
}
Guard lock() {
     return Guard();
}
void bar() {
     auto foo = lock();
}
```

Compiled with GCC version 13.1:

```
$ gdc -fanalyzer test.d -O2 -c
test.d: In function 'lock':
test.d:5:5: warning: use of uninitialized value 'MEM[(unsigned 
char * {ref-all})&D.3438]' [CWE-457] 
[-Wanalyzer-use-of-uninitialized-value]
     5 |     return Guard();
       |     ^
   'lock': events 1-2
     |
     |    5 |     return Guard();
     |      |     ^
     |      |     |
     |      |     (1) region created on stack here
     |      |     (2) use of uninitialized value 'MEM[(unsigned 
char * {ref-all})&D.3438]' here
     |
```

Any ideas? Thanks!
Aug 31 2023
next sibling parent Iain Buclaw <ibuclaw gdcproject.org> writes:
On Thursday, 31 August 2023 at 13:53:55 UTC, Zachary Yedidia 
wrote:
 The following code generates a warning when using GCC analyzer, 
 but I don't think the warning is valid.

 ```
 struct Guard {
     ~this() {}
 }
 Guard lock() {
     return Guard();
 }
 void bar() {
     auto foo = lock();
 }
 ```

 Compiled with GCC version 13.1:

 ```
 $ gdc -fanalyzer test.d -O2 -c
 test.d: In function 'lock':
 test.d:5:5: warning: use of uninitialized value 'MEM[(unsigned 
 char * {ref-all})&D.3438]' [CWE-457] 
 [-Wanalyzer-use-of-uninitialized-value]
     5 |     return Guard();
       |     ^
   'lock': events 1-2
     |
     |    5 |     return Guard();
     |      |     ^
     |      |     |
     |      |     (1) region created on stack here
     |      |     (2) use of uninitialized value 'MEM[(unsigned 
 char * {ref-all})&D.3438]' here
     |
 ```

 Any ideas? Thanks!
Hi, Thanks for posting this! Had a quick poke around - also with a C++ equivalent test - and it looks like GCC's static analyzer is not good at dealing with returning non-trivially copyable struct via NRVO. I'll have a prod a bit more later, but my first instinct would be to raise a bug report with GCC.
Sep 01 2023
prev sibling parent Iain Buclaw <ibuclaw gdcproject.org> writes:
On Thursday, 31 August 2023 at 13:53:55 UTC, Zachary Yedidia 
wrote:
 The following code generates a warning when using GCC analyzer, 
 but I don't think the warning is valid.

 ```
 struct Guard {
     ~this() {}
 }
 Guard lock() {
     return Guard();
 }
 void bar() {
     auto foo = lock();
 }
 ```

 Compiled with GCC version 13.1:
I'm at the GNU Cauldron, and David has just said "don't use -fanalyzer on C++ in GCC 13, you will get rubbish out of it". This would also extend to all other languages that aren't C too. The good news is that things are going to be improved in GCC 14.
Sep 22 2023