[ 
https://issues.apache.org/jira/browse/HADOOP-19908?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18085434#comment-18085434
 ] 

ASF GitHub Bot commented on HADOOP-19908:
-----------------------------------------

pan3793 commented on code in PR #8526:
URL: https://github.com/apache/hadoop/pull/8526#discussion_r3339674503


##########
hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/compress/zstd/TestZStandardCompressorDecompressor.java:
##########
@@ -557,6 +557,64 @@ public void testDecompressReturnsWhenNothingToDecompress() 
throws Exception {
     assertEquals(0, result);
   }
 
+  /**
+   * Verify that {@code setInput()} does not throw {@code 
BufferOverflowException}
+   * after a previous {@code decompress()} call threw an exception.
+   *
+   * <p>When {@code decompress()} processes compressed data, it sets
+   * {@code compressedDirectBuf.limit(bytesInCompressedBuffer)} — a value that
+   * may be smaller than {@code directBufferSize}. If {@code 
decompressDirectByteBufferStream}
+   * throws (e.g. on corrupted input), the limit is never restored. A 
subsequent
+   * {@code reset()} also does not restore {@code compressedDirectBuf.limit}.
+   * So the next {@code setInput()} call will hit {@code 
BufferOverflowException}
+   * because {@code setInputFromSavedData()} tries to {@code put()} more bytes
+   * than the current limit allows.</p>
+   *
+   * <p>This scenario occurs in practice when reading multiple zstd-compressed
+   * files from a directory: a corrupted file causes an exception 
mid-decompress,
+   * the decompressor is returned to the pool and reset, but the limit stays
+   * small. The next file's {@code setInput()} then fails.</p>
+   */
+  @Test
+  public void testSetInputAfterDecompressThrowsOnCorruptedData() throws 
Exception {
+    byte[] rawData = generate(400);
+    int bufSize = IO_FILE_BUFFER_SIZE_DEFAULT;
+
+    ByteArrayOutputStream baos = new ByteArrayOutputStream();
+    try (CompressionOutputStream cos = new CompressorStream(baos,
+        new ZStandardCompressor(), bufSize)) {
+      cos.write(rawData);
+    }
+    byte[] compressed = baos.toByteArray();
+
+    // Corrupt the compressed data by dropping the first 10 bytes.
+    byte[] corrupted = new byte[compressed.length - 10];
+    System.arraycopy(compressed, 10, corrupted, 0, corrupted.length);
+
+    ZStandardDecompressor decompressor = new ZStandardDecompressor(bufSize);
+    byte[] out = new byte[bufSize];
+
+    // Feed corrupted data — decompress() sets limit to corrupted.length, then 
throws.
+    decompressor.setInput(corrupted, 0, corrupted.length);
+    try {
+      decompressor.decompress(out, 0, out.length);

Review Comment:
   added `fail("decompress should throw exception on corrupted data");`





> ZStandardDecompressor should reset compressedDirectBuf correctly
> ----------------------------------------------------------------
>
>                 Key: HADOOP-19908
>                 URL: https://issues.apache.org/jira/browse/HADOOP-19908
>             Project: Hadoop Common
>          Issue Type: Bug
>          Components: compress
>    Affects Versions: 3.5.1
>            Reporter: Cheng Pan
>            Priority: Major
>              Labels: pull-request-available
>




--
This message was sent by Atlassian Jira
(v8.20.10#820010)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to