Skip to content

Commit 4998e93

Browse files
avargitster
authored andcommitted
range-diff: plug memory leak in common invocation
Create a public release_patch() version of the private free_patch() function added in 13b5af2 (apply: move libified code from builtin/apply.c to apply.{c,h}, 2016-04-22). Unlike the existing function this one doesn't free() the "struct patch" itself, so we can use it for variables on the stack. Use it in range-diff.c to fix a memory leak in common range-diff invocations, e.g.: git -P range-diff origin/master origin/next origin/seen Would emit several errors when compiled with SANITIZE=leak, but now runs cleanly. Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ef3fe21 commit 4998e93

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

apply.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,18 @@ static void free_fragment_list(struct fragment *list)
219219
}
220220
}
221221

222-
static void free_patch(struct patch *patch)
222+
void release_patch(struct patch *patch)
223223
{
224224
free_fragment_list(patch->fragments);
225225
free(patch->def_name);
226226
free(patch->old_name);
227227
free(patch->new_name);
228228
free(patch->result);
229+
}
230+
231+
static void free_patch(struct patch *patch)
232+
{
233+
release_patch(patch);
229234
free(patch);
230235
}
231236

apply.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ int parse_git_diff_header(struct strbuf *root,
173173
unsigned int size,
174174
struct patch *patch);
175175

176+
void release_patch(struct patch *patch);
177+
176178
/*
177179
* Some aspects of the apply behavior are controlled by the following
178180
* bits in the "options" parameter passed to apply_all_patches().

range-diff.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ static int read_patches(const char *range, struct string_list *list,
165165
patch.old_mode, patch.new_mode);
166166

167167
strbuf_addstr(&buf, " ##");
168+
release_patch(&patch);
168169
} else if (in_header) {
169170
if (starts_with(line, "Author: ")) {
170171
strbuf_addstr(&buf, " ## Metadata ##\n");

0 commit comments

Comments
 (0)