Promote Integer to Long before multiplying to avoid numeric overflow (#5602)

This commit is contained in:
kezhenxu94 2020-10-02 19:53:48 +08:00 committed by GitHub
parent 235fbe729c
commit 0171ff6a01
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 5 additions and 4 deletions

View File

@ -27,7 +27,7 @@ import org.apache.skywalking.apm.commons.datacarrier.partition.IDataPartitioner;
public class Channels<T> {
private final QueueBuffer<T>[] bufferChannels;
private IDataPartitioner<T> dataPartitioner;
private BufferStrategy strategy;
private final BufferStrategy strategy;
private final long size;
public Channels(int channelSize, int bufferSize, IDataPartitioner<T> partitioner, BufferStrategy strategy) {
@ -36,12 +36,13 @@ public class Channels<T> {
bufferChannels = new QueueBuffer[channelSize];
for (int i = 0; i < channelSize; i++) {
if (BufferStrategy.BLOCKING.equals(strategy)) {
bufferChannels[i] = new ArrayBlockingQueueBuffer<T>(bufferSize, strategy);
bufferChannels[i] = new ArrayBlockingQueueBuffer<>(bufferSize, strategy);
} else {
bufferChannels[i] = new Buffer<T>(bufferSize, strategy);
bufferChannels[i] = new Buffer<>(bufferSize, strategy);
}
}
size = channelSize * bufferSize;
// noinspection PointlessArithmeticExpression
size = 1L * channelSize * bufferSize; // it's not pointless, it prevents numeric overflow before assigning an integer to a long
}
public boolean save(T data) {