summaryrefslogtreecommitdiff
path: root/worktree.c
diff options
context:
space:
mode:
authorMartin Ågren <martin.agren@gmail.com>2020-09-27 13:15:43 (GMT)
committerJunio C Hamano <gitster@pobox.com>2020-09-27 21:21:47 (GMT)
commit962dd7ebc3e76afc2c896d377c319f8140966303 (patch)
tree0d91180f628c4bafd721d636e0b537575885fdff /worktree.c
parent8f7e3de0970c419688f23f505b5cb7a9690d9b09 (diff)
downloadgit-962dd7ebc3e76afc2c896d377c319f8140966303.zip
git-962dd7ebc3e76afc2c896d377c319f8140966303.tar.gz
git-962dd7ebc3e76afc2c896d377c319f8140966303.tar.bz2
wt-status: introduce wt_status_state_free_buffers()
When we have a `struct wt_status_state`, we manually free its `branch`, `onto` and `detached_from`, or sometimes just one or two of them. Provide a function `wt_status_state_free_buffers()` which does the freeing. The callers are still aware of these fields, e.g., they check whether `branch` was populated or not. But this way, they don't need to know about *all* of them, and if `struct wt_status_state` gets more fields, they will not need to learn to free them. Users of `struct wt_status` (which contains a `wt_status_state`) already have `wt_status_collect_free_buffers()` (corresponding to `wt_status_collect()`) which we can also teach to use this new helper. Finally, note that we're currently leaving dangling pointers behind. Some callers work on a stack-allocated struct, where this is obviously ok. But for the users of `run_status()` in builtin/commit.c, there are ample opportunities for someone to mistakenly use those dangling pointers. We seem to be ok for now, but it's a use-after-free waiting to happen. Let's leave NULL-pointers behind instead. Signed-off-by: Martin Ågren <martin.agren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'worktree.c')
-rw-r--r--worktree.c5
1 files changed, 2 insertions, 3 deletions
diff --git a/worktree.c b/worktree.c
index 62217b4..62a7eb9 100644
--- a/worktree.c
+++ b/worktree.c
@@ -357,8 +357,7 @@ int is_worktree_being_rebased(const struct worktree *wt,
state.branch &&
starts_with(target, "refs/heads/") &&
!strcmp(state.branch, target + strlen("refs/heads/")));
- free(state.branch);
- free(state.onto);
+ wt_status_state_free_buffers(&state);
return found_rebase;
}
@@ -373,7 +372,7 @@ int is_worktree_being_bisected(const struct worktree *wt,
state.branch &&
starts_with(target, "refs/heads/") &&
!strcmp(state.branch, target + strlen("refs/heads/"));
- free(state.branch);
+ wt_status_state_free_buffers(&state);
return found_rebase;
}