Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix corruption for large RLE blocks #98

Merged
merged 3 commits into from Jun 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -94,7 +94,6 @@ else if (sequenceCount < LONG_NUMBER_OF_SEQUENCES) {
// flags for FSE encoding type
long headerAddress = output++;

Histogram histogram;
int maxSymbol;
int largestCount;

Expand Down
Expand Up @@ -272,7 +272,7 @@ private static int encodeLiterals(
{
// TODO: move this to Strategy
boolean bypassCompression = (parameters.getStrategy() == CompressionParameters.Strategy.FAST) && (parameters.getTargetLength() > 0);
if (bypassCompression | literalsSize <= MINIMUM_LITERALS_SIZE) {
if (bypassCompression || literalsSize <= MINIMUM_LITERALS_SIZE) {
return rawLiterals(outputBase, outputAddress, outputSize, literals, ARRAY_BYTE_BASE_OFFSET, literalsSize);
}

Expand Down Expand Up @@ -391,10 +391,10 @@ private static int rleLiterals(Object outputBase, long outputAddress, int output
UNSAFE.putByte(outputBase, outputAddress, (byte) (RLE_LITERALS_BLOCK | (inputSize << 3)));
break;
case 2: // 2 - 2 - 12
UNSAFE.putShort(outputBase, outputAddress, (byte) (RLE_LITERALS_BLOCK | (1 << 2) | (inputSize << 4)));
UNSAFE.putShort(outputBase, outputAddress, (short) (RLE_LITERALS_BLOCK | (1 << 2) | (inputSize << 4)));
break;
case 3: // 2 - 2 - 20
UNSAFE.putInt(outputBase, outputAddress, (byte) (RLE_LITERALS_BLOCK | (3 << 2) | (inputSize << 4)));
UNSAFE.putInt(outputBase, outputAddress, RLE_LITERALS_BLOCK | 3 << 2 | inputSize << 4);
break;
default: // impossible. headerSize is {1,2,3}
throw new IllegalStateException();
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/io/airlift/compress/zstd/TestZstd.java
Expand Up @@ -110,4 +110,24 @@ public void testSmallLiteralsAfterIncompressibleLiterals()

assertByteArraysEqual(original, 0, original.length, decompressed, 0, decompressedSize);
}

@Test
public void testLargeRle()
throws IOException
{
// Dataset that produces an RLE block with 3-byte header

Compressor compressor = getCompressor();

byte[] original = Resources.toByteArray(getClass().getClassLoader().getResource("data/zstd/large-rle"));
int maxCompressLength = compressor.maxCompressedLength(original.length);

byte[] compressed = new byte[maxCompressLength];
int compressedSize = compressor.compress(original, 0, original.length, compressed, 0, compressed.length);

byte[] decompressed = new byte[original.length];
int decompressedSize = getDecompressor().decompress(compressed, 0, compressedSize, decompressed, 0, decompressed.length);

assertByteArraysEqual(original, 0, original.length, decompressed, 0, decompressedSize);
}
}
1 change: 1 addition & 0 deletions src/test/resources/data/zstd/large-rle

Large diffs are not rendered by default.