summaryrefslogtreecommitdiff
path: root/accel-pppd/memdebug.c
diff options
context:
space:
mode:
authorGuillaume Nault <g.nault@alphalink.fr>2013-07-10 12:54:41 +0200
committerDmitry Kozlov <xeb@mail.ru>2013-07-18 22:57:36 +0400
commit16b4d40a0f52eed4fd7def096a443670eab96b66 (patch)
treec090ef613f78eef02622e224d8397963827bf163 /accel-pppd/memdebug.c
parent6ce3556e0b1e39fe5474114137fde0d29ab093af (diff)
downloadaccel-ppp-16b4d40a0f52eed4fd7def096a443670eab96b66.tar.gz
accel-ppp-16b4d40a0f52eed4fd7def096a443670eab96b66.zip
memdbg: improve md_realloc() compliance with plain realloc()
-Allow reallocation of NULL pointers (equivalent to a malloc() call) -Allow 'size' to be 0 (equivalent to a free() call) -Handle case where 'size' is smaller than the original buffer length Signed-off-by: Guillaume Nault <g.nault@alphalink.fr>
Diffstat (limited to 'accel-pppd/memdebug.c')
-rw-r--r--accel-pppd/memdebug.c38
1 files changed, 26 insertions, 12 deletions
diff --git a/accel-pppd/memdebug.c b/accel-pppd/memdebug.c
index 286ddd84..67c84985 100644
--- a/accel-pppd/memdebug.c
+++ b/accel-pppd/memdebug.c
@@ -105,23 +105,37 @@ void __export md_free(void *ptr, const char *fname, int line)
void __export *md_realloc(void *ptr, size_t size, const char *fname, int line)
{
- struct mem_t *mem = container_of(ptr, typeof(*mem), data);
+ struct mem_t *mem = ptr ? container_of(ptr, typeof(*mem), data) : NULL;
struct mem_t *mem2;
-
- if (mem->magic1 != MAGIC1) {
- printf("memory corruption:\nfree at %s:%i\n", fname, line);
- abort();
- }
- if (mem->magic2 != *(uint64_t*)(mem->data + mem->size)) {
- printf("memory corruption:\nmalloc(%lu) at %s:%i\nfree at %s:%i\n", (long unsigned)mem->size, mem->fname, mem->line, fname, line);
- abort();
+ if (mem) {
+ if (mem->magic1 != MAGIC1) {
+ printf("memory corruption:\nfree at %s:%i\n",
+ fname, line);
+ abort();
+ }
+
+ if (mem->magic2 != *(uint64_t*)(mem->data + mem->size)) {
+ printf("memory corruption:\nmalloc(%zu) at %s:%i\nfree at %s:%i\n",
+ mem->size, mem->fname, mem->line, fname, line);
+ abort();
+ }
+
+ if (size == 0) {
+ md_free(mem->data, fname, line);
+ return NULL;
+ }
}
mem2 = _md_malloc(size, fname, line);
- memcpy(mem2->data, mem->data, mem->size);
-
- md_free(mem->data, fname, line);
+ if (mem2 == NULL)
+ return NULL;
+
+ if (mem) {
+ memcpy(mem2->data, mem->data,
+ (size < mem->size) ? size : mem->size);
+ md_free(mem->data, fname, line);
+ }
return mem2->data;
}