From 99faa0cf39dd6cd382a03fd20a48b21829f3719d Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Wed, 28 Sep 2016 15:15:15 -0400 Subject: [PATCH] tests: g_assert_cmpstr_free() stores the lhs in a temporary variable Valgrind told me that all calls to g_assert_cmpstr_free() weren't actually freeing the string. A closer look shows that if we pass a function as "lhs" then the function will be called twice, allocating twice as much memory. Fix this by storing the result of lhs in a temporary variable so functions are only ever called once. Signed-off-by: Anna Schumaker --- include/tests/test.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/include/tests/test.h b/include/tests/test.h index 73675e9c..e1786f1a 100644 --- a/include/tests/test.h +++ b/include/tests/test.h @@ -7,10 +7,16 @@ #include +/* + * Compares two strings, and frees lhs before returning. + * NOTE: We store lhs in a temporary string so that "lhs" only gets + * evaluated once in the case that it is a function. + */ #define g_assert_cmpstr_free(lhs, cmp, rhs) \ do { \ - g_assert_cmpstr(lhs, cmp, rhs); \ - g_free(lhs); \ + gchar *__tmp_str = lhs; \ + g_assert_cmpstr(__tmp_str, cmp, rhs); \ + g_free(__tmp_str); \ } while (0) #endif /* OCARINA_TESTS_TEST_H */