This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-imaging.git
commit c1ba8734d312208698639baa11568dfdc6610aeb Author: Gary D. Gregory <garydgreg...@gmail.com> AuthorDate: Sat Apr 26 17:34:45 2025 -0400 Fix SpotBugs CT_CONSTRUCTOR_THROW - Use "Compliant Solution (Java SE 6, Public and Private Constructors)" from https://wiki.sei.cmu.edu/confluence/display/java/OBJ11-J.+Be+wary+of+letting+constructors+throw+exceptions --- .../imaging/formats/webp/chunks/WebPChunk.java | 37 ++++++++++++---------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/apache/commons/imaging/formats/webp/chunks/WebPChunk.java b/src/main/java/org/apache/commons/imaging/formats/webp/chunks/WebPChunk.java index 4cdb86ae..75295b80 100644 --- a/src/main/java/org/apache/commons/imaging/formats/webp/chunks/WebPChunk.java +++ b/src/main/java/org/apache/commons/imaging/formats/webp/chunks/WebPChunk.java @@ -32,11 +32,30 @@ import org.apache.commons.imaging.internal.SafeOperations; * @since 1.0.0-alpha4 */ public abstract class WebPChunk extends BinaryFileParser { + + private static boolean checkArgs(final int size, final byte[] bytes) throws ImagingException { + if (size != bytes.length) { + throw new ImagingException("Chunk size must match bytes length"); + } + return true; + } + private final int type; private final int size; protected final byte[] bytes; private final int chunkSize; + private WebPChunk(final int type, final int size, final byte[] bytes, final boolean ignored) { + super(ByteOrder.LITTLE_ENDIAN); + this.type = type; + this.size = bytes.length; + this.bytes = bytes; + // if chunk size is odd, a single padding byte is added + final int padding = size % 2 != 0 ? 1 : 0; + // Chunk FourCC (4 bytes) + Chunk Size (4 bytes) + Chunk Payload (n bytes) + Padding + this.chunkSize = SafeOperations.add(4, 4, size, padding); + } + /** * Create a new WebP chunk. * @@ -45,22 +64,8 @@ public abstract class WebPChunk extends BinaryFileParser { * @param bytes chunk data. * @throws ImagingException if the chunk data and the size provided do not match. */ - WebPChunk(final int type, final int size, final byte[] bytes) throws ImagingException { - super(ByteOrder.LITTLE_ENDIAN); - - if (size != bytes.length) { - throw new ImagingException("Chunk size must match bytes length"); - } - - this.type = type; - this.size = size; - this.bytes = bytes; - - // if chunk size is odd, a single padding byte is added - final int padding = size % 2 != 0 ? 1 : 0; - - // Chunk FourCC (4 bytes) + Chunk Size (4 bytes) + Chunk Payload (n bytes) + Padding - this.chunkSize = SafeOperations.add(4, 4, size, padding); + public WebPChunk(final int type, final int size, final byte[] bytes) throws ImagingException { + this(type, size, bytes, checkArgs(size, bytes)); } /**