Skip to content

Commit c64aba2

Browse files
committed
Merge branch 'bugfix/heap_int_overflow' into 'master'
fix(heap): Add integer overflow checks See merge request sdk/ESP8266_RTOS_SDK!1586
2 parents 14e3889 + b9b4d8d commit c64aba2

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

components/heap/port/esp8266/include/priv/esp_heap_caps_priv.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
extern "C" {
2222
#endif
2323

24+
#define HEAP_MAX_SIZE (96 * 1024)
25+
2426
#define MEM_BLK_TAG 0x80000000 ///< Mark the memory block used
2527

2628
#ifdef CONFIG_HEAP_TRACING

components/heap/src/esp_heap_caps.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ void IRAM_ATTR *_heap_caps_malloc(size_t size, uint32_t caps, const char *file,
101101
uint32_t num;
102102
uint32_t mem_blk_size;
103103

104+
if (size > (HEAP_MAX_SIZE - sizeof(mem2_blk_t) * 2)) {
105+
ESP_EARLY_LOGV(TAG, "size=%u is oveflow", size);
106+
return NULL;
107+
}
108+
104109
if (line == 0) {
105110
ESP_EARLY_LOGV(TAG, "caller func %p", file);
106111
} else {
@@ -297,9 +302,16 @@ void IRAM_ATTR _heap_caps_free(void *ptr, const char *file, size_t line)
297302
*/
298303
void *_heap_caps_calloc(size_t count, size_t size, uint32_t caps, const char *file, size_t line)
299304
{
300-
void *p = _heap_caps_malloc(count * size, caps, file, line);
305+
size_t size_bytes;
306+
307+
if (__builtin_mul_overflow(count, size, &size_bytes)) {
308+
ESP_EARLY_LOGV(TAG, "count=%u size=%u is oveflow", count, size);
309+
return NULL;
310+
}
311+
312+
void *p = _heap_caps_malloc(size_bytes, caps, file, line);
301313
if (p)
302-
memset(p, 0, count * size);
314+
memset(p, 0, size_bytes);
303315

304316
return p;
305317
}

0 commit comments

Comments
 (0)