Skip to content

Commit c86b5e2

Browse files
committed
FindBugs detected potential calculation overflow
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1064292 13f79535-47bb-0310-9956-ffa450edef68
1 parent e7b46fd commit c86b5e2

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

src/java/org/apache/commons/codec/binary/BaseNCodec.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
import org.apache.commons.codec.DecoderException;
2424
import org.apache.commons.codec.EncoderException;
2525

26+
/**
27+
* Implements common Base-N codec functions.
28+
*
29+
* This class is not thread-safe.
30+
* Each thread should use its own instance.
31+
*/
2632
public abstract class BaseNCodec implements BinaryEncoder, BinaryDecoder {
2733

2834
/**
@@ -424,12 +430,13 @@ protected boolean containsAlphabetOrPad(byte[] arrayOctet) {
424430
*
425431
* @param pArray byte[] array which will later be encoded
426432
*
427-
* @return amount of space needed to encoded the supplied array. Returns
428-
* a long since a max-len array will require > Integer.MAX_VALUE
433+
* @return amount of space needed to encoded the supplied array.
434+
* Returns a long since a max-len array will require > Integer.MAX_VALUE
429435
*/
430436
public long getEncodedLength(byte[] pArray) {
431437
// Calculate non-chunked size - rounded up to allow for padding
432-
long len = ((pArray.length + unencodedBlockSize-1) / unencodedBlockSize) * encodedBlockSize;
438+
// cast to long is needed to avoid possibility of overflow
439+
long len = ((pArray.length + unencodedBlockSize-1) / unencodedBlockSize) * (long) encodedBlockSize;
433440
if (lineLength > 0) { // We're using chunking
434441
// Round up to nearest multiple
435442
len += ((len + lineLength-1) / lineLength) * chunkSeparatorLength;

0 commit comments

Comments
 (0)