Bug 97608 - -Wanalyzer-malloc-leak false positive when returning p+1 instead of p
Summary: -Wanalyzer-malloc-leak false positive when returning p+1 instead of p
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: analyzer (show other bugs)
Version: 11.0
: P3 normal
Target Milestone: ---
Assignee: David Malcolm
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-10-28 02:02 UTC by Vincent Lefèvre
Modified: 2020-10-29 09:22 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2020-10-28 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Vincent Lefèvre 2020-10-28 02:02:04 UTC
On the following program tst.c

#include <stdlib.h>

void *f (void)
{
  void *p = malloc (8);
  if (p == NULL)
    abort ();
  return (void *) ((char *) p + 0);
}

void *g (void)
{
  void *p = malloc (8);
  if (p == NULL)
    abort ();
  return (void *) ((char *) p + 1);
}

I get:

cventin:~> gcc -c -fanalyzer tst.c
tst.c: In function ‘g’:
tst.c:16:10: warning: leak of ‘p’ [CWE-401] [-Wanalyzer-malloc-leak]
   16 |   return (void *) ((char *) p + 1);
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~
  ‘g’: events 1-5
    |
    |   13 |   void *p = malloc (8);
    |      |             ^~~~~~~~~~
    |      |             |
    |      |             (1) allocated here
    |   14 |   if (p == NULL)
    |      |      ~       
    |      |      |
    |      |      (2) assuming ‘p’ is non-NULL
    |      |      (3) following ‘false’ branch (when ‘p’ is non-NULL)...
    |   15 |     abort ();
    |   16 |   return (void *) ((char *) p + 1);
    |      |          ~~~~~~~~~~~~~~~~~~~~~~~~~
    |      |          |
    |      |          (4) ...to here
    |      |          (5) ‘p’ leaks here; was allocated at (1)
    |

(MPFR has something similar in its talloc-cache.c test in order to test the behavior of memory allocators, implemented in this test program as wrappers around malloc).

Tested with gcc (GCC) 11.0.0 20201028 (experimental), based on commit c25d317cf7d4ea8df0402feb939ce286e5f42988.
Comment 1 David Malcolm 2020-10-28 19:31:57 UTC
Thanks for filing this bug.  Confirmed.  Am testing a fix.
Comment 2 CVS Commits 2020-10-29 00:11:15 UTC
The master branch has been updated by David Malcolm <dmalcolm@gcc.gnu.org>:

https://gcc.gnu.org/g:1a9af271275f4893e28c789c8f1964025694eda1

commit r11-4510-g1a9af271275f4893e28c789c8f1964025694eda1
Author: David Malcolm <dmalcolm@redhat.com>
Date:   Wed Oct 28 20:10:39 2020 -0400

    analyzer: fix false leak diagnostic on offsets from malloc [PR97608]
    
    gcc/analyzer/ChangeLog:
            PR analyzer/97608
            * region-model-reachability.cc (reachable_regions::handle_sval):
            Operands of reachable reversible operations are reachable.
    
    gcc/testsuite/ChangeLog:
            PR analyzer/97608
            * gcc.dg/analyzer/malloc-1.c (test_42d): New.
            * gcc.dg/analyzer/pr97608.c: New test.
Comment 3 David Malcolm 2020-10-29 00:20:13 UTC
Should be fixed by the above commit.
Comment 4 Vincent Lefèvre 2020-10-29 09:22:29 UTC
Thanks. There's no longer any issue when testing GNU MPFR.