Glavo commented on code in PR #254:
URL: https://github.com/apache/commons-imaging/pull/254#discussion_r1342188466
##########
src/main/java/org/apache/commons/imaging/formats/webp/WebPImageParser.java:
##########
@@ -277,13 +279,14 @@ public void close() throws IOException {
WebPChunk readChunk() throws ImagingException, IOException {
while (sizeCount < fileSize) {
- int type = read4Bytes("Chunk Type", is, "Not a Valid WebP
File", ByteOrder.LITTLE_ENDIAN);
- int payloadSize = read4Bytes("Chunk Size", is, "Not a Valid
WebP File", ByteOrder.LITTLE_ENDIAN);
+ int type = read4Bytes("Chunk Type", is, "Not a valid WebP
file", ByteOrder.LITTLE_ENDIAN);
+ int payloadSize = read4Bytes("Chunk Size", is, "Not a valid
WebP file", ByteOrder.LITTLE_ENDIAN);
if (payloadSize < 0) {
throw new ImagingException("Chunk Payload is too long:" +
payloadSize);
}
boolean padding = (payloadSize % 2) != 0;
- int chunkSize = payloadSize + 8 + (padding ? 1 : 0);
+ int extraPadding = Math.addExact(8, (padding ? 1 : 0));
Review Comment:
The maximum value of `extraPadding` is 9, so there is no risk of overflow.
Furthermore, the name `extraPadding ` is somewhat misleading, since
`padding` only refers to the padding at the end of the chunk.
##########
src/main/java/org/apache/commons/imaging/formats/webp/chunks/WebPChunk.java:
##########
@@ -61,22 +74,41 @@ public String getTypeDescription() {
(byte) ((type >> 24) & 0xff)}, StandardCharsets.UTF_8);
}
+ /**
+ * @return the payload size.
+ */
public int getPayloadSize() {
return size;
}
+ /**
+ * @return the chunk size.
+ */
public int getChunkSize() {
// if chunk size is odd, a single padding byte is added
int padding = (size % 2) != 0 ? 1 : 0;
// Chunk FourCC (4 bytes) + Chunk Size (4 bytes) + Chunk Payload (n
bytes) + Padding
- return 4 + 4 + size + padding;
+ int chunkFourCCandSize = 4 + 4;
+ int paddedSize = Math.addExact(size, padding);
+ return Math.addExact(chunkFourCCandSize, paddedSize);
Review Comment:
I think we should check chunk size in constructor instead of here, this
method should not throw exception.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]