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
The following commit(s) were added to refs/heads/master by this push: new 4e608718 Use a configurable limit to avoid OOME on too large or broken input 4e608718 is described below commit 4e60871832e17a43d01d46d8146a6364bfce7ba5 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Sat May 13 09:22:07 2023 -0400 Use a configurable limit to avoid OOME on too large or broken input --- .../imaging/common/mylzw/AllocationChecker.java | 48 ++++++++++++++++++++++ .../imaging/common/mylzw/MyLzwDecompressor.java | 2 +- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/apache/commons/imaging/common/mylzw/AllocationChecker.java b/src/main/java/org/apache/commons/imaging/common/mylzw/AllocationChecker.java new file mode 100644 index 00000000..7d61d03f --- /dev/null +++ b/src/main/java/org/apache/commons/imaging/common/mylzw/AllocationChecker.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.imaging.common.mylzw; + +/** + * Checks inputs for meeting allocation limits. + */ +class AllocationChecker { + + private static final String CANONICAL_NAME = AllocationChecker.class.getCanonicalName(); + + /** One GB. */ + private static final int DEFAULT = 1_073_741_824; + + /** + * Checks a request for meeting allocation limits. + * <p> + * The default limit is {@value #DEFAULT}, override with the system property "org.apache.commons.imaging.common.mylzw.AllocationChecker". + * </p> + * + * @param request an allocation request. + * @return the request. + */ + static int check(final int request) { + // 1 GB limit + final int limit = Integer.getInteger(CANONICAL_NAME, DEFAULT); + if (request > limit) { + throw new IllegalArgumentException(String.format("Allocation limit %,d exceeded: %,d", limit, request)); + } + return request; + } + +} diff --git a/src/main/java/org/apache/commons/imaging/common/mylzw/MyLzwDecompressor.java b/src/main/java/org/apache/commons/imaging/common/mylzw/MyLzwDecompressor.java index b95d6277..a7023641 100644 --- a/src/main/java/org/apache/commons/imaging/common/mylzw/MyLzwDecompressor.java +++ b/src/main/java/org/apache/commons/imaging/common/mylzw/MyLzwDecompressor.java @@ -111,7 +111,7 @@ public final class MyLzwDecompressor { mbis.setTiffLZWMode(); } - final ByteArrayOutputStream baos = new ByteArrayOutputStream(expectedLength); + final ByteArrayOutputStream baos = new ByteArrayOutputStream(AllocationChecker.check(expectedLength)); clearTable();