From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 46672385741D; Mon, 9 May 2022 10:12:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 46672385741D From: "dilyan.palauzov at aegee dot org" To: gcc-bugs@gcc.gnu.org Subject: [Bug analyzer/105530] New: Analyzer makes wrong conclusions for the malloc (deallocator, ptr-index) attribute Date: Mon, 09 May 2022 10:12:35 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: analyzer X-Bugzilla-Version: 11.3.1 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: dilyan.palauzov at aegee dot org X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: dmalcolm at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 09 May 2022 10:12:35 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D105530 Bug ID: 105530 Summary: Analyzer makes wrong conclusions for the malloc (deallocator, ptr-index) attribute Product: gcc Version: 11.3.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: analyzer Assignee: dmalcolm at gcc dot gnu.org Reporter: dilyan.palauzov at aegee dot org Target Milestone: --- gcc (GCC) 11.2.1 20220413 (compiled from git) For a.c: #include void fr(char* x) { free(x); } __attribute__((malloc, malloc(fr, 1))) char * al() { return (char*) malloc(10); } int main() { fr(al()); return 0; } $ gcc a.c -fanalyzer prints: a.c: In function =E2=80=98al=E2=80=99:=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20 a.c:8:10: warning: leak of =E2=80=98al()=E2=80=99 [CWE-401] [-Wanalyzer-mal= loc-leak] 8 | return (char*) malloc(10); | ^~~~~~~~~~~~~~~~~~ =E2=80=98main=E2=80=99: events 1-3 | | 11 | int main() { | | ^~~~ | | | | | (1) entry to =E2=80=98main=E2=80=99 | 12 | fr(al()); | | ~~~~~~~~ | | | | | (2) allocated here | | (3) calling =E2=80=98al=E2=80=99 from =E2=80=98main=E2=80=99 | +--> =E2=80=98al=E2=80=99: events 4-5 | | 7 | __attribute__((malloc, malloc(fr, 1))) char * al() { | | ^~ | | | | | (4) entry= to =E2=80=98al=E2=80=99 | 8 | return (char*) malloc(10); | | ~~~~~~~~~~~~~~~~~~=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20 | | | | | (5) =E2=80=98al()=E2=80=99 leaks here; was all= ocated at (2) | a.c: In function =E2=80=98main=E2=80=99: a.c:12:3: warning: =E2=80=98al()=E2=80=99 should have been deallocated with= =E2=80=98free=E2=80=99 but was deallocated with =E2=80=98fr=E2=80=99 [CWE-762] [-Wanalyzer-mismatchi[4/196= 3] ocation] 12 | fr(al()); | ^~~~~~~~ =E2=80=98main=E2=80=99: events 1-2 | | 11 | int main() { | | ^~~~ | | | | | (1) entry to =E2=80=98main=E2=80=99 | 12 | fr(al()); | | ~~~~~~~~ | | | | | (2) calling =E2=80=98al=E2=80=99 from =E2=80=98main=E2=80=99 | +--> =E2=80=98al=E2=80=99: events 3-4 | | 7 | __attribute__((malloc, malloc(fr, 1))) char * al() { | | ^~ | | | | | (3) entry= to =E2=80=98al=E2=80=99 | 8 | return (char*) malloc(10); | | ~~~~~~~~~~=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20 | | | | | (4) allocated here (expects deallocati= on with =E2=80=98free=E2=80=99) | <------+ | =E2=80=98main=E2=80=99: events 5-6 | | 12 | fr(al()); | | ^~~~~~~~ | | | | | (5) returning to =E2=80=98main=E2=80=99 from =E2=80=98al=E2= =80=99 | | (6) deallocated with =E2=80=98fr=E2=80=99 here; allocation a= t (4) expects deallocation with =E2=80=98free=E2=80=99 | Now, I do not understand these warnings. First of all, nothing is leaked. > a.c:12:3: warning: =E2=80=98al()=E2=80=99 should have been deallocated wi= th =E2=80=98free=E2=80=99 but was deallocated with =E2=80=98fr=E2=80=99 [CW= E-762] [-Wanalyzer-mismatchi[4/1963] The complier/analyzer is explicitly told by the malloc(fr, 1) attribute, th= at fr() is the right way to deallocate the result of al(). > a.c:8:10: warning: leak of =E2=80=98al()=E2=80=99 [CWE-401] [-Wanalyzer-m= alloc-leak] return (char*) malloc(10); is by itself not a memory leak.=