Commit e1ed7f77442c0693e02984d550e3cc674e3c0dc0

Stefan Sperling 2019-01-06T13:47:14

must use safe iteration over file index entries

diff --git a/lib/fileindex.c b/lib/fileindex.c
index dbe625f..05a75f2 100644
--- a/lib/fileindex.c
+++ b/lib/fileindex.c
@@ -121,14 +121,14 @@ got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path)
 }
 
 const struct got_error *
-got_fileindex_for_each_entry(struct got_fileindex *fileindex,
+got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
     const struct got_error *(cb)(void *, struct got_fileindex_entry *),
     void *cb_arg)
 {
 	const struct got_error *err = NULL;
-	struct got_fileindex_entry *entry;
+	struct got_fileindex_entry *entry, *tmp;
 
-	TAILQ_FOREACH(entry, &fileindex->entries, entry) {
+	TAILQ_FOREACH_SAFE(entry, &fileindex->entries, entry, tmp) {
 		err = cb(cb_arg, entry);
 		if (err)
 			break;
diff --git a/lib/got_lib_fileindex.h b/lib/got_lib_fileindex.h
index f93174f..327aba8 100644
--- a/lib/got_lib_fileindex.h
+++ b/lib/got_lib_fileindex.h
@@ -100,5 +100,6 @@ void got_fileindex_entry_remove(struct got_fileindex *,
 struct got_fileindex_entry *got_fileindex_entry_get(struct got_fileindex *,
     const char *);
 const struct got_error *got_fileindex_read(struct got_fileindex *, FILE *);
-const struct got_error *got_fileindex_for_each_entry(struct got_fileindex *,
+const struct got_error *got_fileindex_for_each_entry_safe(
+    struct got_fileindex *,
     const struct got_error *(cb)(void *, struct got_fileindex_entry *), void *);
diff --git a/lib/worktree.c b/lib/worktree.c
index 68c4f29..e43e15f 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -800,7 +800,8 @@ remove_missing_files(struct got_worktree *worktree, const char *path,
 	a.missing_entries.nentries = 0;
 	a.current_subdir = apply_path_prefix(worktree, path);
 	TAILQ_INIT(&a.missing_entries.entries);
-	err = got_fileindex_for_each_entry(fileindex, collect_missing_file, &a);
+	err = got_fileindex_for_each_entry_safe(fileindex,
+	    collect_missing_file, &a);
 	if (err)
 		return err;