From 490df3a34867b7ebdbc0aec0f5999973c1decd4a Mon Sep 17 00:00:00 2001 From: Guillaume Souchere Date: Mon, 18 Dec 2023 12:33:59 +0100 Subject: [PATCH] fix(heap): prevent double traversal of hashmap in map_find_and_remove Remove the use of SLIST_REMOVE in map_find_and_remove to prevent the hashmap list to be traversed twice in the function. Closes https://github.com/espressif/esp-idf/issues/12820 --- components/heap/heap_trace_standalone.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/components/heap/heap_trace_standalone.c b/components/heap/heap_trace_standalone.c index 3a62d24cf75..3a292a13ed2 100644 --- a/components/heap/heap_trace_standalone.c +++ b/components/heap/heap_trace_standalone.c @@ -134,12 +134,18 @@ static HEAP_IRAM_ATTR heap_trace_record_t* map_find_and_remove(void *p) { size_t idx = hash_idx(p); heap_trace_record_t *r_cur = NULL; + heap_trace_record_t *r_prev = NULL; SLIST_FOREACH(r_cur, &hash_map[idx], slist_hashmap) { if (r_cur->address == p) { total_hashmap_hits++; - SLIST_REMOVE(&hash_map[idx], r_cur, heap_trace_record_t, slist_hashmap); + if (r_prev) { + SLIST_REMOVE_AFTER(r_prev, slist_hashmap); + } else { + SLIST_REMOVE_HEAD(&hash_map[idx], slist_hashmap); + } return r_cur; } + r_prev = r_cur; } total_hashmap_miss++; return NULL;