Plug diff leaks.
It is a bit embarrassing that it took this long for a fix since the problem was first reported on Aug 13th. Message-ID: <87y876gl1r.wl@mail2.atmark-techno.com> From: Yasushi SHOJI <yashi@atmark-techno.com> Newsgroups: gmane.comp.version-control.git Subject: [patch] possible memory leak in diff.c::diff_free_filepair() Date: Sat, 13 Aug 2005 19:58:56 +0900 This time I used valgrind to make sure that it does not overeagerly discard memory that is still being used. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Sep 15, 2005 at 16:13 UTC
5098bafb756de69d03882707a3382899c0cb7dd1
3 files changed
+37
-25
diff-tree.c
+1
@@ -370,6 +370,7 @@ static int diff_tree_commit(const unsigned char *commit, const char *name)
370
}
371
offset += 48;
372
}
373
+ free(buf);
374
return 0;
375
}
376
diff.c
+4
-1
@@ -762,7 +762,8 @@ struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
762
dp->status = 0;
763
dp->source_stays = 0;
764
dp->broken_pair = 0;
765
- diff_q(queue, dp);
765
+ if (queue)
766
+ diff_q(queue, dp);
767
return dp;
768
}
769
@@ -770,6 +771,8 @@ void diff_free_filepair(struct diff_filepair *p)
771
{
772
diff_free_filespec_data(p->one);
773
diff_free_filespec_data(p->two);
774
+ free(p->one);
775
+ free(p->two);
776
free(p);
777
}
778
diffcore-rename.c
+32
-24
@@ -47,7 +47,8 @@ static struct diff_rename_dst *locate_rename_dst(struct diff_filespec *two,
47
if (first < rename_dst_nr)
48
memmove(rename_dst + first + 1, rename_dst + first,
49
(rename_dst_nr - first - 1) * sizeof(*rename_dst));
50
- rename_dst[first].two = two;
50
+ rename_dst[first].two = alloc_filespec(two->path);
51
+ fill_filespec(rename_dst[first].two, two->sha1, two->mode);
52
rename_dst[first].pair = NULL;
53
return &(rename_dst[first]);
54
}
@@ -201,8 +202,7 @@ static int estimate_similarity(struct diff_filespec *src,
202
return score;
203
}
204
204
-static void record_rename_pair(struct diff_queue_struct *renq,
205
- int dst_index, int src_index, int score)
205
+static void record_rename_pair(int dst_index, int src_index, int score)
206
{
207
struct diff_filespec *one, *two, *src, *dst;
208
struct diff_filepair *dp;
@@ -218,7 +218,7 @@ static void record_rename_pair(struct diff_queue_struct *renq,
218
two = alloc_filespec(dst->path);
219
fill_filespec(two, dst->sha1, dst->mode);
220
221
- dp = diff_queue(renq, one, two);
221
+ dp = diff_queue(NULL, one, two);
222
dp->score = score;
223
dp->source_stays = rename_src[src_index].src_path_left;
224
rename_dst[dst_index].pair = dp;
@@ -252,15 +252,14 @@ static int compute_stays(struct diff_queue_struct *q,
252
void diffcore_rename(int detect_rename, int minimum_score)
253
{
254
struct diff_queue_struct *q = &diff_queued_diff;
255
- struct diff_queue_struct renq, outq;
255
+ struct diff_queue_struct outq;
256
struct diff_score *mx;
257
- int i, j;
257
+ int i, j, rename_count;
258
int num_create, num_src, dst_cnt;
259
260
if (!minimum_score)
261
minimum_score = DEFAULT_RENAME_SCORE;
262
- renq.queue = NULL;
263
- renq.nr = renq.alloc = 0;
262
+ rename_count = 0;
263
264
for (i = 0; i < q->nr; i++) {
265
struct diff_filepair *p = q->queue[i];
@@ -292,19 +291,19 @@ void diffcore_rename(int detect_rename, int minimum_score)
291
struct diff_filespec *one = rename_src[j].one;
292
if (!is_exact_match(one, two))
293
continue;
295
- record_rename_pair(&renq, i, j, MAX_SCORE);
294
+ record_rename_pair(i, j, MAX_SCORE);
295
+ rename_count++;
296
break; /* we are done with this entry */
297
}
298
}
299
- diff_debug_queue("done detecting exact", &renq);
299
300
/* Have we run out the created file pool? If so we can avoid
301
* doing the delta matrix altogether.
302
*/
304
- if (renq.nr == rename_dst_nr)
303
+ if (rename_count == rename_dst_nr)
304
goto cleanup;
305
307
- num_create = (rename_dst_nr - renq.nr);
306
+ num_create = (rename_dst_nr - rename_count);
307
num_src = rename_src_nr;
308
mx = xmalloc(sizeof(*mx) * num_create * num_src);
309
for (dst_cnt = i = 0; i < rename_dst_nr; i++) {
@@ -330,14 +329,14 @@ void diffcore_rename(int detect_rename, int minimum_score)
329
continue; /* already done, either exact or fuzzy. */
330
if (mx[i].score < minimum_score)
331
break; /* there is no more usable pair. */
333
- record_rename_pair(&renq, mx[i].dst, mx[i].src, mx[i].score);
332
+ record_rename_pair(mx[i].dst, mx[i].src, mx[i].score);
333
+ rename_count++;
334
}
335
free(mx);
336
- diff_debug_queue("done detecting fuzzy", &renq);
336
337
cleanup:
338
/* At this point, we have found some renames and copies and they
340
- * are kept in renq. The original list is still in *q.
339
+ * are recorded in rename_dst. The original list is still in *q.
340
*/
341
outq.queue = NULL;
342
outq.nr = outq.alloc = 0;
@@ -372,9 +371,9 @@ void diffcore_rename(int detect_rename, int minimum_score)
371
*
372
* (1) this is a broken delete and the counterpart
373
* broken create remains in the output; or
375
- * (2) this is not a broken delete, and renq does
376
- * not have a rename/copy to move p->one->path
377
- * out.
374
+ * (2) this is not a broken delete, and rename_dst
375
+ * does not have a rename/copy to move p->one->path
376
+ * out of existence.
377
*
378
* Otherwise, the counterpart broken create
379
* has been turned into a rename-edit; or
@@ -390,11 +389,16 @@ void diffcore_rename(int detect_rename, int minimum_score)
389
pair_to_free = p;
390
}
391
else {
393
- for (j = 0; j < renq.nr; j++)
394
- if (!strcmp(renq.queue[j]->one->path,
395
- p->one->path))
396
- break;
397
- if (j < renq.nr)
392
+ for (j = 0; j < rename_dst_nr; j++) {
393
+ if (!rename_dst[j].pair)
394
+ continue;
395
+ if (strcmp(rename_dst[j].pair->
396
+ one->path,
397
+ p->one->path))
398
+ continue;
399
+ break;
400
+ }
401
+ if (j < rename_dst_nr)
402
/* this path remains */
403
pair_to_free = p;
404
}
@@ -416,7 +420,6 @@ void diffcore_rename(int detect_rename, int minimum_score)
420
}
421
diff_debug_queue("done copying original", &outq);
422
419
- free(renq.queue);
423
free(q->queue);
424
*q = outq;
425
diff_debug_queue("done collapsing", q);
@@ -438,6 +441,11 @@ void diffcore_rename(int detect_rename, int minimum_score)
441
}
442
}
443
444
+ for (i = 0; i < rename_dst_nr; i++) {
445
+ diff_free_filespec_data(rename_dst[i].two);
446
+ free(rename_dst[i].two);
447
+ }
448
+
449
free(rename_dst);
450
rename_dst = NULL;
451
rename_dst_nr = rename_dst_alloc = 0;