diff options
author | Niels de Vos <ndevos@redhat.com> | 2015-02-26 21:47:03 +0100 |
---|---|---|
committer | Andreas Schneider <asn@cryptomilk.org> | 2015-03-02 10:16:22 +0100 |
commit | 78191eaebfa2fc212ea4ade51e2b67c468e84834 (patch) | |
tree | 38d19df57866a7c537ea741cd1cca671df4dbc13 | |
parent | 710400b8c71b97e97b85addaefead4489743c108 (diff) | |
download | cmocka-78191eaebfa2fc212ea4ade51e2b67c468e84834.tar.gz cmocka-78191eaebfa2fc212ea4ade51e2b67c468e84834.tar.bz2 cmocka-78191eaebfa2fc212ea4ade51e2b67c468e84834.zip |
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 <ndevos@redhat.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
Reviewed-by: Jakub Hrozek <jakub.hrozek@posteo.se>
-rw-r--r-- | src/cmocka.c | 1 | ||||
-rw-r--r-- | tests/test_alloc.c | 17 |
2 files changed, 18 insertions, 0 deletions
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); |