Memory leak in libpfm4 using PAPI
Status: Beta
Brought to you by:
seranian
C program: main.c
#include <papi.h>
int main(int argv, char **argc) {
PAPI_library_init(PAPI_VER_CURRENT);
PAPI_shutdown();
}
Compiling the program with
gcc -fsanitize=address main.c -o main
and running the program causes the following error,
=================================================================
==8743==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 4443 byte(s) in 47 object(s) allocated from:
#0 0x5596b4e37c91 in malloc (papi_memory+0xe0c91)
#1 0x7f80d3321f36 in pfmlib_build_fstr ./papi/src/libpfm4/lib/pfmlib_common.c:867:10
#2 0x7f80d33283d8 in pfmlib_perf_event_encode ./papi/src/libpfm4/lib/pfmlib_perf_event.c:333:8
#3 0x7f80d33238cc in pfm_get_os_event_encoding ./papi/src/libpfm4/lib/pfmlib_common.c:1614:9
#4 0x7f80d46ad121 in allocate_native_event ./papi/src/components/perf_event/pe_libpfm4_events.c:193:8
SUMMARY: AddressSanitizer: 4443 byte(s) leaked in 47 allocation(s).
The memory leak is fixed by freeing a dynamically allocated string in file pfmlib_perf_event.c
@@ -337,8 +337,10 @@ pfmlib_perf_event_encode(void *this, const char *str, int dfl_plm, void *data)
}
ret = pfmlib_build_fstr(&e, arg.fstr);
- if (ret == PFM_SUCCESS)
+ if (ret == PFM_SUCCESS) {
memcpy(uarg, &arg, sz);
+ free(*arg.fstr);
+ }
Similar change possibly required in pfmlib_common.c: pfmlib_raw_pmu_encode: 2399
error_fstr:
if (ret != PFM_SUCCESS)
free(arg.fstr);