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>
This commit is contained in:
parent
859c5bf836
commit
acba178546
|
@ -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);
|
||||
|
||||
|
|
|
@ -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 *
|
||||
|
|
|
@ -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/) */
|
||||
|
|
Loading…
Reference in New Issue