Skip to content

Commit 6549913

Browse files
garydgregoryluben
authored andcommitted
Fix segmentation fault when calling ZstdOutputStream#setDict(ZstdDictCompress) with null
1 parent 7fc2ec2 commit 6549913

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

src/main/native/jni_zstd.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ JNIEXPORT jint JNICALL Java_com_github_luben_zstd_Zstd_loadDictCompress
286286
*/
287287
JNIEXPORT jint JNICALL Java_com_github_luben_zstd_Zstd_loadFastDictCompress
288288
(JNIEnv *env, jclass obj, jlong stream, jobject dict) {
289+
if (dict == NULL) return -ZSTD_error_dictionary_wrong;
289290
jclass dict_clazz = (*env)->GetObjectClass(env, dict);
290291
jfieldID compress_dict = (*env)->GetFieldID(env, dict_clazz, "nativePtr", "J");
291292
ZSTD_CDict* cdict = (ZSTD_CDict*)(intptr_t)(*env)->GetLongField(env, dict, compress_dict);

src/test/scala/Zstd.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,14 @@ class ZstdSpec extends AnyFlatSpec with ScalaCheckPropertyChecks {
918918
}
919919
}
920920

921+
"ZstdOutputStream" should s"do not cause a segmentation fault" in {
922+
val os = new ByteArrayOutputStream(100)
923+
val zos = new ZstdOutputStream(os)
924+
assertThrows[ZstdIOException] {
925+
zos.setDict(null.asInstanceOf[ZstdDictCompress])
926+
}
927+
}
928+
921929
"ZstdDirectBufferCompressingStream" should s"do nothing on double close but throw on writing on closed stream" in {
922930
val os = ByteBuffer.allocateDirect(100)
923931
val zos = new ZstdDirectBufferCompressingStream(os, 1)

0 commit comments

Comments
 (0)