Skip to content

Commit b97eed7

Browse files
garydgregoryluben
authored andcommitted
Fix segmentation fault when calling Zstd.loadFastDictDecompress(long, ZstdDictDecompress)
1 parent 6549913 commit b97eed7

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/main/native/jni_zstd.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ JNIEXPORT jint JNICALL Java_com_github_luben_zstd_Zstd_loadDictDecompress
254254
*/
255255
JNIEXPORT jint JNICALL Java_com_github_luben_zstd_Zstd_loadFastDictDecompress
256256
(JNIEnv *env, jclass obj, jlong stream, jobject dict) {
257+
if (dict == NULL) return -ZSTD_error_dictionary_wrong;
257258
jclass dict_clazz = (*env)->GetObjectClass(env, dict);
258259
jfieldID decompress_dict = (*env)->GetFieldID(env, dict_clazz, "nativePtr", "J");
259260
ZSTD_DDict* ddict = (ZSTD_DDict*)(intptr_t)(*env)->GetLongField(env, dict, decompress_dict);

src/test/scala/Zstd.scala

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -918,14 +918,25 @@ class ZstdSpec extends AnyFlatSpec with ScalaCheckPropertyChecks {
918918
}
919919
}
920920

921-
"ZstdOutputStream" should s"do not cause a segmentation fault" in {
921+
"ZstdOutputStream" should s"do not cause a segmentation fault in setDict(ZstdDictCompress)" in {
922922
val os = new ByteArrayOutputStream(100)
923923
val zos = new ZstdOutputStream(os)
924924
assertThrows[ZstdIOException] {
925925
zos.setDict(null.asInstanceOf[ZstdDictCompress])
926926
}
927927
}
928928

929+
"Zstd" should s"do not cause a segmentation fault in loadFastDictDecompress()" in {
930+
val zin = new ZstdInputStreamNoFinalizer(new ByteArrayInputStream(Array[Byte]()))
931+
assertThrows[NullPointerException] {
932+
zin.setDict(null.asInstanceOf[ZstdDictDecompress])
933+
}
934+
// Avoid:
935+
// 0 0x0000000103a067c8 AccessInternal::PostRuntimeDispatch<G1BarrierSet::AccessBarrier<548964ull, G1BarrierSet>, (AccessInternal::BarrierType)2, 548964ull>::oop_access_barrier(void*) + 8 in libjvm.dylib
936+
// 1 0x0000000103daa0f8 jni_GetObjectClass + 184 in libjvm.dylib
937+
assert(Zstd.loadFastDictDecompress(0, null.asInstanceOf[ZstdDictDecompress]) == -32)
938+
}
939+
929940
"ZstdDirectBufferCompressingStream" should s"do nothing on double close but throw on writing on closed stream" in {
930941
val os = ByteBuffer.allocateDirect(100)
931942
val zos = new ZstdDirectBufferCompressingStream(os, 1)

0 commit comments

Comments
 (0)