From 9d06b25ecd7dfaa88f43526f29a3eea571d94e46 Mon Sep 17 00:00:00 2001 From: Angus He Date: Fri, 3 Dec 2010 16:52:27 +0800 Subject: [PATCH] bugfix: prevent LzoDecompressor from entering endless loop when a empty lzo file comes. Signed-off-by: Todd Lipcon --- .../compression/lzo/LzoDecompressor.java | 24 ++++++++---------- .../compression/lzo/TestLzopInputStream.java | 9 +++++++ src/test/data/0.txt | 0 src/test/data/0.txt.lzo | Bin 0 -> 47 bytes 4 files changed, 20 insertions(+), 13 deletions(-) create mode 100644 src/test/data/0.txt create mode 100644 src/test/data/0.txt.lzo diff --git a/src/java/com/hadoop/compression/lzo/LzoDecompressor.java b/src/java/com/hadoop/compression/lzo/LzoDecompressor.java index 7e0f1c59..ad3e95ca 100644 --- a/src/java/com/hadoop/compression/lzo/LzoDecompressor.java +++ b/src/java/com/hadoop/compression/lzo/LzoDecompressor.java @@ -296,21 +296,19 @@ public synchronized int decompress(byte[] b, int off, int len) } // Check if there is data to decompress - if (compressedDirectBufLen <= 0) { - return 0; - } - - // Re-initialize the lzo's output direct-buffer - uncompressedDirectBuf.rewind(); - uncompressedDirectBuf.limit(directBufferSize); + if (compressedDirectBufLen > 0) { + // Re-initialize the lzo's output direct-buffer + uncompressedDirectBuf.rewind(); + uncompressedDirectBuf.limit(directBufferSize); - // Decompress data - numBytes = decompressBytesDirect(strategy.getDecompressor()); - uncompressedDirectBuf.limit(numBytes); + // Decompress data + numBytes = decompressBytesDirect(strategy.getDecompressor()); + uncompressedDirectBuf.limit(numBytes); - // Return atmost 'len' bytes - numBytes = Math.min(numBytes, len); - ((ByteBuffer)uncompressedDirectBuf).get(b, off, numBytes); + // Return atmost 'len' bytes + numBytes = Math.min(numBytes, len); + ((ByteBuffer)uncompressedDirectBuf).get(b, off, numBytes); + } } // Set 'finished' if lzo has consumed all user-data diff --git a/src/test/com/hadoop/compression/lzo/TestLzopInputStream.java b/src/test/com/hadoop/compression/lzo/TestLzopInputStream.java index 0249e09c..c23b090b 100644 --- a/src/test/com/hadoop/compression/lzo/TestLzopInputStream.java +++ b/src/test/com/hadoop/compression/lzo/TestLzopInputStream.java @@ -43,6 +43,7 @@ public class TestLzopInputStream extends TestCase { private final String bigFile = "100000.txt"; private final String mediumFile = "1000.txt"; private final String smallFile = "100.txt"; + private final String emptyFile = "0.txt"; @Override protected void setUp() throws Exception { @@ -81,6 +82,14 @@ public void testSmallFile() throws NoSuchAlgorithmException, IOException, runTest(smallFile); } + /** + * Test against a 0 line file. + */ + public void testEmptyFile() throws NoSuchAlgorithmException, IOException, + InterruptedException { + runTest(emptyFile); + } + /** * Test that reading an lzo-compressed file produces the same lines as reading the equivalent * flat file. The test opens both the compressed and flat file, successively reading each diff --git a/src/test/data/0.txt b/src/test/data/0.txt new file mode 100644 index 00000000..e69de29b diff --git a/src/test/data/0.txt.lzo b/src/test/data/0.txt.lzo new file mode 100644 index 0000000000000000000000000000000000000000..27bfb388df3890f645b177445bcc64decbb67094 GIT binary patch literal 47 ycmeD5iSlRQ<&xqOP*5=7bYNs