Skip to content

[th/leak-test-data] fix valgrind leak with nmtst_add_test_func()

Thomas Haller requested to merge th/leak-test-data into main

nmtst_add_test_func() calls g_test_add_data_func_full() with an allocated test data. However, if you only run a subset of the tests, then the free function is never called, and valrind reports a leak:

 $ ./tools/run-nm-test.sh -m src/core/platform/tests/test-route-linux -v
# no leak

$ ./tools/run-nm-test.sh -m src/core/platform/tests/test-route-linux -v -p /route/ip4
# many leaks:
==1662102== 107 (96 direct, 11 indirect) bytes in 1 blocks are definitely lost in loss record 388 of 448
==1662102==    at 0x4848464: calloc (vg_replace_malloc.c:1340)
==1662102==    by 0x4F615F0: g_malloc0 (gmem.c:163)
==1662102==    by 0x1621A6: _nmtst_add_test_func_full (nm-test-utils.h:918)
==1662102==    by 0x1623EB: _nmtstp_setup_tests (test-route.c:2179)
==1662102==    by 0x16E53D: main (test-common.c:2693)
==1662102==
{
   <insert_a_suppression_name_here>
   Memcheck:Leak
   match-leak-kinds: definite
   fun:calloc
   fun:g_malloc0
   fun:_nmtst_add_test_func_full
   fun:_nmtstp_setup_tests
   fun:main
}

Fix that.

Also introduce (and use) a general mechanism _nmtst_testdata_track_add() so that test data can be released at the end of the test.

Merge request reports