Commit cdd7cc89 authored by Davide Galassi's avatar Davide Galassi

Malloc mock hotfix

parent e542f83f
Pipeline #59995884 passed with stages
in 1 minute and 40 seconds
...@@ -4,7 +4,7 @@ CC := gcc ...@@ -4,7 +4,7 @@ CC := gcc
CFLAGS := -O0 -g3 -Wall CFLAGS := -O0 -g3 -Wall
CPPFLAGS = -I. -I../include -I../src CPPFLAGS = -I. -I../include -I../src
LDFLAGS := --coverage -Wl,--wrap=malloc,--wrap=realloc,--wrap=free LDFLAGS := --coverage -Wl,--wrap=malloc,--wrap=realloc,--wrap=calloc,--wrap=free
objs := \ objs := \
......
...@@ -3,14 +3,16 @@ ...@@ -3,14 +3,16 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
void *__real_malloc(size_t size);
void *__real_realloc(void *ptr, size_t size);
void __real_free(void *ptr);
int g_malloc_mock_count; int g_malloc_mock_count;
int g_malloc_mock_state = MALLOC_MOCK_STOPPED; int g_malloc_mock_state = MALLOC_MOCK_STOPPED;
#define MAGIC 0xABADCAFE #define MAGIC 0xABADCAFE
void *__real_malloc(size_t size);
void *__real_realloc(void *ptr, size_t size);
void __real_free(void *ptr);
static int mock_update(void) static int mock_update(void)
{ {
...@@ -28,11 +30,13 @@ struct mem_head { ...@@ -28,11 +30,13 @@ struct mem_head {
size_t size; size_t size;
}; };
#define HEAD_SIZE sizeof(struct mem_head) typedef struct mem_head mem_head_t;
#define HEAD_SIZE sizeof(mem_head_t)
void *__wrap_malloc(size_t size) void *__wrap_malloc(size_t size)
{ {
struct mem_head *head; mem_head_t *head;
if (mock_update() == MALLOC_MOCK_FAILED) if (mock_update() == MALLOC_MOCK_FAILED)
return NULL; return NULL;
...@@ -48,7 +52,7 @@ void *__wrap_malloc(size_t size) ...@@ -48,7 +52,7 @@ void *__wrap_malloc(size_t size)
void *__wrap_realloc(void *ptr, size_t size) void *__wrap_realloc(void *ptr, size_t size)
{ {
struct mem_head *head; mem_head_t *head;
if (ptr == NULL) if (ptr == NULL)
return __wrap_malloc(size); return __wrap_malloc(size);
...@@ -56,27 +60,41 @@ void *__wrap_realloc(void *ptr, size_t size) ...@@ -56,27 +60,41 @@ void *__wrap_realloc(void *ptr, size_t size)
if (mock_update() == MALLOC_MOCK_FAILED) if (mock_update() == MALLOC_MOCK_FAILED)
return NULL; return NULL;
head = ((struct mem_head *)ptr) - 1; head = ((mem_head_t *)ptr) - 1;
if (head->magic != MAGIC) { if (head->magic != MAGIC) {
printf(">>> Warning: realloc of a not alloced buffer\n"); printf(">>> Warning: realloc of a not alloced buffer\n");
return NULL; return NULL;
} }
if (size < head->size)
memset((unsigned char *)&head[1] + size, 0xFF, head->size - size);
head = __real_realloc(head, (size != 0) ? size + HEAD_SIZE : 0); head = __real_realloc(head, (size != 0) ? size + HEAD_SIZE : 0);
if (size == 0) if (head == NULL || size == 0)
return NULL; return NULL;
if (size > head->size) if (size > head->size)
memset((unsigned char *)&head[1] + head->size, 0xFF, size - head->size); memset((unsigned char *)&head[1] + head->size, 0xFF, size - head->size);
else
memset((unsigned char *)&head[1] + size, 0xFF, head->size - size);
head->size = size; head->size = size;
return &head[1]; return &head[1];
} }
void *__wrap_calloc(size_t nmemb, size_t size)
{
void *ptr;
size = nmemb * size;
ptr = __wrap_malloc(size);
if (ptr == NULL || size == 0)
return NULL;
memset(ptr, 0, size);
return ptr;
}
void __wrap_free(void *ptr) void __wrap_free(void *ptr)
{ {
struct mem_head *head; mem_head_t *head;
head = ((struct mem_head *)ptr) - 1; if (ptr == NULL)
return;
head = ((mem_head_t *)ptr) - 1;
if (head->magic != MAGIC) { if (head->magic != MAGIC) {
printf(">>> Warning: free of a not alloced buffer\n"); printf(">>> Warning: free of a not alloced buffer\n");
return; return;
...@@ -84,4 +102,3 @@ void __wrap_free(void *ptr) ...@@ -84,4 +102,3 @@ void __wrap_free(void *ptr)
memset(head, 0xFF, HEAD_SIZE + head->size); memset(head, 0xFF, HEAD_SIZE + head->size);
__real_free(head); __real_free(head);
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment