Skip to content

Commit e213563

Browse files
committed
HADOOP-15822. zstd compressor can fail with a small output buffer. Contributed by Jason Lowe.
(cherry picked from commit 8f97d6f) (cherry picked from commit 63396be)
1 parent 7031fe1 commit e213563

File tree

5 files changed

+15
-12
lines changed

5 files changed

+15
-12
lines changed

dev-support/docker/Dockerfile

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ RUN apt-get -q update \
6363
libsnappy-dev \
6464
libssl-dev \
6565
libtool \
66+
libzstd1-dev \
6667
locales \
6768
make \
6869
pinentry-curses \

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/compress/zstd/ZStandardDecompressor.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,8 @@ int inflateDirect(ByteBuffer src, ByteBuffer dst) throws IOException {
262262

263263
int originalPosition = dst.position();
264264
int n = inflateBytesDirect(
265-
src, src.position(), src.remaining(), dst, dst.position(),
266-
dst.remaining()
265+
src, src.position(), src.limit(), dst, dst.position(),
266+
dst.limit()
267267
);
268268
dst.position(originalPosition + n);
269269
if (bytesInCompressedBuffer > 0) {

hadoop-common-project/hadoop-common/src/main/native/src/org/apache/hadoop/io/compress/zstd/ZStandardCompressor.c

+7-4
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,13 @@ JNIEXPORT jint Java_org_apache_hadoop_io_compress_zstd_ZStandardCompressor_defla
195195
ZSTD_inBuffer input = { uncompressed_bytes, uncompressed_direct_buf_len, uncompressed_direct_buf_off };
196196
ZSTD_outBuffer output = { compressed_bytes, compressed_direct_buf_len, 0 };
197197

198-
size_t size = dlsym_ZSTD_compressStream(stream, &output, &input);
199-
if (dlsym_ZSTD_isError(size)) {
200-
THROW(env, "java/lang/InternalError", dlsym_ZSTD_getErrorName(size));
201-
return (jint) 0;
198+
size_t size;
199+
if (uncompressed_direct_buf_len != 0) {
200+
size = dlsym_ZSTD_compressStream(stream, &output, &input);
201+
if (dlsym_ZSTD_isError(size)) {
202+
THROW(env, "java/lang/InternalError", dlsym_ZSTD_getErrorName(size));
203+
return (jint) 0;
204+
}
202205
}
203206
if (finish && input.pos == input.size) {
204207
// end the stream, flush and write the frame epilogue

hadoop-common-project/hadoop-common/src/main/native/src/org/apache/hadoop/io/compress/zstd/ZStandardDecompressor.c

+1
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ JNIEXPORT jint JNICALL Java_org_apache_hadoop_io_compress_zstd_ZStandardDecompre
178178
return (jint) 0;
179179
}
180180
uncompressed_bytes = ((char*) uncompressed_bytes) + uncompressed_direct_buf_off;
181+
uncompressed_direct_buf_len -= uncompressed_direct_buf_off;
181182

182183
ZSTD_inBuffer input = { compressed_bytes, compressed_direct_buf_len, compressed_direct_buf_off };
183184
ZSTD_outBuffer output = { uncompressed_bytes, uncompressed_direct_buf_len, 0 };

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/compress/zstd/TestZStandardCompressorDecompressor.java

+4-6
Original file line numberDiff line numberDiff line change
@@ -414,13 +414,11 @@ private void compressDecompressLoop(int rawDataSize) throws IOException {
414414
outBuf.clear();
415415
while (!decompressor.finished()) {
416416
decompressor.decompress(inBuf, outBuf);
417-
if (outBuf.remaining() == 0) {
418-
outBuf.flip();
419-
while (outBuf.remaining() > 0) {
420-
assertEquals(expected.get(), outBuf.get());
421-
}
422-
outBuf.clear();
417+
outBuf.flip();
418+
while (outBuf.remaining() > 0) {
419+
assertEquals(expected.get(), outBuf.get());
423420
}
421+
outBuf.clear();
424422
}
425423
outBuf.flip();
426424
while (outBuf.remaining() > 0) {

0 commit comments

Comments
 (0)