Menu

False Positive? memleak

Chris Rice
2022-08-06
2022-08-07
  • Chris Rice

    Chris Rice - 2022-08-06

    CppCheck is calling this "Memory leak: ptr" on the line marked "****" below. The purpose of the function is to create and object and return it, so it's okay that it doesn't delete the object that is created. Is that all that CppCheck is reacting to? Weird then that it was on that line and not the others. Or is there something else it is saying?

    Thank you.

    Dir* Fpopendir( const char* DirName )
    {
      Dir* result = NULL;
      int FD = 0;
      Stat st;
      Dir* ptr = NULL;
      if ( fpstat( (char*) DirName, st ) < 0 )
        return result;
    /* is it A Dir ? */
      if ( ! ( ( ( st.st_mode & 0xF000 ) ) == 0x4000 ) )
      {
        errno = ESysENOTDIR;
        return result;
      }
    /* Open it*/
      FD = fpopen( DirName, O_RDONLY, 438 );
      if ( FD < 0 )
        return result;
      ptr = new Dir;
      if ( ptr == NULL )
        return result;
      ptr->dd_buf = new dirent;
      if ( ptr->dd_buf == NULL ) 
        return result;                 // <-- **** memleak here??
      ptr->dd_fd = FD;
      ptr->dd_loc = 0;
      ptr->dd_size = 0;
      ptr->dd_nextoff = 0;
      ptr->dd_max = sizeof( *ptr->dd_buf );
      return ptr;
    }
    
     

    Last edit: Chris Rice 2022-08-06
  • Daniel Marjamäki

    It looks like a memory leak to me. The result that you return is NULL right? And ptr is not NULL.

     

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.