Skip to content

Commit 54d3d50

Browse files
benjaminpluben
authored andcommitted
Fix some error return codes.
The ZSTD_ErrorCode enum (ZSTD_error_*) contains small integers. The normal ZSTD convention for returning errors is to return large size_t values. Therefore, for the cases fixed in this commit, the enum values need to be transformed, so the caller can recognize them as errors.
1 parent b788a2e commit 54d3d50

File tree

3 files changed

+52
-52
lines changed

3 files changed

+52
-52
lines changed

src/main/native/jni_directbuffercompress_zstd.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ JNIEXPORT jint JNICALL Java_com_github_luben_zstd_ZstdDirectBufferCompressingStr
8484
jclass dict_clazz = (*env)->GetObjectClass(env, dict);
8585
jfieldID compress_dict = (*env)->GetFieldID(env, dict_clazz, "nativePtr", "J");
8686
ZSTD_CDict* cdict = (ZSTD_CDict*)(intptr_t)(*env)->GetLongField(env, dict, compress_dict);
87-
if (cdict == NULL) return ZSTD_error_dictionary_wrong;
87+
if (cdict == NULL) return ZSTD_ERROR(dictionary_wrong);
8888
ZSTD_CCtx_reset((ZSTD_CStream *)(intptr_t) stream, ZSTD_reset_session_only);
8989
return ZSTD_CCtx_refCDict((ZSTD_CStream *)(intptr_t) stream, cdict);
9090
}

src/main/native/jni_fast_zstd.c

+50-50
Original file line numberDiff line numberDiff line change
@@ -86,20 +86,20 @@ JNIEXPORT void JNICALL Java_com_github_luben_zstd_ZstdDictDecompress_free
8686
JNIEXPORT jlong JNICALL Java_com_github_luben_zstd_Zstd_decompressFastDict0
8787
(JNIEnv *env, jclass obj, jbyteArray dst, jint dst_offset, jbyteArray src, jint src_offset, jint src_length, jobject dict)
8888
{
89-
if (NULL == dict) return ZSTD_error_dictionary_wrong;
89+
if (NULL == dict) return ZSTD_ERROR(dictionary_wrong);
9090
ZSTD_DDict* ddict = (ZSTD_DDict*)(intptr_t)(*env)->GetLongField(env, dict, decompress_dict);
91-
if (NULL == ddict) return ZSTD_error_dictionary_wrong;
92-
if (NULL == dst) return ZSTD_error_dstSize_tooSmall;
93-
if (NULL == src) return ZSTD_error_srcSize_wrong;
94-
if (0 > dst_offset) return ZSTD_error_dstSize_tooSmall;
95-
if (0 > src_offset) return ZSTD_error_srcSize_wrong;
96-
if (0 > src_length) return ZSTD_error_srcSize_wrong;
91+
if (NULL == ddict) return ZSTD_ERROR(dictionary_wrong);
92+
if (NULL == dst) return ZSTD_ERROR(dstSize_tooSmall);
93+
if (NULL == src) return ZSTD_ERROR(srcSize_wrong);
94+
if (0 > dst_offset) return ZSTD_ERROR(dstSize_tooSmall);
95+
if (0 > src_offset) return ZSTD_ERROR(srcSize_wrong);
96+
if (0 > src_length) return ZSTD_ERROR(srcSize_wrong);
9797

9898
size_t size = (size_t)(0-ZSTD_error_memory_allocation);
9999
jsize dst_size = (*env)->GetArrayLength(env, dst);
100100
jsize src_size = (*env)->GetArrayLength(env, src);
101-
if (dst_offset > dst_size) return ZSTD_error_dstSize_tooSmall;
102-
if (src_size < (src_offset + src_length)) return ZSTD_error_srcSize_wrong;
101+
if (dst_offset > dst_size) return ZSTD_ERROR(dstSize_tooSmall);
102+
if (src_size < (src_offset + src_length)) return ZSTD_ERROR(srcSize_wrong);
103103
dst_size -= dst_offset;
104104
void *dst_buff = (*env)->GetPrimitiveArrayCritical(env, dst, NULL);
105105
if (dst_buff == NULL) goto E1;
@@ -120,21 +120,21 @@ E1: return size;
120120
*/
121121
JNIEXPORT jlong JNICALL Java_com_github_luben_zstd_Zstd_compressFastDict0
122122
(JNIEnv *env, jclass obj, jbyteArray dst, jint dst_offset, jbyteArray src, jint src_offset, jint src_length, jobject dict) {
123-
if (NULL == dict) return ZSTD_error_dictionary_wrong;
123+
if (NULL == dict) return ZSTD_ERROR(dictionary_wrong);
124124
ZSTD_CDict* cdict = (ZSTD_CDict*)(intptr_t)(*env)->GetLongField(env, dict, compress_dict);
125-
if (NULL == cdict) return ZSTD_error_dictionary_wrong;
126-
if (NULL == dst) return ZSTD_error_dstSize_tooSmall;
127-
if (NULL == src) return ZSTD_error_srcSize_wrong;
128-
if (0 > dst_offset) return ZSTD_error_dstSize_tooSmall;
129-
if (0 > src_offset) return ZSTD_error_srcSize_wrong;
130-
if (0 > src_length) return ZSTD_error_srcSize_wrong;
125+
if (NULL == cdict) return ZSTD_ERROR(dictionary_wrong);
126+
if (NULL == dst) return ZSTD_ERROR(dstSize_tooSmall);
127+
if (NULL == src) return ZSTD_ERROR(srcSize_wrong);
128+
if (0 > dst_offset) return ZSTD_ERROR(dstSize_tooSmall);
129+
if (0 > src_offset) return ZSTD_ERROR(srcSize_wrong);
130+
if (0 > src_length) return ZSTD_ERROR(srcSize_wrong);
131131

132132

133133
size_t size = (size_t)(0-ZSTD_error_memory_allocation);
134134
jsize dst_size = (*env)->GetArrayLength(env, dst);
135135
jsize src_size = (*env)->GetArrayLength(env, src);
136-
if (dst_offset > dst_size) return ZSTD_error_dstSize_tooSmall;
137-
if (src_size < (src_offset + src_length)) return ZSTD_error_srcSize_wrong;
136+
if (dst_offset > dst_size) return ZSTD_ERROR(dstSize_tooSmall);
137+
if (src_size < (src_offset + src_length)) return ZSTD_ERROR(srcSize_wrong);
138138
dst_size -= dst_offset;
139139
void *dst_buff = (*env)->GetPrimitiveArrayCritical(env, dst, NULL);
140140
if (dst_buff == NULL) goto E1;
@@ -154,14 +154,14 @@ E1: return size;
154154
*/
155155
JNIEXPORT jlong JNICALL Java_com_github_luben_zstd_Zstd_compressDirectByteBufferFastDict0
156156
(JNIEnv *env, jclass obj, jobject dst, jint dst_offset, jint dst_size, jobject src, jint src_offset, jint src_size, jobject dict) {
157-
if (NULL == dict) return ZSTD_error_dictionary_wrong;
157+
if (NULL == dict) return ZSTD_ERROR(dictionary_wrong);
158158
ZSTD_CDict* cdict = (ZSTD_CDict*)(intptr_t)(*env)->GetLongField(env, dict, compress_dict);
159-
if (NULL == cdict) return ZSTD_error_dictionary_wrong;
160-
if (NULL == dst) return ZSTD_error_dstSize_tooSmall;
161-
if (NULL == src) return ZSTD_error_srcSize_wrong;
162-
if (0 > dst_offset) return ZSTD_error_dstSize_tooSmall;
163-
if (0 > src_offset) return ZSTD_error_srcSize_wrong;
164-
if (0 > src_size) return ZSTD_error_srcSize_wrong;
159+
if (NULL == cdict) return ZSTD_ERROR(dictionary_wrong);
160+
if (NULL == dst) return ZSTD_ERROR(dstSize_tooSmall);
161+
if (NULL == src) return ZSTD_ERROR(srcSize_wrong);
162+
if (0 > dst_offset) return ZSTD_ERROR(dstSize_tooSmall);
163+
if (0 > src_offset) return ZSTD_ERROR(srcSize_wrong);
164+
if (0 > src_size) return ZSTD_ERROR(srcSize_wrong);
165165
size_t size = (size_t)(0-ZSTD_error_memory_allocation);
166166
char *dst_buff = (char*)(*env)->GetDirectBufferAddress(env, dst);
167167
char *src_buff = (char*)(*env)->GetDirectBufferAddress(env, src);
@@ -178,14 +178,14 @@ JNIEXPORT jlong JNICALL Java_com_github_luben_zstd_Zstd_compressDirectByteBuffer
178178
JNIEXPORT jlong JNICALL Java_com_github_luben_zstd_Zstd_decompressDirectByteBufferFastDict0
179179
(JNIEnv *env, jclass obj, jobject dst, jint dst_offset, jint dst_size, jobject src, jint src_offset, jint src_size, jobject dict)
180180
{
181-
if (NULL == dict) return ZSTD_error_dictionary_wrong;
181+
if (NULL == dict) return ZSTD_ERROR(dictionary_wrong);
182182
ZSTD_DDict* ddict = (ZSTD_DDict*)(intptr_t)(*env)->GetLongField(env, dict, decompress_dict);
183-
if (NULL == ddict) return ZSTD_error_dictionary_wrong;
184-
if (NULL == dst) return ZSTD_error_dstSize_tooSmall;
185-
if (NULL == src) return ZSTD_error_srcSize_wrong;
186-
if (0 > dst_offset) return ZSTD_error_dstSize_tooSmall;
187-
if (0 > src_offset) return ZSTD_error_srcSize_wrong;
188-
if (0 > src_size) return ZSTD_error_srcSize_wrong;
183+
if (NULL == ddict) return ZSTD_ERROR(dictionary_wrong);
184+
if (NULL == dst) return ZSTD_ERROR(dstSize_tooSmall);
185+
if (NULL == src) return ZSTD_ERROR(srcSize_wrong);
186+
if (0 > dst_offset) return ZSTD_ERROR(dstSize_tooSmall);
187+
if (0 > src_offset) return ZSTD_ERROR(srcSize_wrong);
188+
if (0 > src_size) return ZSTD_ERROR(srcSize_wrong);
189189

190190
size_t size = (size_t)(0-ZSTD_error_memory_allocation);
191191
char *dst_buff = (char*)(*env)->GetDirectBufferAddress(env, dst);
@@ -294,7 +294,7 @@ JNIEXPORT jlong JNICALL Java_com_github_luben_zstd_ZstdCompressCtx_loadCDictFast
294294
return ZSTD_CCtx_refCDict(cctx, NULL);
295295
}
296296
ZSTD_CDict* cdict = (ZSTD_CDict*)(intptr_t)(*env)->GetLongField(env, dict, compress_dict);
297-
if (NULL == cdict) return ZSTD_error_dictionary_wrong;
297+
if (NULL == cdict) return ZSTD_ERROR(dictionary_wrong);
298298
return ZSTD_CCtx_refCDict(cctx, cdict);
299299
}
300300

@@ -326,11 +326,11 @@ JNIEXPORT jlong JNICALL Java_com_github_luben_zstd_ZstdCompressCtx_loadCDict0
326326
*/
327327
JNIEXPORT jlong JNICALL Java_com_github_luben_zstd_ZstdCompressCtx_compressDirectByteBuffer0
328328
(JNIEnv *env, jclass jctx, jobject dst, jint dst_offset, jint dst_size, jobject src, jint src_offset, jint src_size) {
329-
if (NULL == dst) return ZSTD_error_dstSize_tooSmall;
330-
if (NULL == src) return ZSTD_error_srcSize_wrong;
331-
if (0 > dst_offset) return ZSTD_error_dstSize_tooSmall;
332-
if (0 > src_offset) return ZSTD_error_srcSize_wrong;
333-
if (0 > src_size) return ZSTD_error_srcSize_wrong;
329+
if (NULL == dst) return ZSTD_ERROR(dstSize_tooSmall);
330+
if (NULL == src) return ZSTD_ERROR(srcSize_wrong);
331+
if (0 > dst_offset) return ZSTD_ERROR(dstSize_tooSmall);
332+
if (0 > src_offset) return ZSTD_ERROR(srcSize_wrong);
333+
if (0 > src_size) return ZSTD_ERROR(srcSize_wrong);
334334

335335
jsize dst_cap = (*env)->GetDirectBufferCapacity(env, dst);
336336
if (dst_offset + dst_size > dst_cap) return ERROR(dstSize_tooSmall);
@@ -357,9 +357,9 @@ JNIEXPORT jlong JNICALL Java_com_github_luben_zstd_ZstdCompressCtx_compressByteA
357357
(JNIEnv *env, jclass jctx, jbyteArray dst, jint dst_offset, jint dst_size, jbyteArray src, jint src_offset, jint src_size) {
358358
size_t size = (size_t)(0-ZSTD_error_memory_allocation);
359359

360-
if (0 > dst_offset) return ZSTD_error_dstSize_tooSmall;
361-
if (0 > src_offset) return ZSTD_error_srcSize_wrong;
362-
if (0 > src_size) return ZSTD_error_srcSize_wrong;
360+
if (0 > dst_offset) return ZSTD_ERROR(dstSize_tooSmall);
361+
if (0 > src_offset) return ZSTD_ERROR(srcSize_wrong);
362+
if (0 > src_size) return ZSTD_ERROR(srcSize_wrong);
363363

364364
if (src_offset + src_size > (*env)->GetArrayLength(env, src)) return ERROR(srcSize_wrong);
365365
if (dst_offset + dst_size > (*env)->GetArrayLength(env, dst)) return ERROR(dstSize_tooSmall);
@@ -425,7 +425,7 @@ JNIEXPORT jlong JNICALL Java_com_github_luben_zstd_ZstdDecompressCtx_loadDDictFa
425425
return ZSTD_DCtx_refDDict(dctx, NULL);
426426
}
427427
ZSTD_DDict* ddict = (ZSTD_DDict*)(intptr_t)(*env)->GetLongField(env, dict, decompress_dict);
428-
if (NULL == ddict) return ZSTD_error_dictionary_wrong;
428+
if (NULL == ddict) return ZSTD_ERROR(dictionary_wrong);
429429
return ZSTD_DCtx_refDDict(dctx, ddict);
430430
}
431431

@@ -459,11 +459,11 @@ JNIEXPORT jlong JNICALL Java_com_github_luben_zstd_ZstdDecompressCtx_loadDDict0
459459
JNIEXPORT jlong JNICALL Java_com_github_luben_zstd_ZstdDecompressCtx_decompressDirectByteBuffer0
460460
(JNIEnv *env, jclass jctx, jobject dst, jint dst_offset, jint dst_size, jobject src, jint src_offset, jint src_size)
461461
{
462-
if (NULL == dst) return ZSTD_error_dstSize_tooSmall;
463-
if (NULL == src) return ZSTD_error_srcSize_wrong;
464-
if (0 > dst_offset) return ZSTD_error_dstSize_tooSmall;
465-
if (0 > src_offset) return ZSTD_error_srcSize_wrong;
466-
if (0 > src_size) return ZSTD_error_srcSize_wrong;
462+
if (NULL == dst) return ZSTD_ERROR(dstSize_tooSmall);
463+
if (NULL == src) return ZSTD_ERROR(srcSize_wrong);
464+
if (0 > dst_offset) return ZSTD_ERROR(dstSize_tooSmall);
465+
if (0 > src_offset) return ZSTD_ERROR(srcSize_wrong);
466+
if (0 > src_size) return ZSTD_ERROR(srcSize_wrong);
467467

468468
jsize dst_cap = (*env)->GetDirectBufferCapacity(env, dst);
469469
if (dst_offset + dst_size > dst_cap) return ERROR(dstSize_tooSmall);
@@ -490,9 +490,9 @@ JNIEXPORT jlong JNICALL Java_com_github_luben_zstd_ZstdDecompressCtx_decompressB
490490
(JNIEnv *env, jclass jctx, jbyteArray dst, jint dst_offset, jint dst_size, jbyteArray src, jint src_offset, jint src_size) {
491491
size_t size = (size_t)(0-ZSTD_error_memory_allocation);
492492

493-
if (0 > dst_offset) return ZSTD_error_dstSize_tooSmall;
494-
if (0 > src_offset) return ZSTD_error_srcSize_wrong;
495-
if (0 > src_size) return ZSTD_error_srcSize_wrong;
493+
if (0 > dst_offset) return ZSTD_ERROR(dstSize_tooSmall);
494+
if (0 > src_offset) return ZSTD_ERROR(srcSize_wrong);
495+
if (0 > src_size) return ZSTD_ERROR(srcSize_wrong);
496496

497497
if (src_offset + src_size > (*env)->GetArrayLength(env, src)) return ERROR(srcSize_wrong);
498498
if (dst_offset + dst_size > (*env)->GetArrayLength(env, dst)) return ERROR(dstSize_tooSmall);

src/main/native/jni_zstd.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ JNIEXPORT jlong JNICALL Java_com_github_luben_zstd_Zstd_decompressedDirectByteBu
114114
(JNIEnv *env, jclass obj, jobject src_buf, jint src_offset, jint src_size) {
115115
size_t size = (size_t)(0-ZSTD_error_memory_allocation);
116116
jsize src_cap = (*env)->GetDirectBufferCapacity(env, src_buf);
117-
if (src_offset + src_size > src_cap) return ZSTD_error_GENERIC;
117+
if (src_offset + src_size > src_cap) return ZSTD_ERROR(GENERIC);
118118
char *src_buf_ptr = (char*)(*env)->GetDirectBufferAddress(env, src_buf);
119119
if (src_buf_ptr == NULL) goto E1;
120120
size = ZSTD_getDecompressedSize(src_buf_ptr + src_offset, (size_t) src_size);

0 commit comments

Comments
 (0)