Skip to content

Commit b450a39

Browse files
committed
Merge branch 'cc/apply' into maint
Minor code clean-up. * cc/apply: builtin/apply: free patch when parse_chunk() fails builtin/apply: handle parse_binary() failure apply: remove unused call to free() in gitdiff_{old,new}name() builtin/apply: get rid of useless 'name' variable
2 parents c75fb77 + 7a6a44c commit b450a39

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

builtin/apply.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -931,22 +931,19 @@ static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name,
931931
return find_name(line, NULL, p_value, TERM_TAB);
932932

933933
if (orig_name) {
934-
int len;
935-
const char *name;
934+
int len = strlen(orig_name);
936935
char *another;
937-
name = orig_name;
938-
len = strlen(name);
939936
if (isnull)
940-
die(_("git apply: bad git-diff - expected /dev/null, got %s on line %d"), name, linenr);
937+
die(_("git apply: bad git-diff - expected /dev/null, got %s on line %d"),
938+
orig_name, linenr);
941939
another = find_name(line, NULL, p_value, TERM_TAB);
942-
if (!another || memcmp(another, name, len + 1))
940+
if (!another || memcmp(another, orig_name, len + 1))
943941
die((side == DIFF_NEW_NAME) ?
944942
_("git apply: bad git-diff - inconsistent new filename on line %d") :
945943
_("git apply: bad git-diff - inconsistent old filename on line %d"), linenr);
946944
free(another);
947945
return orig_name;
948-
}
949-
else {
946+
} else {
950947
/* expect "/dev/null" */
951948
if (memcmp("/dev/null", line, 9) || line[9] != '\n')
952949
die(_("git apply: bad git-diff - expected /dev/null on line %d"), linenr);
@@ -956,21 +953,15 @@ static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name,
956953

957954
static int gitdiff_oldname(const char *line, struct patch *patch)
958955
{
959-
char *orig = patch->old_name;
960956
patch->old_name = gitdiff_verify_name(line, patch->is_new, patch->old_name,
961957
DIFF_OLD_NAME);
962-
if (orig != patch->old_name)
963-
free(orig);
964958
return 0;
965959
}
966960

967961
static int gitdiff_newname(const char *line, struct patch *patch)
968962
{
969-
char *orig = patch->new_name;
970963
patch->new_name = gitdiff_verify_name(line, patch->is_delete, patch->new_name,
971964
DIFF_NEW_NAME);
972-
if (orig != patch->new_name)
973-
free(orig);
974965
return 0;
975966
}
976967

@@ -1872,6 +1863,11 @@ static struct fragment *parse_binary_hunk(char **buf_p,
18721863
return NULL;
18731864
}
18741865

1866+
/*
1867+
* Returns:
1868+
* -1 in case of error,
1869+
* the length of the parsed binary patch otherwise
1870+
*/
18751871
static int parse_binary(char *buffer, unsigned long size, struct patch *patch)
18761872
{
18771873
/*
@@ -2017,6 +2013,8 @@ static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)
20172013
linenr++;
20182014
used = parse_binary(buffer + hd + llen,
20192015
size - hd - llen, patch);
2016+
if (used < 0)
2017+
return -1;
20202018
if (used)
20212019
patchsize = used + llen;
20222020
else
@@ -4373,8 +4371,10 @@ static int apply_patch(int fd, const char *filename, int options)
43734371
patch->inaccurate_eof = !!(options & INACCURATE_EOF);
43744372
patch->recount = !!(options & RECOUNT);
43754373
nr = parse_chunk(buf.buf + offset, buf.len - offset, patch);
4376-
if (nr < 0)
4374+
if (nr < 0) {
4375+
free_patch(patch);
43774376
break;
4377+
}
43784378
if (apply_in_reverse)
43794379
reverse_patches(patch);
43804380
if (use_patch(patch)) {

0 commit comments

Comments
 (0)