Merge "Gracefully handle integer overflows." am: 0e0b61e92e am: aeddf47458

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1316217

Change-Id: I0bba17d0367ebf4e1c868a8cc7471366bc7a4408
This commit is contained in:
Junyu Lai
2020-07-29 01:55:23 +00:00
committed by Automerger Merge Worker
2 changed files with 36 additions and 46 deletions

View File

@@ -26,6 +26,7 @@ import static android.net.NetworkStatsHistory.DataStreamUtils.writeVarLongArray;
import static android.net.NetworkStatsHistory.Entry.UNKNOWN;
import static android.net.NetworkStatsHistory.ParcelUtils.readLongArray;
import static android.net.NetworkStatsHistory.ParcelUtils.writeLongArray;
import static android.net.NetworkUtils.multiplySafeByRational;
import static android.text.format.DateUtils.SECOND_IN_MILLIS;
import static com.android.internal.util.ArrayUtils.total;
@@ -364,11 +365,12 @@ public class NetworkStatsHistory implements Parcelable {
if (overlap <= 0) continue;
// integer math each time is faster than floating point
final long fracRxBytes = rxBytes * overlap / duration;
final long fracRxPackets = rxPackets * overlap / duration;
final long fracTxBytes = txBytes * overlap / duration;
final long fracTxPackets = txPackets * overlap / duration;
final long fracOperations = operations * overlap / duration;
final long fracRxBytes = multiplySafeByRational(rxBytes, overlap, duration);
final long fracRxPackets = multiplySafeByRational(rxPackets, overlap, duration);
final long fracTxBytes = multiplySafeByRational(txBytes, overlap, duration);
final long fracTxPackets = multiplySafeByRational(txPackets, overlap, duration);
final long fracOperations = multiplySafeByRational(operations, overlap, duration);
addLong(activeTime, i, overlap);
addLong(this.rxBytes, i, fracRxBytes); rxBytes -= fracRxBytes;
@@ -568,12 +570,24 @@ public class NetworkStatsHistory implements Parcelable {
if (overlap <= 0) continue;
// integer math each time is faster than floating point
if (activeTime != null) entry.activeTime += activeTime[i] * overlap / bucketSpan;
if (rxBytes != null) entry.rxBytes += rxBytes[i] * overlap / bucketSpan;
if (rxPackets != null) entry.rxPackets += rxPackets[i] * overlap / bucketSpan;
if (txBytes != null) entry.txBytes += txBytes[i] * overlap / bucketSpan;
if (txPackets != null) entry.txPackets += txPackets[i] * overlap / bucketSpan;
if (operations != null) entry.operations += operations[i] * overlap / bucketSpan;
if (activeTime != null) {
entry.activeTime += multiplySafeByRational(activeTime[i], overlap, bucketSpan);
}
if (rxBytes != null) {
entry.rxBytes += multiplySafeByRational(rxBytes[i], overlap, bucketSpan);
}
if (rxPackets != null) {
entry.rxPackets += multiplySafeByRational(rxPackets[i], overlap, bucketSpan);
}
if (txBytes != null) {
entry.txBytes += multiplySafeByRational(txBytes[i], overlap, bucketSpan);
}
if (txPackets != null) {
entry.txPackets += multiplySafeByRational(txPackets[i], overlap, bucketSpan);
}
if (operations != null) {
entry.operations += multiplySafeByRational(operations[i], overlap, bucketSpan);
}
}
return entry;
}