Skip to content

Commit 61f8839

Browse files
phillipwoodgitster
authored andcommitted
xdiff: handle allocation failure in patience diff
Other users of libxdiff such as libgit2 need to be able to handle allocation failures. As NULL is a valid return value the function signature is changed to be able report allocation failures. Signed-off-by: Phillip Wood <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9df0fc3 commit 61f8839

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

xdiff/xpatience.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ static int binary_search(struct entry **sequence, int longest,
198198
* item per sequence length: the sequence with the smallest last
199199
* element (in terms of line2).
200200
*/
201-
static struct entry *find_longest_common_sequence(struct hashmap *map)
201+
static int find_longest_common_sequence(struct hashmap *map, struct entry **res)
202202
{
203203
struct entry **sequence = xdl_malloc(map->nr * sizeof(struct entry *));
204204
int longest = 0, i;
@@ -211,6 +211,9 @@ static struct entry *find_longest_common_sequence(struct hashmap *map)
211211
*/
212212
int anchor_i = -1;
213213

214+
if (!sequence)
215+
return -1;
216+
214217
for (entry = map->first; entry; entry = entry->next) {
215218
if (!entry->line2 || entry->line2 == NON_UNIQUE)
216219
continue;
@@ -230,8 +233,9 @@ static struct entry *find_longest_common_sequence(struct hashmap *map)
230233

231234
/* No common unique lines were found */
232235
if (!longest) {
236+
*res = NULL;
233237
xdl_free(sequence);
234-
return NULL;
238+
return 0;
235239
}
236240

237241
/* Iterate starting at the last element, adjusting the "next" members */
@@ -241,8 +245,9 @@ static struct entry *find_longest_common_sequence(struct hashmap *map)
241245
entry->previous->next = entry;
242246
entry = entry->previous;
243247
}
248+
*res = entry;
244249
xdl_free(sequence);
245-
return entry;
250+
return 0;
246251
}
247252

248253
static int match(struct hashmap *map, int line1, int line2)
@@ -358,14 +363,16 @@ static int patience_diff(mmfile_t *file1, mmfile_t *file2,
358363
return 0;
359364
}
360365

361-
first = find_longest_common_sequence(&map);
366+
result = find_longest_common_sequence(&map, &first);
367+
if (result)
368+
goto out;
362369
if (first)
363370
result = walk_common_sequence(&map, first,
364371
line1, count1, line2, count2);
365372
else
366373
result = fall_back_to_classic_diff(&map,
367374
line1, count1, line2, count2);
368-
375+
out:
369376
xdl_free(map.entries);
370377
return result;
371378
}

0 commit comments

Comments
 (0)