Skip to content

Commit a3d8a43

Browse files
authored
Merge pull request libgit2#5298 from pks-t/pks/patch-whitespace-only-paths
patch_parse: fix segfault when header path contains whitespace only
2 parents 5d773a1 + de543e2 commit a3d8a43

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

src/patch_parse.c

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,27 +69,24 @@ static int parse_header_path_buf(git_buf *path, git_patch_parse_ctx *ctx, size_t
6969
{
7070
int error;
7171

72-
if (!path_len)
73-
return git_parse_err("patch contains empty path at line %"PRIuZ,
74-
ctx->parse_ctx.line_num);
75-
7672
if ((error = git_buf_put(path, ctx->parse_ctx.line, path_len)) < 0)
77-
goto done;
73+
return error;
7874

7975
git_parse_advance_chars(&ctx->parse_ctx, path_len);
8076

8177
git_buf_rtrim(path);
8278

83-
if (path->size > 0 && path->ptr[0] == '"')
84-
error = git_buf_unquote(path);
85-
86-
if (error < 0)
87-
goto done;
79+
if (path->size > 0 && path->ptr[0] == '"' &&
80+
(error = git_buf_unquote(path)) < 0)
81+
return error;
8882

8983
git_path_squash_slashes(path);
9084

91-
done:
92-
return error;
85+
if (!path->size)
86+
return git_parse_err("patch contains empty path at line %"PRIuZ,
87+
ctx->parse_ctx.line_num);
88+
89+
return 0;
9390
}
9491

9592
static int parse_header_path(char **out, git_patch_parse_ctx *ctx)

tests/patch/parse.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,20 @@ void test_patch_parse__binary_file_with_missing_paths(void)
156156
strlen(PATCH_BINARY_FILE_WITH_MISSING_PATHS), NULL));
157157
}
158158

159+
void test_patch_parse__binary_file_with_whitespace_paths(void)
160+
{
161+
git_patch *patch;
162+
cl_git_fail(git_patch_from_buffer(&patch, PATCH_BINARY_FILE_WITH_WHITESPACE_PATHS,
163+
strlen(PATCH_BINARY_FILE_WITH_WHITESPACE_PATHS), NULL));
164+
}
165+
166+
void test_patch_parse__binary_file_with_empty_quoted_paths(void)
167+
{
168+
git_patch *patch;
169+
cl_git_fail(git_patch_from_buffer(&patch, PATCH_BINARY_FILE_WITH_QUOTED_EMPTY_PATHS,
170+
strlen(PATCH_BINARY_FILE_WITH_QUOTED_EMPTY_PATHS), NULL));
171+
}
172+
159173
void test_patch_parse__memory_leak_on_multiple_paths(void)
160174
{
161175
git_patch *patch;

tests/patch/patch_common.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,18 @@
912912
"+++ \n" \
913913
"Binary files "
914914

915+
#define PATCH_BINARY_FILE_WITH_WHITESPACE_PATHS \
916+
"diff --git a/file b/file\n" \
917+
"--- \n" \
918+
"+++ \n" \
919+
"Binary files "
920+
921+
#define PATCH_BINARY_FILE_WITH_QUOTED_EMPTY_PATHS \
922+
"diff --git a/file b/file\n" \
923+
"--- \"\"\n" \
924+
"+++ \"\"\n" \
925+
"Binary files "
926+
915927
#define PATCH_MULTIPLE_OLD_PATHS \
916928
"diff --git \n" \
917929
"--- \n" \

0 commit comments

Comments
 (0)