From 8cbd12b9b776caa88b9bc9b91a8324d3afe2a321 Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Tue, 31 Jul 2018 14:51:40 +1000 Subject: [PATCH] heap: When verifying & swapping a fill pattern, stop swapping if verification fails Avoids situation where data has been overwritten during any post-mortem analysis (core dump, gdbstub, etc). As reported via forum https://esp32.com/viewtopic.php?f=2&t=6471&p=27790 --- components/heap/multi_heap_poisoning.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/components/heap/multi_heap_poisoning.c b/components/heap/multi_heap_poisoning.c index 3c8cff2410..dabf6cc241 100644 --- a/components/heap/multi_heap_poisoning.c +++ b/components/heap/multi_heap_poisoning.c @@ -147,6 +147,12 @@ static bool verify_fill_pattern(void *data, size_t size, bool print_errors, bool MULTI_HEAP_STDERR_PRINTF("CORRUPT HEAP: Invalid data at %p. Expected 0x%08x got 0x%08x\n", p, EXPECT_WORD, *p); } valid = false; +#ifndef NDEBUG + /* If an assertion is going to fail as soon as we're done verifying the pattern, leave the rest of the + buffer contents as-is for better post-mortem analysis + */ + swap_pattern = false; +#endif } if (swap_pattern) { *p = REPLACE_WORD; @@ -164,6 +170,9 @@ static bool verify_fill_pattern(void *data, size_t size, bool print_errors, bool MULTI_HEAP_STDERR_PRINTF("CORRUPT HEAP: Invalid data at %p. Expected 0x%02x got 0x%02x\n", p, (uint8_t)EXPECT_WORD, *p); } valid = false; +#ifndef NDEBUG + swap_pattern = false; // same as above +#endif } if (swap_pattern) { p[i] = (uint8_t)REPLACE_WORD;