Skip to content

Commit e5c6a32

Browse files
committed
Fix overflows
[error] /home/luben/zstd-jni/src/main/native/jni_directbuffercompress_zstd.c: In function ‘Java_com_github_luben_zstd_ZstdDirectBufferCompressingStreamNoFinalizer_initCStreamWithFastDict’: [error] /home/luben/zstd-jni/src/main/native/common/error_private.h:56:26: warning: overflow in conversion from ‘long unsigned int’ to ‘jint’ {aka ‘int’} changes value from ‘18446744073709551584’ to ‘-32’ [-Woverflow] [error] 56 | #define ZSTD_ERROR(name) ((size_t)-PREFIX(name)) [error] | ^ [error] /home/luben/zstd-jni/src/main/native/jni_directbuffercompress_zstd.c:87:31: note: in expansion of macro ‘ZSTD_ERROR’ [error] 87 | if (cdict == NULL) return ZSTD_ERROR(dictionary_wrong); [error] | ^~~~~~~~~~
1 parent 54d3d50 commit e5c6a32

4 files changed

+31
-32
lines changed

src/main/java/com/github/luben/zstd/ZstdDirectBufferCompressingStreamNoFinalizer.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ public ZstdDirectBufferCompressingStreamNoFinalizer(ByteBuffer target, int level
4848
/* JNI methods */
4949
private static native long recommendedCOutSize();
5050
private static native long createCStream();
51-
private static native int freeCStream(long ctx);
52-
private native int initCStream(long ctx, int level);
53-
private native int initCStreamWithDict(long ctx, byte[] dict, int dict_size, int level);
54-
private native int initCStreamWithFastDict(long ctx, ZstdDictCompress dict);
55-
private native int compressDirectByteBuffer(long ctx, ByteBuffer dst, int dstOffset, int dstSize, ByteBuffer src, int srcOffset, int srcSize);
56-
private native int flushStream(long ctx, ByteBuffer dst, int dstOffset, int dstSize);
57-
private native int endStream(long ctx, ByteBuffer dst, int dstOffset, int dstSize);
51+
private static native long freeCStream(long ctx);
52+
private native long initCStream(long ctx, int level);
53+
private native long initCStreamWithDict(long ctx, byte[] dict, int dict_size, int level);
54+
private native long initCStreamWithFastDict(long ctx, ZstdDictCompress dict);
55+
private native long compressDirectByteBuffer(long ctx, ByteBuffer dst, int dstOffset, int dstSize, ByteBuffer src, int srcOffset, int srcSize);
56+
private native long flushStream(long ctx, ByteBuffer dst, int dstOffset, int dstSize);
57+
private native long endStream(long ctx, ByteBuffer dst, int dstOffset, int dstSize);
5858

5959
public ZstdDirectBufferCompressingStreamNoFinalizer setDict(byte[] dict) throws IOException {
6060
if (initialized) {
@@ -82,7 +82,7 @@ public void compress(ByteBuffer source) throws IOException {
8282
throw new IOException("Stream closed");
8383
}
8484
if (!initialized) {
85-
int result = 0;
85+
long result = 0;
8686
ZstdDictCompress fastDict = this.fastDict;
8787
if (fastDict != null) {
8888
fastDict.acquireSharedLock();
@@ -111,7 +111,7 @@ public void compress(ByteBuffer source) throws IOException {
111111
throw new IOException("The target buffer has no more space, even after flushing, and there are still bytes to compress");
112112
}
113113
}
114-
int result = compressDirectByteBuffer(stream, target, target.position(), target.remaining(), source, source.position(), source.remaining());
114+
long result = compressDirectByteBuffer(stream, target, target.position(), target.remaining(), source, source.position(), source.remaining());
115115
if (Zstd.isError(result)) {
116116
throw new IOException("Compression error: " + Zstd.getErrorName(result));
117117
}
@@ -126,7 +126,7 @@ public void flush() throws IOException {
126126
throw new IOException("Already closed");
127127
}
128128
if (initialized) {
129-
int needed;
129+
long needed;
130130
do {
131131
needed = flushStream(stream, target, target.position(), target.remaining());
132132
if (Zstd.isError(needed)) {
@@ -151,7 +151,7 @@ public void close() throws IOException {
151151
if (!closed) {
152152
try {
153153
if (initialized) {
154-
int needed;
154+
long needed;
155155
do {
156156
needed = endStream(stream, target, target.position(), target.remaining());
157157
if (Zstd.isError(needed)) {

src/main/java/com/github/luben/zstd/ZstdDirectBufferDecompressingStreamNoFinalizer.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ protected ByteBuffer refill(ByteBuffer toRefill) {
2727
private boolean closed = false;
2828
private boolean streamEnd = false;
2929

30-
private static native int recommendedDOutSize();
30+
private static native long recommendedDOutSize();
3131
private static native long createDStream();
32-
private static native int freeDStream(long stream);
33-
private native int initDStream(long stream);
32+
private static native long freeDStream(long stream);
33+
private native long initDStream(long stream);
3434
private native long decompressStream(long stream, ByteBuffer dst, int dstOffset, int dstSize, ByteBuffer src, int srcOffset, int srcSize);
3535

3636
public ZstdDirectBufferDecompressingStreamNoFinalizer(ByteBuffer source) {
@@ -51,7 +51,7 @@ public static int recommendedTargetBufferSize() {
5151
}
5252

5353
public ZstdDirectBufferDecompressingStreamNoFinalizer setDict(byte[] dict) throws IOException {
54-
int size = Zstd.loadDictDecompress(stream, dict, dict.length);
54+
long size = Zstd.loadDictDecompress(stream, dict, dict.length);
5555
if (Zstd.isError(size)) {
5656
throw new IOException("Decompression error: " + Zstd.getErrorName(size));
5757
}
@@ -61,7 +61,7 @@ public ZstdDirectBufferDecompressingStreamNoFinalizer setDict(byte[] dict) throw
6161
public ZstdDirectBufferDecompressingStreamNoFinalizer setDict(ZstdDictDecompress dict) throws IOException {
6262
dict.acquireSharedLock();
6363
try {
64-
int size = Zstd.loadFastDictDecompress(stream, dict);
64+
long size = Zstd.loadFastDictDecompress(stream, dict);
6565
if (Zstd.isError(size)) {
6666
throw new IOException("Decompression error: " + Zstd.getErrorName(size));
6767
}

src/main/native/jni_directbuffercompress_zstd.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ static jfieldID produced_id;
1313
* Method: recommendedCOutSize
1414
* Signature: ()I
1515
*/
16-
JNIEXPORT jint JNICALL Java_com_github_luben_zstd_ZstdDirectBufferCompressingStreamNoFinalizer_recommendedCOutSize
16+
JNIEXPORT jlong JNICALL Java_com_github_luben_zstd_ZstdDirectBufferCompressingStreamNoFinalizer_recommendedCOutSize
1717
(JNIEnv *env, jclass obj) {
18-
return (jint) ZSTD_CStreamOutSize();
18+
return ZSTD_CStreamOutSize();
1919
}
2020

2121
/*
@@ -33,7 +33,7 @@ JNIEXPORT jlong JNICALL Java_com_github_luben_zstd_ZstdDirectBufferCompressingSt
3333
* Method: freeCStream
3434
* Signature: (J)I
3535
*/
36-
JNIEXPORT jint JNICALL Java_com_github_luben_zstd_ZstdDirectBufferCompressingStreamNoFinalizer_freeCStream
36+
JNIEXPORT jlong JNICALL Java_com_github_luben_zstd_ZstdDirectBufferCompressingStreamNoFinalizer_freeCStream
3737
(JNIEnv *env, jclass obj, jlong stream) {
3838
return ZSTD_freeCStream((ZSTD_CStream *)(intptr_t) stream);
3939
}
@@ -43,7 +43,7 @@ JNIEXPORT jint JNICALL Java_com_github_luben_zstd_ZstdDirectBufferCompressingStr
4343
* Method: initCStream
4444
* Signature: (JI)I
4545
*/
46-
JNIEXPORT jint JNICALL Java_com_github_luben_zstd_ZstdDirectBufferCompressingStreamNoFinalizer_initCStream
46+
JNIEXPORT jlong JNICALL Java_com_github_luben_zstd_ZstdDirectBufferCompressingStreamNoFinalizer_initCStream
4747
(JNIEnv *env, jclass obj, jlong stream, jint level) {
4848
jclass clazz = (*env)->GetObjectClass(env, obj);
4949
consumed_id = (*env)->GetFieldID(env, clazz, "consumed", "I");
@@ -56,7 +56,7 @@ JNIEXPORT jint JNICALL Java_com_github_luben_zstd_ZstdDirectBufferCompressingStr
5656
* Method: initCStreamWithDict
5757
* Signature: (J[BII)I
5858
*/
59-
JNIEXPORT jint JNICALL Java_com_github_luben_zstd_ZstdDirectBufferCompressingStreamNoFinalizer_initCStreamWithDict
59+
JNIEXPORT jlong JNICALL Java_com_github_luben_zstd_ZstdDirectBufferCompressingStreamNoFinalizer_initCStreamWithDict
6060
(JNIEnv *env, jclass obj, jlong stream, jbyteArray dict, jint dict_size, jint level) {
6161
size_t result = (size_t)(0-ZSTD_error_memory_allocation);
6262
jclass clazz = (*env)->GetObjectClass(env, obj);
@@ -76,7 +76,7 @@ JNIEXPORT jint JNICALL Java_com_github_luben_zstd_ZstdDirectBufferCompressingStr
7676
* Class: com_github_luben_zstd_ZstdDirectBufferCompressingStreamNoFinalizer
7777
* Method: initCStreamWithFastDict
7878
*/
79-
JNIEXPORT jint JNICALL Java_com_github_luben_zstd_ZstdDirectBufferCompressingStreamNoFinalizer_initCStreamWithFastDict
79+
JNIEXPORT jlong JNICALL Java_com_github_luben_zstd_ZstdDirectBufferCompressingStreamNoFinalizer_initCStreamWithFastDict
8080
(JNIEnv *env, jclass obj, jlong stream, jobject dict) {
8181
jclass clazz = (*env)->GetObjectClass(env, obj);
8282
consumed_id = (*env)->GetFieldID(env, clazz, "consumed", "I");
@@ -97,7 +97,7 @@ JNIEXPORT jint JNICALL Java_com_github_luben_zstd_ZstdDirectBufferCompressingStr
9797
JNIEXPORT jlong JNICALL Java_com_github_luben_zstd_ZstdDirectBufferCompressingStreamNoFinalizer_compressDirectByteBuffer
9898
(JNIEnv *env, jclass obj, jlong stream, jobject dst_buf, jint dst_offset, jint dst_size, jobject src_buf, jint src_offset, jint src_size) {
9999

100-
size_t size = (size_t)ERROR(memory_allocation);
100+
size_t size = ERROR(memory_allocation);
101101
jsize dst_cap = (*env)->GetDirectBufferCapacity(env, dst_buf);
102102
if (dst_offset + dst_size > dst_cap) return ERROR(dstSize_tooSmall);
103103
jsize src_cap = (*env)->GetDirectBufferCapacity(env, src_buf);
@@ -122,7 +122,7 @@ E1: return size;
122122
* Method: endStream
123123
* Signature: (JLjava/nio/ByteBuffer;II)I
124124
*/
125-
JNIEXPORT jint JNICALL Java_com_github_luben_zstd_ZstdDirectBufferCompressingStreamNoFinalizer_endStream
125+
JNIEXPORT jlong JNICALL Java_com_github_luben_zstd_ZstdDirectBufferCompressingStreamNoFinalizer_endStream
126126
(JNIEnv *env, jclass obj, jlong stream, jobject dst_buf, jint dst_offset, jint dst_size) {
127127

128128
size_t size = (size_t)(0-ZSTD_error_memory_allocation);
@@ -136,15 +136,15 @@ JNIEXPORT jint JNICALL Java_com_github_luben_zstd_ZstdDirectBufferCompressingStr
136136
size = ZSTD_endStream((ZSTD_CStream *)(intptr_t) stream, &output);
137137
(*env)->SetIntField(env, obj, produced_id, output.pos);
138138
}
139-
return (jint) size;
139+
return size;
140140
}
141141

142142
/*
143143
* Class: com_github_luben_zstd_ZstdDirectBufferCompressingStreamNoFinalizer
144144
* Method: flushStream
145145
* Signature: (JLjava/nio/ByteBuffer;II)I
146146
*/
147-
JNIEXPORT jint JNICALL Java_com_github_luben_zstd_ZstdDirectBufferCompressingStreamNoFinalizer_flushStream
147+
JNIEXPORT jlong JNICALL Java_com_github_luben_zstd_ZstdDirectBufferCompressingStreamNoFinalizer_flushStream
148148
(JNIEnv *env, jclass obj, jlong stream, jobject dst_buf, jint dst_offset, jint dst_size) {
149149

150150
size_t size = (size_t)(0-ZSTD_error_memory_allocation);
@@ -157,5 +157,5 @@ JNIEXPORT jint JNICALL Java_com_github_luben_zstd_ZstdDirectBufferCompressingStr
157157
size = ZSTD_flushStream((ZSTD_CStream *)(intptr_t) stream, &output);
158158
(*env)->SetIntField(env, obj, produced_id, output.pos);
159159
}
160-
return (jint) size;
160+
return size;
161161
}

src/main/native/jni_directbufferdecompress_zstd.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ static jfieldID produced_id;
1111
/*
1212
* Class: com_github_luben_zstd_ZstdDirectBufferDecompressingStreamNoFinalizer
1313
* Method: recommendedDOutSize
14-
* Signature: ()I
1514
*/
16-
JNIEXPORT jint JNICALL Java_com_github_luben_zstd_ZstdDirectBufferDecompressingStreamNoFinalizer_recommendedDOutSize
15+
JNIEXPORT jlong JNICALL Java_com_github_luben_zstd_ZstdDirectBufferDecompressingStreamNoFinalizer_recommendedDOutSize
1716
(JNIEnv *env, jclass obj) {
18-
return (jint) ZSTD_DStreamOutSize();
17+
return ZSTD_DStreamOutSize();
1918
}
2019

2120
/*
@@ -33,7 +32,7 @@ JNIEXPORT jlong JNICALL Java_com_github_luben_zstd_ZstdDirectBufferDecompressing
3332
* Method: freeDStream
3433
* Signature: (J)I
3534
*/
36-
JNIEXPORT jint JNICALL Java_com_github_luben_zstd_ZstdDirectBufferDecompressingStreamNoFinalizer_freeDStream
35+
JNIEXPORT jlong JNICALL Java_com_github_luben_zstd_ZstdDirectBufferDecompressingStreamNoFinalizer_freeDStream
3736
(JNIEnv *env, jclass obj, jlong stream) {
3837
return ZSTD_freeDCtx((ZSTD_DCtx *)(intptr_t) stream);
3938
}
@@ -43,7 +42,7 @@ JNIEXPORT jint JNICALL Java_com_github_luben_zstd_ZstdDirectBufferDecompressingS
4342
* Method: initDStream
4443
* Signature: (J)I
4544
*/
46-
JNIEXPORT jint JNICALL Java_com_github_luben_zstd_ZstdDirectBufferDecompressingStreamNoFinalizer_initDStream
45+
JNIEXPORT jlong JNICALL Java_com_github_luben_zstd_ZstdDirectBufferDecompressingStreamNoFinalizer_initDStream
4746
(JNIEnv *env, jclass obj, jlong stream) {
4847
jclass clazz = (*env)->GetObjectClass(env, obj);
4948
consumed_id = (*env)->GetFieldID(env, clazz, "consumed", "I");

0 commit comments

Comments
 (0)