Commit 841ddc10 authored by Bo Liu's avatar Bo Liu Committed by Commit Bot

android: Fix rank comparator overflow

Native can pass up std::numeric_limits<unsigned int>::max() as the frame
depth. We store this in a long in java which does not overflow. However
comparator currently does substracts two frame depths and casts it down
to int to get the sign. This can cause int overflow and change the
result. Use Long.signum instead.

Change-Id: Id0591250d4ae588bd3f54964fd53e3211402133a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1610083Reviewed-by: default avatarssid <ssid@chromium.org>
Commit-Queue: Bo <boliu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#659779}
parent c5bb7de6
......@@ -66,7 +66,7 @@ public class ChildProcessRanking implements Iterable<ChildProcessConnection> {
return 1;
}
return (int) (o1.frameDepth - o2.frameDepth);
return Long.signum(o1.frameDepth - o2.frameDepth);
}
@Override
......
......@@ -229,6 +229,25 @@ public class ChildProcessRankingTest {
assertInGroupOrderedByImportance(new ChildProcessConnection[] {c1});
}
@Test
public void testFrameDepthIntOverflow() {
ChildProcessConnection c1 = createConnection();
ChildProcessConnection c2 = createConnection();
ChildProcessConnection c3 = createConnection();
ChildProcessRanking ranking = new ChildProcessRanking();
// Native can pass up the maximum value of unsigned int.
long intOverflow = ((long) Integer.MAX_VALUE) * 2;
ranking.addConnection(c3, true /* foreground */, intOverflow - 1 /* frameDepth */,
true /* intersectsViewport */, ChildProcessImportance.NORMAL);
ranking.addConnection(c2, true /* foreground */, 10 /* frameDepth */,
true /* intersectsViewport */, ChildProcessImportance.NORMAL);
ranking.addConnection(c1, true /* foreground */, intOverflow /* frameDepth */,
true /* intersectsViewport */, ChildProcessImportance.NORMAL);
assertRankingAndRemoveAll(ranking, new ChildProcessConnection[] {c2, c3, c1});
}
@Test
public void testThrowExceptionWhenGoingOverLimit() {
ChildProcessRanking ranking = new ChildProcessRanking(2);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment