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 <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2016-09-28 15:15:15 -04:00
parent 0407486316
commit 99faa0cf39
1 changed files with 8 additions and 2 deletions

View File

@ -7,10 +7,16 @@
#include <glib.h>
/*
* 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 */