Menu

Possible false positives: doubleFree and deallocret

figaro_
2024-09-15
2024-09-16
  •  figaro_

    figaro_ - 2024-09-15

    This code generates two errors in particular:

    gsl_sum_levin_u_workspace *gsl_sum_levin_u_alloc (size_t n) {
      gsl_sum_levin_u_workspace *w;
      w = (gsl_sum_levin_u_workspace *) malloc (sizeof (gsl_sum_levin_u_workspace));
    
      w->q_num = (double *) malloc (n * sizeof (double));
    
      if (w->q_num == NULL) {
        free (w);                   /* constructor error, prevent memory leak */
        GSL_ERROR_VAL ("failed to allocate space for q_num", GSL_ENOMEM, 0);
      }
    
      w->q_den = (double *) malloc (n * sizeof (double));
    
      if (w->q_den == NULL) {
        free (w->q_num);
        free (w);                   /* constructor error, prevent memory leak */
        GSL_ERROR_VAL ("failed to allocate space for q_den", GSL_ENOMEM, 0);
      }
    
      return w;
    }
    

    Using version 2.7 on Ubuntu 22.04 with the command: cppcheck --library=gnu work_u_.c

    work_u_.c:22:5: error: Memory pointed to by 'w' is freed twice. [doubleFree]
        free (w);                   /* constructor error, prevent memory leak */
        ^
    work_u_.c:14:5: note: Memory pointed to by 'w' is freed twice.
        free (w);                   /* constructor error, prevent memory leak */
        ^
    work_u_.c:22:5: note: Memory pointed to by 'w' is freed twice.
        free (w);                   /* constructor error, prevent memory leak */
        ^
    work_u_.c:26:3: error: Returning/dereferencing 'w' after it is deallocated / released [deallocret]
      return w;
      ^
    work_u_.c:14:5: note: Returning/dereferencing 'w' after it is deallocated / released
        free (w);                   /* constructor error, prevent memory leak */
        ^
    work_u_.c:26:3: note: Returning/dereferencing 'w' after it is deallocated / released
      return w;
      ^
    

    Error messages are probably similar in origin and strictly speaking they are false positives, but could accept pushing them to info level if there is a method to do that.

    Origin: https://git.savannah.gnu.org/cgit/gsl.git

     
  • CHR

    CHR - 2024-09-16

    Seems like GSL_ERROR_VAL is missing in gnu.cfg, so cppcheck doesn't know that it returns from the function.

     
  •  figaro_

    figaro_ - 2024-09-16

    Thanks, replaced the GSL_ERROR_VAL lines with exit(GSL_ENOMEM) for now and that works.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.