Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static <T> T apply(final int request, final IntFunction<T> factory) {
* @see #check(int)
*/
public static <T> T[] array(final int request, final IntFunction<T[]> factory, final int eltShallowByteSize) {
check(request * eltShallowByteSize);
check(request, eltShallowByteSize);
return factory.apply(request);
}

Expand All @@ -76,7 +76,7 @@ public static <T> T[] array(final int request, final IntFunction<T[]> factory, f
* @see #check(int)
*/
public static <T> ArrayList<T> arrayList(final int request) {
check(24 + request * 4); // 4 bytes per element
check(request, Integer.BYTES); // 4 bytes per element
return apply(request, ArrayList::new);
}

Expand Down
37 changes: 37 additions & 0 deletions src/test/java/org/apache/commons/imaging/common/AllocatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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
*
* https://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;

import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.Test;

class AllocatorTest {

/** 107374183 * 40 overflows int to 24, slipping past the byte-cost limit. */
@Test
void testArrayOverflowIsRejected() {
assertThrows(AllocationRequestException.class, () -> Allocator.array(107374183, Object[]::new, 40));
}

/** 536870912 * Integer.BYTES overflows int to a negative value. */
@Test
void testArrayListOverflowIsRejected() {
assertThrows(AllocationRequestException.class, () -> Allocator.arrayList(536870912));
}
}