Skip to content

Commit 800b254

Browse files
committed
More cython fixes for Lz4 error handling
1 parent dc2de64 commit 800b254

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/_compress.pyx

+9-5
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ def decompress(pString):
106106
cdef char *cString # *char pStr
107107
cdef char *result # destination buffer
108108
cdef bytes pyResult # python wrapped result
109+
cdef int ret
109110

110111
# convert to char*
111112
cString = pString
@@ -114,7 +115,8 @@ def decompress(pString):
114115
# malloc
115116
result = <char*>malloc(original_size)
116117
# decompress
117-
if LZ4_decompress_safe(cString + hdr_size, result, compressed_size - hdr_size, original_size) < 0:
118+
ret = LZ4_decompress_safe(cString + hdr_size, result, compressed_size - hdr_size, original_size)
119+
if ret <= 0 or ret != original_size:
118120
free(result)
119121
raise Exception("Error decompressing")
120122
# cast back into python string
@@ -211,7 +213,8 @@ def decompressarr(pStrList):
211213
cdef uint32_t compressed_size
212214
cdef char *result
213215
cdef Py_ssize_t i
214-
cdef int ret = 0
216+
cdef int ret
217+
cdef int error = 0
215218

216219
# output parameters
217220
cdef char **cResult = <char **>malloc(n * sizeof(char *))
@@ -232,8 +235,9 @@ def decompressarr(pStrList):
232235
# malloc
233236
result = <char*>malloc(original_size)
234237
# decompress
235-
if LZ4_decompress_safe(cString + hdr_size, result, compressed_size - hdr_size, original_size) < 0:
236-
ret = -1
238+
ret = LZ4_decompress_safe(cString + hdr_size, result, compressed_size - hdr_size, original_size)
239+
if ret <= 0 or ret != original_size:
240+
error = -1
237241
# assign to result
238242
cResult[i] = result
239243
lengths[i] = original_size
@@ -248,7 +252,7 @@ def decompressarr(pStrList):
248252
free(cResult)
249253
free(cStrList)
250254

251-
if ret == -1:
255+
if error == -1:
252256
raise Exception("Error decompressing array")
253257

254258
return result_list

0 commit comments

Comments
 (0)