Skip to content

Commit 41fc6b3

Browse files
dschogitster
authored andcommitted
split_commit_in_progress(): simplify & fix memory leak
This function did a whole lot of unnecessary work, such as reading in four files just to figure out that, oh, hey, we do not need to look at them after all because the HEAD is not detached. Simplify the entire function to return early when possible, to read in the files only when necessary, and to release the allocated memory always (there was a leak, reported via Coverity, where we failed to release the allocated strings if the HEAD is not detached). Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 514e803 commit 41fc6b3

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

wt-status.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,29 +1082,29 @@ static char *read_line_from_git_path(const char *filename)
10821082
static int split_commit_in_progress(struct wt_status *s)
10831083
{
10841084
int split_in_progress = 0;
1085-
char *head = read_line_from_git_path("HEAD");
1086-
char *orig_head = read_line_from_git_path("ORIG_HEAD");
1087-
char *rebase_amend = read_line_from_git_path("rebase-merge/amend");
1088-
char *rebase_orig_head = read_line_from_git_path("rebase-merge/orig-head");
1085+
char *head, *orig_head, *rebase_amend, *rebase_orig_head;
10891086

1090-
if (!head || !orig_head || !rebase_amend || !rebase_orig_head ||
1087+
if ((!s->amend && !s->nowarn && !s->workdir_dirty) ||
10911088
!s->branch || strcmp(s->branch, "HEAD"))
1092-
return split_in_progress;
1089+
return 0;
10931090

1094-
if (!strcmp(rebase_amend, rebase_orig_head)) {
1095-
if (strcmp(head, rebase_amend))
1096-
split_in_progress = 1;
1097-
} else if (strcmp(orig_head, rebase_orig_head)) {
1098-
split_in_progress = 1;
1099-
}
1091+
head = read_line_from_git_path("HEAD");
1092+
orig_head = read_line_from_git_path("ORIG_HEAD");
1093+
rebase_amend = read_line_from_git_path("rebase-merge/amend");
1094+
rebase_orig_head = read_line_from_git_path("rebase-merge/orig-head");
11001095

1101-
if (!s->amend && !s->nowarn && !s->workdir_dirty)
1102-
split_in_progress = 0;
1096+
if (!head || !orig_head || !rebase_amend || !rebase_orig_head)
1097+
; /* fall through, no split in progress */
1098+
else if (!strcmp(rebase_amend, rebase_orig_head))
1099+
split_in_progress = !!strcmp(head, rebase_amend);
1100+
else if (strcmp(orig_head, rebase_orig_head))
1101+
split_in_progress = 1;
11031102

11041103
free(head);
11051104
free(orig_head);
11061105
free(rebase_amend);
11071106
free(rebase_orig_head);
1107+
11081108
return split_in_progress;
11091109
}
11101110

0 commit comments

Comments
 (0)