mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2025-01-17 04:32:46 +01:00
cpu/esp_common: add overflow detection to calloc
If esp_idf_heap is not used, implement calloc through a custom wrapper function on top of malloc to add overflow detection, which is not present in the newlib forks with xtensa support yet.
This commit is contained in:
parent
eefed0e0cd
commit
a9dea12eb8
9
cpu/esp32/vendor/esp-idf/heap/heap_trace.c
vendored
9
cpu/esp32/vendor/esp-idf/heap/heap_trace.c
vendored
@ -407,10 +407,13 @@ IRAM_ATTR void *__wrap_realloc(void *p, size_t size)
|
||||
|
||||
IRAM_ATTR void *__wrap_calloc(size_t nmemb, size_t size)
|
||||
{
|
||||
size = size * nmemb;
|
||||
void *result = trace_malloc(size, 0, TRACE_MALLOC_DEFAULT);
|
||||
size_t total_size;
|
||||
if (__builtin_mul_overflow(nmemb, size, &total_size)) {
|
||||
return NULL;
|
||||
}
|
||||
void *result = trace_malloc(total_size, 0, TRACE_MALLOC_DEFAULT);
|
||||
if (result != NULL) {
|
||||
memset(result, 0, size);
|
||||
memset(result, 0, total_size);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
@ -85,6 +85,12 @@ LINKFLAGS += -L$(ESP_SDK_DIR)/components/$(CPU)
|
||||
LINKFLAGS += -L$(ESP_SDK_DIR)/components/$(CPU)/lib
|
||||
LINKFLAGS += -nostdlib -Wl,-gc-sections -Wl,-static
|
||||
|
||||
ifeq (,$(filter esp_idf_heap,$(USEMODULE)))
|
||||
# use the wrapper functions for calloc to add correct overflow detection missing
|
||||
# in the newlib's version.
|
||||
LINKFLAGS += -Wl,-wrap=calloc
|
||||
endif
|
||||
|
||||
# LINKFLAGS += -Wl,--verbose
|
||||
# LINKFLAGS += -Wl,--print-gc-sections
|
||||
|
||||
|
@ -289,6 +289,25 @@ void* IRAM_ATTR __wrap__calloc_r(struct _reent *r, size_t count, size_t size)
|
||||
|
||||
#else /* MODULE_ESP_IDF_HEAP */
|
||||
|
||||
void *__wrap_calloc(size_t nmemb, size_t size)
|
||||
{
|
||||
/* The xtensa support has not yet upstreamed to newlib. Hence, the fixed
|
||||
* calloc implementation of newlib >= 4.0.0 is not available to the ESP
|
||||
* platform. We fix this by implementing calloc on top of malloc ourselves */
|
||||
size_t total_size;
|
||||
if (__builtin_mul_overflow(nmemb, size, &total_size)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void *res = malloc(total_size);
|
||||
|
||||
if (res) {
|
||||
memset(res, 0, total_size);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
/* for compatibility with ESP-IDF heap functions */
|
||||
|
||||
void* _heap_caps_malloc(size_t size, uint32_t caps, const char *file, size_t line)
|
||||
|
Loading…
Reference in New Issue
Block a user