@@ -136,17 +136,17 @@ cdef Py_ssize_t py_ssize_t_min(Py_ssize_t a, Py_ssize_t b):
136
136
else :
137
137
return b
138
138
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):
143
143
cdef Py_ssize_t occupied
144
144
cdef Py_ssize_t new_length
145
145
cdef unsigned char * new_buffer
146
146
if buffer [0 ] == NULL :
147
147
buffer [0 ] = < unsigned char * > PyMem_Malloc(length * sizeof(char ))
148
148
if buffer [0 ] == NULL :
149
- raise MemoryError ( " Unsufficient memory for buffer allocation " )
149
+ return - 1
150
150
occupied = 0
151
151
else :
152
152
occupied = stream.next_out - buffer [0 ]
@@ -159,18 +159,20 @@ cdef arrange_output_buffer_with_maximum(stream_or_state *stream,
159
159
new_length = max_length
160
160
new_buffer = < unsigned char * > PyMem_Realloc(buffer [0 ], new_length)
161
161
if new_buffer == NULL :
162
- raise MemoryError ( " Unssufficient memory for buffer allocation " )
162
+ return - 1
163
163
buffer [0 ] = new_buffer
164
164
length = new_length
165
165
stream.avail_out = < unsigned int > py_ssize_t_min(length - occupied, UINT32_MAX)
166
166
stream.next_out = buffer [0 ] + occupied
167
167
return length
168
168
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):
170
172
cdef Py_ssize_t ret
171
173
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
174
176
return ret
175
177
176
178
cdef void arrange_input_buffer(stream_or_state * stream, Py_ssize_t * remains):
@@ -242,6 +244,8 @@ def compress(data,
242
244
# this loop still needs to run one time.
243
245
while True :
244
246
bufsize = arrange_output_buffer(& stream, & obuf, bufsize)
247
+ if bufsize == - 1 :
248
+ raise MemoryError (" Unsufficient memory for buffer allocation" )
245
249
err = isal_deflate(& stream)
246
250
if err != COMP_OK:
247
251
# There is some python interacting when possible exceptions
@@ -310,6 +314,8 @@ def decompress(data,
310
314
# to be called again.
311
315
while True :
312
316
bufsize = arrange_output_buffer(& stream, & obuf, bufsize)
317
+ if bufsize == - 1 :
318
+ raise MemoryError (" Unsufficient memory for buffer allocation" )
313
319
err = isal_inflate(& stream)
314
320
if err != ISAL_DECOMP_OK:
315
321
# There is some python interacting when possible exceptions
@@ -448,6 +454,8 @@ cdef class Compress:
448
454
arrange_input_buffer(& self .stream, & ibuflen)
449
455
while True : # self.stream.avail_in != 0:
450
456
obuflen = arrange_output_buffer(& self .stream, & obuf, obuflen)
457
+ if obuflen== - 1 :
458
+ raise MemoryError (" Unsufficient memory for buffer allocation" )
451
459
err = isal_deflate(& self .stream)
452
460
if err != COMP_OK:
453
461
# There is some python interacting when possible exceptions
@@ -495,6 +503,8 @@ cdef class Compress:
495
503
try :
496
504
while True :
497
505
length = arrange_output_buffer(& self .stream, & obuf, length)
506
+ if length == - 1 :
507
+ raise MemoryError (" Unsufficient memory for buffer allocation" )
498
508
err = isal_deflate(& self .stream)
499
509
if err != COMP_OK:
500
510
# There is some python interacting when possible exceptions
@@ -630,7 +640,9 @@ cdef class Decompress:
630
640
while True :# (self.stream.avail_out == 0 or self.stream.avail_in != 0):
631
641
obuflen = arrange_output_buffer_with_maximum(
632
642
& 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 :
634
646
max_length_reached = True
635
647
break
636
648
err = isal_inflate(& self .stream)
@@ -677,6 +689,8 @@ cdef class Decompress:
677
689
arrange_input_buffer(& self .stream, & ibuflen)
678
690
while True :
679
691
obuflen = arrange_output_buffer(& self .stream, & obuf, obuflen)
692
+ if obuflen == - 1 :
693
+ raise MemoryError (" Unsufficient memory for buffer allocation" )
680
694
err = isal_inflate(& self .stream)
681
695
if err != ISAL_DECOMP_OK:
682
696
# There is some python interacting when possible exceptions
0 commit comments