NOAA-EMC / NCEPLIBS-bacio

This library performs binary I/O for the NCEP models.
Other
2 stars 6 forks source link

memory leak when bad filename is passed to bacio() in bacio.c #62

Closed edwardhartnett closed 2 years ago

edwardhartnett commented 2 years ago

This test (currently commented out) in test_bacio_c.c causes a memory leak:

        /* Try to reopen the file with a bad name - won't work. */
        /* This currently causes a memory leak. See: 
        /* mode = BAOPEN_RONLY; */
        /* if ((ierr = bacio_(&mode, &start, &newpos, &size, &no, &nactual, */
        /*                    &fdes, bad_fname, datary, bad_namelen, datanamelen)) != 252) */
        /*     return ierr; */

The leak looks like:

Direct leak of 22 byte(s) in 1 object(s) allocated from:
    #0 0x7f79a2f58c47 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
    #1 0x55d11ff3481a in bacio_ /home/ed/NCEPLIBS-bacio/src/bacio.c:199
    #2 0x55d11ff3236c in main /home/ed/NCEPLIBS-bacio/tests/test_bacio_c.c:81
    #3 0x7f79a288c564 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x28564)

The problem is that the code does not free the malloced variable realname before returning on line 253:

  if (*fdes < 0) {
    #ifdef VERBOSE
      printf("error in file descriptor! *fdes %d\n", *fdes);
    #endif
    return 252;
  }