Skip to content

Commit

Permalink
Use long instead of int for thread indices
Browse files Browse the repository at this point in the history
This protects against an integer overflow which could occur for large key list size and large thread counts.

Regrettably, it's difficult to write a regression test for this scenario, as exercising this overflow requires lots of time and heap, so it would be a performance regression to our test suites.

Fixes bazelbuild#19445

PiperOrigin-RevId: 595420516
Change-Id: Ic0a475a6a273c50fe9895dd0852fa5b062859cb2
  • Loading branch information
c-parsons authored and bazel-io committed Jan 8, 2025
1 parent 09c29e3 commit a30bd1b
Showing 1 changed file with 8 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -274,18 +274,18 @@ protected void runInternal(ImmutableList<Pair<SkyKey, InvalidationType>> pending
MIN_TIME_FOR_LOGGING)) {
// To avoid contention and scheduling too many jobs for our #cpus, we start
// DEFAULT_THREAD_COUNT jobs, each processing a chunk of the pending visitations.
int listSize = pendingList.size();
int numThreads = min(DEFAULT_THREAD_COUNT, listSize);
for (int i = 0; i < numThreads; i++) {
int index = i;
long listSize = pendingList.size();
long numThreads = min(DEFAULT_THREAD_COUNT, listSize);
for (long i = 0; i < numThreads; i++) {
// Use long multiplication to avoid possible overflow, as numThreads * listSize might be
// larger than max int.
int startIndex = (int) ((i * listSize) / numThreads);
int endIndex = (int) (((i + 1) * listSize) / numThreads);
executor.execute(
() ->
visit(
Collections2.transform(
pendingList.subList(
(index * listSize) / numThreads,
((index + 1) * listSize) / numThreads),
Pair::getFirst),
pendingList.subList(startIndex, endIndex), Pair::getFirst),
InvalidationType.DELETED));
}
}
Expand Down

0 comments on commit a30bd1b

Please sign in to comment.