diff options
| author | Patrick Steinhardt <ps@pks.im> | 2019-10-19 15:42:54 +0200 |
|---|---|---|
| committer | Patrick Steinhardt <ps@pks.im> | 2019-10-19 17:02:53 +0200 |
| commit | 223e7e43efffdcab4da864413e70eff40e8ada46 (patch) | |
| tree | 75c191885996bd8f4a07dee3409bb920a20d9689 /src | |
| parent | b246bed5ab83035d8aef95f1b7ff10dd746db7cb (diff) | |
| download | libgit2-223e7e43efffdcab4da864413e70eff40e8ada46.tar.gz | |
patch_parse: reject patches with multiple old/new paths
It's currently possible to have patches with multiple old path name
headers. As we didn't check for this case, this resulted in a memory
leak when overwriting the old old path with the new old path because we
simply discarded the old pointer.
Instead of fixing this by free'ing the old pointer, we should reject
such patches altogether. It doesn't make any sense for the "---" or
"+++" markers to occur multiple times within a patch n the first place.
This also implicitly fixes the memory leak.
Diffstat (limited to 'src')
| -rw-r--r-- | src/patch_parse.c | 19 |
1 files changed, 17 insertions, 2 deletions
diff --git a/src/patch_parse.c b/src/patch_parse.c index 126918249..a71843277 100644 --- a/src/patch_parse.c +++ b/src/patch_parse.c @@ -91,10 +91,14 @@ done: static int parse_header_path(char **out, git_patch_parse_ctx *ctx) { git_buf path = GIT_BUF_INIT; - int error = parse_header_path_buf(&path, ctx, header_path_len(ctx)); + int error; + if ((error = parse_header_path_buf(&path, ctx, header_path_len(ctx))) < 0) + goto out; *out = git_buf_detach(&path); +out: + git_buf_dispose(&path); return error; } @@ -104,6 +108,12 @@ static int parse_header_git_oldpath( git_buf old_path = GIT_BUF_INIT; int error; + if (patch->old_path) { + error = git_parse_err("patch contains duplicate old path at line %"PRIuZ, + ctx->parse_ctx.line_num); + goto out; + } + if ((error = parse_header_path_buf(&old_path, ctx, ctx->parse_ctx.line_len - 1)) < 0) goto out; @@ -120,9 +130,14 @@ static int parse_header_git_newpath( git_buf new_path = GIT_BUF_INIT; int error; - if ((error = parse_header_path_buf(&new_path, ctx, ctx->parse_ctx.line_len - 1)) < 0) + if (patch->new_path) { + error = git_parse_err("patch contains duplicate new path at line %"PRIuZ, + ctx->parse_ctx.line_num); goto out; + } + if ((error = parse_header_path_buf(&new_path, ctx, ctx->parse_ctx.line_len - 1)) < 0) + goto out; patch->new_path = git_buf_detach(&new_path); out: |
