diff options
author | Peter Wu <peter@lekensteyn.nl> | 2018-03-16 12:41:41 +0100 |
---|---|---|
committer | Evan Huus <eapache@gmail.com> | 2018-03-19 16:15:35 +0000 |
commit | acba1785460149b65a890a2b060ed87cf4e41102 (patch) | |
tree | e950601813720048dac5c390950d80b8cbfd03d4 /epan | |
parent | 859c5bf836f31a4d60ea161acc8a52a7571477ba (diff) |
wmem: ensure wmem_memdup(..., NULL, 0) returns NULL
Rather than requiring all callers to pass a non-null source argument,
explicitly allow a NULL source when the size is zero. This is consistent
with g_memdup behavior.
While at it, fix a memleak and avoid memset(0,0,0) in tests.
Change-Id: I86a092625a508544d180da959e4afdd0366539f4
Reviewed-on: https://code.wireshark.org/review/26496
Petri-Dish: Peter Wu <peter@lekensteyn.nl>
Reviewed-by: Mališa Vučinić <malishav@gmail.com>
Tested-by: Petri Dish Buildbot
Reviewed-by: Evan Huus <eapache@gmail.com>
Diffstat (limited to 'epan')
-rw-r--r-- | epan/wmem/wmem_miscutl.c | 3 | ||||
-rw-r--r-- | epan/wmem/wmem_miscutl.h | 2 | ||||
-rw-r--r-- | epan/wmem/wmem_test.c | 7 |
3 files changed, 10 insertions, 2 deletions
diff --git a/epan/wmem/wmem_miscutl.c b/epan/wmem/wmem_miscutl.c index d0ef1b18f1..33d5089e6c 100644 --- a/epan/wmem/wmem_miscutl.c +++ b/epan/wmem/wmem_miscutl.c @@ -20,6 +20,9 @@ wmem_memdup(wmem_allocator_t *allocator, const void *source, const size_t size) { void *dest; + if (!size) + return NULL; + dest = wmem_alloc(allocator, size); memcpy(dest, source, size); diff --git a/epan/wmem/wmem_miscutl.h b/epan/wmem/wmem_miscutl.h index c77e569d4d..012d60a7bb 100644 --- a/epan/wmem/wmem_miscutl.h +++ b/epan/wmem/wmem_miscutl.h @@ -33,7 +33,7 @@ extern "C" { * @param allocator The allocator object to use to allocate memory to copy into. * @param source The pointer to the memory block to copy. * @param size The amount of memory to copy. - * @return The location of the memory copy. + * @return The location of the memory copy or NULL if size is 0. */ WS_DLL_PUBLIC void * diff --git a/epan/wmem/wmem_test.c b/epan/wmem/wmem_test.c index 80044b740b..e3dfdda4c9 100644 --- a/epan/wmem/wmem_test.c +++ b/epan/wmem/wmem_test.c @@ -321,7 +321,8 @@ wmem_test_allocator(wmem_allocator_type_t type, wmem_verify_func verify, ptrs[ptrs_index] = (char *) wmem_realloc(allocator, ptrs[ptrs_index], new_size); - memset(ptrs[ptrs_index], 0, new_size); + if (new_size) + memset(ptrs[ptrs_index], 0, new_size); } else { /* the index is used, and our random bit has determined we will be @@ -379,6 +380,9 @@ wmem_test_miscutls(void) allocator = wmem_allocator_new(WMEM_ALLOCATOR_STRICT); + ret = (char*) wmem_memdup(allocator, NULL, 0); + g_assert(ret == NULL); + ret = (char*) wmem_memdup(allocator, source, 5); ret[4] = '\0'; g_assert_cmpstr(ret, ==, "ABCD"); @@ -608,6 +612,7 @@ wmem_test_stringperf(void) "wmem_strconcat 5 strings: u %.3f ms s %.3f ms", utime_ms, stime_ms); wmem_destroy_allocator(allocator); + g_free(str_ptr); } /* DATA STRUCTURE TESTING FUNCTIONS (/wmem/datastruct/) */ |