From: Brian Aker Date: Wed, 30 Nov 2011 08:16:51 +0000 (-0800) Subject: First pass through to make sure malloc is type safe X-Git-Tag: 1.0.3~4^2^2~1 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=f34725871a4ad46ea3b3194d829eebf81cc5c08e;p=awesomized%2Flibmemcached First pass through to make sure malloc is type safe --- diff --git a/libmemcached-1.0/types.h b/libmemcached-1.0/types.h index 3a4ae3bd..2781321e 100644 --- a/libmemcached-1.0/types.h +++ b/libmemcached-1.0/types.h @@ -38,6 +38,28 @@ #pragma once +#ifdef __cplusplus +struct memcached_st; +struct memcached_stat_st; +struct memcached_analysis_st; +struct memcached_result_st; +struct memcached_array_st; +struct memcached_error_t; + +// All of the flavors of memcache_server_st +struct memcached_server_st; +typedef const struct memcached_server_st *memcached_server_instance_st; +typedef struct memcached_server_st *memcached_server_list_st; + +struct memcached_callback_st; + +// The following two structures are internal, and never exposed to users. +struct memcached_string_st; +struct memcached_string_t; +struct memcached_continuum_item_st; + +#else + typedef struct memcached_st memcached_st; typedef struct memcached_stat_st memcached_stat_st; typedef struct memcached_analysis_st memcached_analysis_st; @@ -57,6 +79,8 @@ typedef struct memcached_string_st memcached_string_st; typedef struct memcached_string_t memcached_string_t; typedef struct memcached_continuum_item_st memcached_continuum_item_st; +#endif + #ifdef __cplusplus extern "C" { diff --git a/libmemcached/allocators.cc b/libmemcached/allocators.cc index d18372b4..98c87591 100644 --- a/libmemcached/allocators.cc +++ b/libmemcached/allocators.cc @@ -60,7 +60,7 @@ void *_libmemcached_calloc(const memcached_st *self, size_t nelem, size_t size, if (self->allocators.malloc != _libmemcached_malloc) { void *ret = _libmemcached_malloc(self, nelem * size, context); - if (not ret) + if (ret == NULL) { memset(ret, 0, nelem * size); } diff --git a/libmemcached/error.cc b/libmemcached/error.cc index e22871cd..f7764d0f 100644 --- a/libmemcached/error.cc +++ b/libmemcached/error.cc @@ -62,7 +62,7 @@ static void _set(memcached_server_st& server, memcached_st& memc) return; } - memcached_error_t *error= (struct memcached_error_t *)libmemcached_malloc(&memc, sizeof(struct memcached_error_t)); + memcached_error_t *error= libmemcached_xmalloc(&memc, memcached_error_t); if (error == NULL) // Bad business if this happens { return; @@ -112,7 +112,7 @@ static void _set(memcached_st& memc, memcached_string_t *str, memcached_return_t rc= MEMCACHED_CONNECTION_FAILURE; } - memcached_error_t *error= (struct memcached_error_t *)libmemcached_malloc(&memc, sizeof(struct memcached_error_t)); + memcached_error_t *error= libmemcached_xmalloc(&memc, memcached_error_t); if (error == NULL) // Bad business if this happens { return; @@ -533,7 +533,7 @@ memcached_error_t *memcached_error_copy(const memcached_server_st& server) return NULL; } - memcached_error_t *error= (memcached_error_t *)libmemcached_malloc(server.root, sizeof(memcached_error_t)); + memcached_error_t *error= libmemcached_xmalloc(server.root, memcached_error_t); memcpy(error, server.error_messages, sizeof(memcached_error_t)); error->next= NULL; diff --git a/libmemcached/get.cc b/libmemcached/get.cc index 4ad49a41..c10f2341 100644 --- a/libmemcached/get.cc +++ b/libmemcached/get.cc @@ -680,8 +680,8 @@ static memcached_return_t binary_mget_by_key(memcached_st *ptr, keys, key_length, number_of_keys, mget_mode); } - uint32_t* hash= static_cast(libmemcached_malloc(ptr, sizeof(uint32_t) * number_of_keys)); - bool* dead_servers= static_cast(libmemcached_calloc(ptr, memcached_server_count(ptr), sizeof(bool))); + uint32_t* hash= libmemcached_xvalloc(ptr, number_of_keys, uint32_t); + bool* dead_servers= libmemcached_xcalloc(ptr, memcached_server_count(ptr), bool); if (hash == NULL || dead_servers == NULL) { diff --git a/libmemcached/hosts.cc b/libmemcached/hosts.cc index fe3c977a..cb0e3f30 100644 --- a/libmemcached/hosts.cc +++ b/libmemcached/hosts.cc @@ -185,7 +185,8 @@ static memcached_return_t update_continuum(memcached_st *ptr) memcached_continuum_item_st *new_ptr; new_ptr= static_cast(libmemcached_realloc(ptr, ptr->ketama.continuum, - sizeof(memcached_continuum_item_st) * (live_servers + MEMCACHED_CONTINUUM_ADDITION) * points_per_server)); + (live_servers + MEMCACHED_CONTINUUM_ADDITION) * points_per_server, + sizeof(memcached_continuum_item_st))); if (new_ptr == 0) { @@ -354,7 +355,8 @@ static memcached_return_t server_add(memcached_st *ptr, assert_msg(ptr, "Programmer mistake, somehow server_add() was passed a NULL memcached_st"); memcached_server_st *new_host_list= static_cast(libmemcached_realloc(ptr, memcached_server_list(ptr), - sizeof(memcached_server_st) * (ptr->number_of_hosts + 1))); + (ptr->number_of_hosts + 1), + sizeof(memcached_server_st))); if (new_host_list == NULL) { @@ -397,7 +399,7 @@ memcached_return_t memcached_server_push(memcached_st *ptr, const memcached_serv memcached_server_st *new_host_list; new_host_list= static_cast(libmemcached_realloc(ptr, memcached_server_list(ptr), - sizeof(memcached_server_st) * (count + memcached_server_count(ptr)))); + (count + memcached_server_count(ptr)), sizeof(memcached_server_st))); if (new_host_list == NULL) { diff --git a/libmemcached/memory.h b/libmemcached/memory.h index 65d06b8c..89ad6984 100644 --- a/libmemcached/memory.h +++ b/libmemcached/memory.h @@ -57,16 +57,19 @@ static inline void *libmemcached_malloc(const memcached_st *self, const size_t s return malloc(size); } +#define libmemcached_xmalloc(__memcachd_st, __type) ((__type *)libmemcached_malloc((__memcachd_st), sizeof(__type))) -static inline void *libmemcached_realloc(const memcached_st *self, void *mem, const size_t size) +static inline void *libmemcached_realloc(const memcached_st *self, void *mem, size_t nmemb, const size_t size) { if (self) { - return self->allocators.realloc(self, mem, size, self->allocators.context); + return self->allocators.realloc(self, mem, nmemb * size, self->allocators.context); } return realloc(mem, size); } +#define libmemcached_xrealloc(__memcachd_st, __mem, __nelem, __type) ((__type *)libmemcached_realloc((__memcachd_st), (__mem), (__nelem), sizeof(__type))) +#define libmemcached_xvalloc(__memcachd_st, __nelem, __type) ((__type *)libmemcached_realloc((__memcachd_st), NULL, (__nelem), sizeof(__type))) static inline void *libmemcached_calloc(const memcached_st *self, size_t nelem, size_t size) { @@ -77,3 +80,4 @@ static inline void *libmemcached_calloc(const memcached_st *self, size_t nelem, return calloc(nelem, size); } +#define libmemcached_xcalloc(__memcachd_st, __nelem, __type) ((__type *)libmemcached_calloc((__memcachd_st), (__nelem), sizeof(__type))) diff --git a/libmemcached/result.cc b/libmemcached/result.cc index 1a5095af..a3438c08 100644 --- a/libmemcached/result.cc +++ b/libmemcached/result.cc @@ -68,7 +68,7 @@ memcached_result_st *memcached_result_create(const memcached_st *memc, } else { - ptr= static_cast(libmemcached_malloc(memc, sizeof(memcached_result_st))); + ptr= libmemcached_xmalloc(memc, memcached_result_st); if (not ptr) { diff --git a/libmemcached/server.cc b/libmemcached/server.cc index e1b91b94..7011fd24 100644 --- a/libmemcached/server.cc +++ b/libmemcached/server.cc @@ -90,7 +90,7 @@ static memcached_server_st *_server_create(memcached_server_st *self, const memc { if (not self) { - self= (memcached_server_st *)libmemcached_malloc(memc, sizeof(memcached_server_st)); + self= libmemcached_xmalloc(memc, struct memcached_server_st); if (not self) { diff --git a/libmemcached/string.cc b/libmemcached/string.cc index d482d4cd..75641ccd 100644 --- a/libmemcached/string.cc +++ b/libmemcached/string.cc @@ -56,7 +56,7 @@ inline static memcached_return_t _string_check(memcached_string_st *string, size if (new_size < need) return memcached_set_error(*string->root, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT); - new_value= static_cast(libmemcached_realloc(string->root, string->string, new_size)); + new_value= static_cast(libmemcached_realloc(string->root, string->string, new_size, sizeof(char))); if (not new_value) { @@ -91,7 +91,7 @@ memcached_string_st *memcached_string_create(memcached_st *memc, memcached_strin } else { - self= static_cast(libmemcached_malloc(memc, sizeof(memcached_string_st))); + self= libmemcached_xmalloc(memc, memcached_string_st); if (self == NULL) {