Skip to content

Commit 04ae000

Browse files
szederttaylorr
authored andcommitted
line-log: free diff queue when processing non-merge commits
When processing a non-merge commit, the line-level log first asks the tree-diff machinery whether any of the files in the given line ranges were modified between the current commit and its parent, and if some of them were, then it loads the contents of those files from both commits to see whether their line ranges were modified and/or need to be adjusted. Alas, it doesn't free() the diff queue holding the results of that query and the contents of those files once its done. This can add up to a substantial amount of leaked memory, especially when the file in question is big and is frequently modified: a user reported "Out of memory, malloc failed" errors with a 2MB text file that was modified ~2800 times [1] (I estimate the leak would use up almost 11GB memory in that case). Free that diff queue to plug this memory leak. However, instead of simply open-coding the necessary three lines, add them as a helper function to the diff API, because it will be useful elsewhere as well. [1] https://public-inbox.org/git/CAFOPqVXz2XwzX8vGU7wLuqb2ZuwTuOFAzBLRM_QPk+NJa=eC-g@mail.gmail.com/ Signed-off-by: SZEDER Gábor <[email protected]> Signed-off-by: Taylor Blau <[email protected]>
1 parent c03801e commit 04ae000

File tree

3 files changed

+9
-0
lines changed

3 files changed

+9
-0
lines changed

diff.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5773,6 +5773,13 @@ void diff_free_filepair(struct diff_filepair *p)
57735773
free(p);
57745774
}
57755775

5776+
void diff_free_queue(struct diff_queue_struct *q)
5777+
{
5778+
for (int i = 0; i < q->nr; i++)
5779+
diff_free_filepair(q->queue[i]);
5780+
free(q->queue);
5781+
}
5782+
57765783
const char *diff_aligned_abbrev(const struct object_id *oid, int len)
57775784
{
57785785
int abblen;

diffcore.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ struct diff_filepair *diff_queue(struct diff_queue_struct *,
162162
struct diff_filespec *,
163163
struct diff_filespec *);
164164
void diff_q(struct diff_queue_struct *, struct diff_filepair *);
165+
void diff_free_queue(struct diff_queue_struct *q);
165166

166167
/* dir_rename_relevance: the reason we want rename information for a dir */
167168
enum dir_rename_relevance {

line-log.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,7 @@ static int process_ranges_ordinary_commit(struct rev_info *rev, struct commit *c
11951195
if (parent)
11961196
add_line_range(rev, parent, parent_range);
11971197
free_line_log_data(parent_range);
1198+
diff_free_queue(&queue);
11981199
return changed;
11991200
}
12001201

0 commit comments

Comments
 (0)