Skip to contentSkip to conversation

dfu: memory leak in dfu_free_entities()

PATCH New Heinrich Schuchardt 1 participant 2 messages Mar 18, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dfu: memory leak in dfu_free_entities()

We must free all entries that we remove from the list not just one to avoid a memory leak.

With list_for_each_entry_safe_reverse() it is safe to delete the current entry as it provides a second pointer for the next entry.

---
 drivers/dfu/dfu.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/dfu/dfu.c +1 -3
index 213a20e7bc..f307c9015f 100644
@@ -535,10 +535,8 @@ void dfu_free_entities(void)
 		list_del(&dfu->list);
 		if (dfu->free_entity)
 			dfu->free_entity(dfu);
-		t = dfu;
+		free(dfu);
 	}
-	if (t)
-		free(t);
 	INIT_LIST_HEAD(&dfu_list);
 
 	alt_num_cnt = 0;

--
2.30.2

#1 lore raw

PATCH 1/1 dfu: memory leak in dfu_free_entities()

Quoted 5 lines · Heinrich Schuchardt

We must free all entries that we remove from the list not just one to avoid a memory leak.

With list_for_each_entry_safe_reverse() it is safe to delete the current entry as it provides a second pointer for the next entry.

I got that wrong:

dfu_alt_init() reserves memory for dfu_alt_num entries at once. So must call free() only once.

Sorry for the noise.

Best regards

Heinrich