Copilot commented on code in PR #755: URL: https://github.com/apache/commons-compress/pull/755#discussion_r2604468456
########## src/test/java/org/apache/commons/compress/archivers/zip/ZipCompress713Test.java: ########## @@ -0,0 +1,57 @@ +/* + * 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.compress.archivers.zip; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.io.ByteArrayInputStream; +import java.io.EOFException; +import java.io.IOException; + +import org.apache.commons.compress.CompressException; +import org.apache.commons.io.function.IOConsumer; +import org.junit.jupiter.api.Test; + +/** + * Tests https://issues.apache.org/jira/browse/COMPRESS-713 + */ +public class ZipCompress713Test { + + @Test + public void testIllegalArrayIndex() throws IOException { + final byte[] data = { 80, 75, 3, 4, 19, 7, 0, 1, 1, 0, -1, 1, 120, 8, 84, -99, 3, 48, 45, 1, 119, -70, 110, 61, 65, 104, 0, 0, 0, 0, 59, -4, -1, -1, -1, + -1, -33, 0, -1, 0, 5, 0, -1, -1, -1, -1, -1, -1, -1, 0, 122 }; + try (ZipArchiveInputStream inputStream = new ZipArchiveInputStream(new ByteArrayInputStream(data))) { + inputStream.getNextEntry(); + // Either the compressor fails properly or the archiver intercepts the unchecked exception + assertThrows(CompressException.class, () -> inputStream.read(new byte[1024])); Review Comment: Method execute ignores exceptional return value of FilterInputStream.read. ########## src/test/java/org/apache/commons/compress/archivers/zip/UnshrinkingInputStreamTest.java: ########## @@ -0,0 +1,79 @@ +/* + * 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.compress.archivers.zip; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +import org.apache.commons.compress.compressors.CompressorException; +import org.junit.jupiter.api.Test; +import org.junitpioneer.jupiter.Issue; + +class UnshrinkingInputStreamTest { + + private static final int CONTROL_CODE = 256; + private static final int CLEAR_SUBCODE = 2; + private static final int NEW_CODE = 257; + + /** + * Encodes a sequence of 9-bit codes into bytes, LSB-first, as used by + * PKZIP's “Shrunk” (Unshrink) method. + */ + private static byte[] encodeLsbFirst9Bit(final int... codes) { + final ByteArrayOutputStream out = new ByteArrayOutputStream(); + int bitBuffer = 0; + int bitCount = 0; + + for (int code : codes) { + bitBuffer |= (code & 0x1FF) << bitCount; + bitCount += 9; + + while (bitCount >= 8) { + out.write(bitBuffer & 0xFF); + bitBuffer >>>= 8; + bitCount -= 8; + } + } + + if (bitCount > 0) { + out.write(bitBuffer & 0xFF); + } + return out.toByteArray(); + } + + @Test + @Issue("COMPRESS-713") + public void testCompress713() throws IOException { + final byte[] data = encodeLsbFirst9Bit( + 'A', + 'B', // defines 257 = "AB" + NEW_CODE, // 257 ("AB") + CONTROL_CODE, // 256 (control) + CLEAR_SUBCODE, // subcode 2 (partial clear) + NEW_CODE // 257 again, now undefined and treated as prevCode + firstChar(prevCode) + ); + try (UnshrinkingInputStream inputStream = new UnshrinkingInputStream(new ByteArrayInputStream(data))) { + assertThrows(CompressorException.class, () -> inputStream.read(new byte[1024])); Review Comment: Method execute ignores exceptional return value of InputStream.read. -- 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]
