add_delta_base_cache: use list_for_each_safe

We may remove elements from the list while we are iterating, which requires using a second temporary pointer. Otherwise stepping to the next element of the list might involve looking at freed memory (which generally works in practice, as we _just_ freed it, but of course is wrong to rely on; valgrind notices it). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Sep 12, 2016 at 12:46 UTC 4b92bae7d363adfe923337f198f9c98dc1c68e27
1 file changed +2 -2
sha1_file.c
+2 -2
@@ -2187,11 +2187,11 @@ static void add_delta_base_cache(struct packed_git *p, off_t base_offset,
2187 void *base, unsigned long base_size, enum object_type type)
2188 {
2189 struct delta_base_cache_entry *ent = xmalloc(sizeof(*ent));
2190 - struct list_head *lru;
2190 + struct list_head *lru, *tmp;
2191
2192 delta_base_cached += base_size;
2193
2194 - list_for_each(lru, &delta_base_cache_lru) {
2194 + list_for_each_safe(lru, tmp, &delta_base_cache_lru) {
2195 struct delta_base_cache_entry *f =
2196 list_entry(lru, struct delta_base_cache_entry, lru);
2197 if (delta_base_cached <= delta_base_cache_limit)