Skip to content

Commit 048dd36

Browse files
committed
Make arrange functions pure C so a pure C variable instead of a PyObject variable is returned
1 parent 0cafb49 commit 048dd36

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

src/isal/isal_zlib.pyx

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,17 +136,17 @@ cdef Py_ssize_t py_ssize_t_min(Py_ssize_t a, Py_ssize_t b):
136136
else:
137137
return b
138138

139-
cdef arrange_output_buffer_with_maximum(stream_or_state *stream,
140-
unsigned char **buffer,
141-
Py_ssize_t length,
142-
Py_ssize_t max_length):
139+
cdef Py_ssize_t arrange_output_buffer_with_maximum(stream_or_state *stream,
140+
unsigned char **buffer,
141+
Py_ssize_t length,
142+
Py_ssize_t max_length):
143143
cdef Py_ssize_t occupied
144144
cdef Py_ssize_t new_length
145145
cdef unsigned char * new_buffer
146146
if buffer[0] == NULL:
147147
buffer[0] = <unsigned char*>PyMem_Malloc(length * sizeof(char))
148148
if buffer[0] == NULL:
149-
raise MemoryError("Unsufficient memory for buffer allocation")
149+
return -1
150150
occupied = 0
151151
else:
152152
occupied = stream.next_out - buffer[0]
@@ -159,18 +159,20 @@ cdef arrange_output_buffer_with_maximum(stream_or_state *stream,
159159
new_length = max_length
160160
new_buffer = <unsigned char *>PyMem_Realloc(buffer[0], new_length)
161161
if new_buffer == NULL:
162-
raise MemoryError("Unssufficient memory for buffer allocation")
162+
return -1
163163
buffer[0] = new_buffer
164164
length = new_length
165165
stream.avail_out = <unsigned int>py_ssize_t_min(length - occupied, UINT32_MAX)
166166
stream.next_out = buffer[0] + occupied
167167
return length
168168

169-
cdef arrange_output_buffer(stream_or_state *stream, unsigned char **buffer, Py_ssize_t length):
169+
cdef Py_ssize_t arrange_output_buffer(stream_or_state *stream,
170+
unsigned char **buffer,
171+
Py_ssize_t length):
170172
cdef Py_ssize_t ret
171173
ret = arrange_output_buffer_with_maximum(stream, buffer, length, PY_SSIZE_T_MAX)
172-
if ret == -2:
173-
raise MemoryError("Output buffer has reached maximum size")
174+
if ret == -2: # Maximum reached.
175+
return -1
174176
return ret
175177

176178
cdef void arrange_input_buffer(stream_or_state *stream, Py_ssize_t *remains):
@@ -242,6 +244,8 @@ def compress(data,
242244
# this loop still needs to run one time.
243245
while True:
244246
bufsize = arrange_output_buffer(&stream, &obuf, bufsize)
247+
if bufsize == -1:
248+
raise MemoryError("Unsufficient memory for buffer allocation")
245249
err = isal_deflate(&stream)
246250
if err != COMP_OK:
247251
# There is some python interacting when possible exceptions
@@ -310,6 +314,8 @@ def decompress(data,
310314
# to be called again.
311315
while True:
312316
bufsize = arrange_output_buffer(&stream, &obuf, bufsize)
317+
if bufsize == -1:
318+
raise MemoryError("Unsufficient memory for buffer allocation")
313319
err = isal_inflate(&stream)
314320
if err != ISAL_DECOMP_OK:
315321
# There is some python interacting when possible exceptions
@@ -448,6 +454,8 @@ cdef class Compress:
448454
arrange_input_buffer(&self.stream, &ibuflen)
449455
while True: #self.stream.avail_in != 0:
450456
obuflen = arrange_output_buffer(&self.stream, &obuf, obuflen)
457+
if obuflen== -1:
458+
raise MemoryError("Unsufficient memory for buffer allocation")
451459
err = isal_deflate(&self.stream)
452460
if err != COMP_OK:
453461
# There is some python interacting when possible exceptions
@@ -495,6 +503,8 @@ cdef class Compress:
495503
try:
496504
while True:
497505
length = arrange_output_buffer(&self.stream, &obuf, length)
506+
if length == -1:
507+
raise MemoryError("Unsufficient memory for buffer allocation")
498508
err = isal_deflate(&self.stream)
499509
if err != COMP_OK:
500510
# There is some python interacting when possible exceptions
@@ -630,7 +640,9 @@ cdef class Decompress:
630640
while True:#(self.stream.avail_out == 0 or self.stream.avail_in != 0):
631641
obuflen = arrange_output_buffer_with_maximum(
632642
&self.stream, &obuf, obuflen, hard_limit)
633-
if obuflen == -2:
643+
if obuflen == -1:
644+
raise MemoryError("Unsufficient memory for buffer allocation")
645+
elif obuflen == -2:
634646
max_length_reached = True
635647
break
636648
err = isal_inflate(&self.stream)
@@ -677,6 +689,8 @@ cdef class Decompress:
677689
arrange_input_buffer(&self.stream, &ibuflen)
678690
while True:
679691
obuflen = arrange_output_buffer(&self.stream, &obuf, obuflen)
692+
if obuflen == -1:
693+
raise MemoryError("Unsufficient memory for buffer allocation")
680694
err = isal_inflate(&self.stream)
681695
if err != ISAL_DECOMP_OK:
682696
# There is some python interacting when possible exceptions

0 commit comments

Comments
 (0)