Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c5dda46

Browse files
committed
patch parse: dup the lines of the patches from the callers
1 parent fd6f2d7 commit c5dda46

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

src/patch.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,6 @@ int git_patch_get_line_in_hunk(
196196

197197
static void git_patch__free(git_patch *patch)
198198
{
199-
git_array_clear(patch->lines);
200-
git_array_clear(patch->hunks);
201-
202-
git__free((char *)patch->binary.old_file.data);
203-
git__free((char *)patch->binary.new_file.data);
204-
205199
if (patch->free_fn)
206200
patch->free_fn(patch);
207201
}

src/patch_diff.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ static void patch_diff_free(git_patch *p)
2525
{
2626
git_patch_diff *patch = (git_patch_diff *)p;
2727

28+
git_array_clear(patch->base.lines);
29+
git_array_clear(patch->base.hunks);
30+
31+
git__free((char *)patch->base.binary.old_file.data);
32+
git__free((char *)patch->base.binary.new_file.data);
33+
2834
git_diff_file_content__clear(&patch->ofile);
2935
git_diff_file_content__clear(&patch->nfile);
3036

src/patch_parse.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ static int parse_hunk_body(
523523
int newlines = hunk->hunk.new_lines;
524524

525525
for (;
526-
ctx->remain > 4 && (oldlines || newlines) &&
526+
ctx->remain > 4 && (oldlines || newlines) &&
527527
memcmp(ctx->line, "@@ -", 4) != 0;
528528
parse_advance_line(ctx)) {
529529

@@ -566,7 +566,9 @@ static int parse_hunk_body(
566566

567567
memset(line, 0x0, sizeof(git_diff_line));
568568

569-
line->content = ctx->line + prefix;
569+
line->content = git__strndup(ctx->line + prefix, ctx->line_len - prefix);
570+
GITERR_CHECK_ALLOC(line->content);
571+
570572
line->content_len = ctx->line_len - prefix;
571573
line->content_offset = ctx->content_len - ctx->remain;
572574
line->origin = origin;
@@ -939,10 +941,21 @@ static int check_patch(git_patch_parsed *patch)
939941
static void patch_parsed__free(git_patch *p)
940942
{
941943
git_patch_parsed *patch = (git_patch_parsed *)p;
944+
git_diff_line *line;
945+
size_t i;
942946

943947
if (!patch)
944948
return;
945949

950+
git_array_foreach(patch->base.lines, i, line)
951+
git__free((char *)line->content);
952+
953+
git__free((char *)patch->base.binary.old_file.data);
954+
git__free((char *)patch->base.binary.new_file.data);
955+
git_array_clear(patch->base.hunks);
956+
git_array_clear(patch->base.lines);
957+
git__free(patch->base.delta);
958+
946959
git__free(patch->old_prefix);
947960
git__free(patch->new_prefix);
948961
git__free(patch->header_old_path);
@@ -951,9 +964,6 @@ static void patch_parsed__free(git_patch *p)
951964
git__free(patch->rename_new_path);
952965
git__free(patch->old_path);
953966
git__free(patch->new_path);
954-
git_array_clear(patch->base.hunks);
955-
git_array_clear(patch->base.lines);
956-
git__free(patch->base.delta);
957967
git__free(patch);
958968
}
959969

0 commit comments

Comments
 (0)