public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* [Static Analyzer] Loop handling - False positive for malloc-sm
@ 2023-03-20 12:28 Pierrick Philippe
  2023-03-20 23:30 ` David Malcolm
  0 siblings, 1 reply; 8+ messages in thread
From: Pierrick Philippe @ 2023-03-20 12:28 UTC (permalink / raw)
  To: gcc

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

Hi everyone,

I'm still playing around with the analyzer, and wanted to have a look at 
loop handling.
I'm using a build from /trunk/ branch (/20230309/).

Here is my analyzed code:

'''
1| #include <stdlib.h>
2| int main(void) {
3|    void * ptr = malloc(sizeof(int));
4|    for (int i = 0; i < 10; i++) {
5|        if (i == 5) free(ptr);
6|    }
7|}
'''

And here, the malloc-sm is reporting a double-free on line 5 with a 
quite confusing output:

'''
./test.c: In function ‘main’:
./test.c:5:21: warning: double-‘free’ of ‘ptr’ [CWE-415] 
[-Wanalyzer-double-free]
     5 |         if (i == 5) free(ptr);
        |                         ^~~~~~~~~
   ‘main’: events 1-13
     |
     |   3 |     void * ptr = malloc(sizeof(int));
     |      |                        ^~~~~~~~~~~~~~~~~~~
     |      |                        |
     |      |                        (1) allocated here
     |   4 |     for (int i = 0; i < 10; i++) {
     |      |                         ~~~~  ~~~
     |      |                         |            |
     |      |                         |            (5) ...to here
     |      |                         (2) following ‘true’ branch (when 
‘i <= 9’)...
     |      |                         (6) following ‘true’ branch (when 
‘i <= 9’)...
     |      |                         (9) following ‘true’ branch (when 
‘i <= 9’)...
     |   5 |         if (i == 5) free(ptr);
     |      |            ~           ~~~~~
     |      |            |             |
     |      |            |             (8) first ‘free’ here
     |      |            |             (12) ...to here
     |      |            |             (13) second ‘free’ here; first 
‘free’ was at (8)
     |      |            (3) ...to here
     |      |            (4) following ‘false’ branch (when ‘i != 5’)...
     |      |            (7) ...to here
     |      |            (10) ...to here
     |      |            (11) following ‘true’ branch (when ‘i == 5’)...
     |
'''

So, I'm guessing that this false positive is due to how the analyzer is 
handling loops.
Which lead to my question: how are loops handled by the analyzer?

Thanks for your time,

Pierrick

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

end of thread, other threads:[~2023-03-23  8:06 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-20 12:28 [Static Analyzer] Loop handling - False positive for malloc-sm Pierrick Philippe
2023-03-20 23:30 ` David Malcolm
2023-03-21  8:21   ` Pierrick Philippe
2023-03-22 18:19     ` David Malcolm
2023-03-23  8:06       ` Pierrick Philippe
2023-03-21 10:01   ` Shengyu Huang
2023-03-22 18:34     ` David Malcolm
2023-03-21 10:12   ` Shengyu Huang

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).