Skip to content

Commit 6c6c15e

Browse files
committed
patch_parse: reject empty path names
When parsing patch headers, we currently accept empty path names just fine, e.g. a line "--- \n" would be parsed as the empty filename. This is not a valid patch format and may cause `NULL` pointer accesses at a later place as `git_buf_detach` will return `NULL` in that case. Reject such patches as malformed with a nice error message.
1 parent 223e7e4 commit 6c6c15e

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

src/patch_parse.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ 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+
7276
if ((error = git_buf_put(path, ctx->parse_ctx.line, path_len)) < 0)
7377
goto done;
7478

tests/patch/parse.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,13 @@ void test_patch_parse__lifetime_of_patch_does_not_depend_on_buffer(void)
149149
git_patch_free(patch);
150150
}
151151

152+
void test_patch_parse__binary_file_with_missing_paths(void)
153+
{
154+
git_patch *patch;
155+
cl_git_fail(git_patch_from_buffer(&patch, PATCH_BINARY_FILE_WITH_MISSING_PATHS,
156+
strlen(PATCH_BINARY_FILE_WITH_MISSING_PATHS), NULL));
157+
}
158+
152159
void test_patch_parse__memory_leak_on_multiple_paths(void)
153160
{
154161
git_patch *patch;

tests/patch/patch_common.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,12 @@
906906
"+bb\n" \
907907
" c\n"
908908

909+
#define PATCH_BINARY_FILE_WITH_MISSING_PATHS \
910+
"diff --git \n" \
911+
"--- \n" \
912+
"+++ \n" \
913+
"Binary files "
914+
909915
#define PATCH_MULTIPLE_OLD_PATHS \
910916
"diff --git \n" \
911917
"--- \n" \

0 commit comments

Comments
 (0)