Commit 6c6c15e9 by Patrick Steinhardt

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.
parent 223e7e43
...@@ -69,6 +69,10 @@ static int parse_header_path_buf(git_buf *path, git_patch_parse_ctx *ctx, size_t ...@@ -69,6 +69,10 @@ static int parse_header_path_buf(git_buf *path, git_patch_parse_ctx *ctx, size_t
{ {
int error; int error;
if (!path_len)
return git_parse_err("patch contains empty path at line %"PRIuZ,
ctx->parse_ctx.line_num);
if ((error = git_buf_put(path, ctx->parse_ctx.line, path_len)) < 0) if ((error = git_buf_put(path, ctx->parse_ctx.line, path_len)) < 0)
goto done; goto done;
......
...@@ -149,6 +149,13 @@ void test_patch_parse__lifetime_of_patch_does_not_depend_on_buffer(void) ...@@ -149,6 +149,13 @@ void test_patch_parse__lifetime_of_patch_does_not_depend_on_buffer(void)
git_patch_free(patch); git_patch_free(patch);
} }
void test_patch_parse__binary_file_with_missing_paths(void)
{
git_patch *patch;
cl_git_fail(git_patch_from_buffer(&patch, PATCH_BINARY_FILE_WITH_MISSING_PATHS,
strlen(PATCH_BINARY_FILE_WITH_MISSING_PATHS), NULL));
}
void test_patch_parse__memory_leak_on_multiple_paths(void) void test_patch_parse__memory_leak_on_multiple_paths(void)
{ {
git_patch *patch; git_patch *patch;
......
...@@ -906,6 +906,12 @@ ...@@ -906,6 +906,12 @@
"+bb\n" \ "+bb\n" \
" c\n" " c\n"
#define PATCH_BINARY_FILE_WITH_MISSING_PATHS \
"diff --git \n" \
"--- \n" \
"+++ \n" \
"Binary files "
#define PATCH_MULTIPLE_OLD_PATHS \ #define PATCH_MULTIPLE_OLD_PATHS \
"diff --git \n" \ "diff --git \n" \
"--- \n" \ "--- \n" \
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment