don't leak 'relpath' memory in got_worktree_schedule_add()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
diff --git a/lib/worktree.c b/lib/worktree.c
index 39f7d97..2809f0b 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -2178,6 +2178,31 @@ done:
return err;
}
+static const struct got_error *
+schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
+ const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
+ struct got_repository *repo)
+{
+ const struct got_error *err = NULL;
+ struct got_fileindex_entry *ie;
+
+ /* Re-adding an existing entry is a no-op. */
+ if (got_fileindex_entry_get(fileindex, relpath) != NULL)
+ return NULL;
+
+ err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
+ if (err)
+ return err;
+
+ err = got_fileindex_entry_add(fileindex, ie);
+ if (err) {
+ got_fileindex_entry_free(ie);
+ return err;
+ }
+
+ return report_file_status(ie, relpath, status_cb, status_arg, repo);
+}
+
const struct got_error *
got_worktree_schedule_add(struct got_worktree *worktree,
struct got_pathlist_head *ondisk_paths,
@@ -2219,34 +2244,16 @@ got_worktree_schedule_add(struct got_worktree *worktree,
goto done;
TAILQ_FOREACH(pe, ondisk_paths, entry) {
- struct got_fileindex_entry *ie = NULL;
char *relpath;
-
err = got_path_skip_common_ancestor(&relpath,
got_worktree_get_root_path(worktree), pe->path);
if (err)
goto done;
-
- /* Re-adding an existing entry is a no-op. */
- if (got_fileindex_entry_get(fileindex, relpath) != NULL)
- continue;
-
- err = got_fileindex_entry_alloc(&ie, pe->path, relpath,
- NULL, NULL);
+ err = schedule_addition(pe->path, fileindex, relpath,
+ status_cb, status_arg, repo);
free(relpath);
if (err)
goto done;
-
- err = got_fileindex_entry_add(fileindex, ie);
- if (err) {
- got_fileindex_entry_free(ie);
- goto done;
- }
-
- err = report_file_status(ie, pe->path, status_cb, status_arg,
- repo);
- if (err)
- goto done;
}
err = sync_fileindex(fileindex, fileindex_path);