Skip to content

Commit ad8a1be

Browse files
newrengitster
authored andcommitted
diffcore-rename: simplify limit check
diffcore-rename had two different checks of the form if ((a < limit || b < limit) && a * b <= limit * limit) This can be simplified to if (st_mult(a, b) <= st_mult(limit, limit)) which makes it clearer how we are checking for overflow, and makes it much easier to parse given the drop from 8 to 4 variable appearances. Signed-off-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 00b8ccc commit ad8a1be

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

diffcore-rename.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -447,12 +447,16 @@ static int too_many_rename_candidates(int num_destinations, int num_sources,
447447
* growing larger than a "rename_limit" square matrix, ie:
448448
*
449449
* num_destinations * num_sources > rename_limit * rename_limit
450+
*
451+
* We use st_mult() to check overflow conditions; in the
452+
* exceptional circumstance that size_t isn't large enough to hold
453+
* the multiplication, the system won't be able to allocate enough
454+
* memory for the matrix anyway.
450455
*/
451456
if (rename_limit <= 0)
452457
rename_limit = 32767;
453-
if ((num_destinations <= rename_limit || num_sources <= rename_limit) &&
454-
((uint64_t)num_destinations * (uint64_t)num_sources
455-
<= (uint64_t)rename_limit * (uint64_t)rename_limit))
458+
if (st_mult(num_destinations, num_sources)
459+
<= st_mult(rename_limit, rename_limit))
456460
return 0;
457461

458462
options->needed_rename_limit =
@@ -468,9 +472,8 @@ static int too_many_rename_candidates(int num_destinations, int num_sources,
468472
continue;
469473
limited_sources++;
470474
}
471-
if ((num_destinations <= rename_limit || limited_sources <= rename_limit) &&
472-
((uint64_t)num_destinations * (uint64_t)limited_sources
473-
<= (uint64_t)rename_limit * (uint64_t)rename_limit))
475+
if (st_mult(num_destinations, limited_sources)
476+
<= st_mult(rename_limit, rename_limit))
474477
return 2;
475478
return 1;
476479
}

0 commit comments

Comments
 (0)