Menu

False Positive (doubleFree) after correcting common realloc error (memleakOnRealloc)

2020-02-06
2020-02-07
  • Absinthe Sautereau

    This correctly generates [memleakOnRealloc]

    int main (void) {
         char * x = malloc(100UL);
            x = realloc(x, 10000UL);
            ...
    

    However, when corrected to this:

    int main (void) {
      char * x = malloc(100UL);
      char * tmp = realloc(x, 10000UL);
      if (NULL == tmp) {
          free(x);
          return 1;
      } 
      x = tmp;
    ...
    

    It incorrectly generates [doubleFree]

    $ cppcheck --version
    Cppcheck 1.89
    $ uname -a
    Linux qipk2dyy8 5.2.0-0.bpo.2-amd64 #1 SMP Debian 5.2.9-2~bpo10+1 (2019-08-25) x86_64 GNU/Linux

     
  • Jean Pierre LeJacq

    I confirm the same with the latest production version:
    $ cppcheck --version
    Cppcheck 1.90
    $ uname -a
    Linux qipf14k7jn 5.4.0-0.bpo.2-amd64 #1 SMP Debian 5.4.8-1~bpo10+1 (2020-01-07) x86_64 GNU/Linux

     
  • versat

    versat - 2020-02-07

    Thanks for reporting this issue.
    It looks like there is already a ticket in the bug tracker: https://trac.cppcheck.net/ticket/9437 (FP: Double free on realloc check)

     

Log in to post a comment.