From: Niels de Vos Date: Thu, 26 Feb 2015 20:47:03 +0000 (+0100) Subject: cmocka: realloc(ptr, 0) should act as free(ptr) X-Git-Tag: cmocka-1.1.1~51 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=78191eaebfa2fc212ea4ade51e2b67c468e84834;p=platform%2Fupstream%2Fcmocka.git cmocka: realloc(ptr, 0) should act as free(ptr) Currently, realloc(ptr, 0) does not free the pointer as specified by 'man 3 realloc': The realloc() function changes the size of the memory block pointed to by ptr to size bytes. [...] if size is equal to zero, and ptr is not NULL, then the call is equivalent to free(ptr). [...] This causes a leak of the allocated memory, and tests that use this particular realloc() pattern fail. Signed-off-by: Niels de Vos Reviewed-by: Andreas Schneider Reviewed-by: Jakub Hrozek --- diff --git a/src/cmocka.c b/src/cmocka.c index 9d3e704..c8e612c 100644 --- a/src/cmocka.c +++ b/src/cmocka.c @@ -1676,6 +1676,7 @@ void *_test_realloc(void *ptr, } if (size == 0) { + _test_free(ptr, file, line); return NULL; } diff --git a/tests/test_alloc.c b/tests/test_alloc.c index babe3a8..966814a 100644 --- a/tests/test_alloc.c +++ b/tests/test_alloc.c @@ -64,10 +64,27 @@ static void torture_test_realloc(void **state) test_free(str); } +static void torture_test_realloc_set0(void **state) +{ + char *str; + size_t str_len; + + (void)state; /* unsused */ + + str_len = 16; + str = (char *)test_malloc(str_len); + assert_non_null(str); + + /* realloc(ptr, 0) is like a free() */ + str = (char *)test_realloc(str, 0); + assert_null(str); +} + int main(void) { const struct CMUnitTest alloc_tests[] = { cmocka_unit_test(torture_test_malloc), cmocka_unit_test(torture_test_realloc), + cmocka_unit_test(torture_test_realloc_set0), }; return cmocka_run_group_tests(alloc_tests, NULL, NULL);