@@ -121,12 +121,27 @@ decompress_buf(IgzipDecompressor *self, Py_ssize_t max_length)
121
121
compare against max_length and PyBytes_GET_SIZE we declare it as
122
122
signed */
123
123
PyObject * RetVal = NULL ;
124
- Py_ssize_t obuflen = DEF_BUF_SIZE ;
124
+ Py_ssize_t hard_limit ;
125
+
126
+ Py_ssize_t obuflen ;
127
+
125
128
int err ;
126
129
127
- if (obuflen > max_length )
130
+ if (max_length < 0 ) {
131
+ hard_limit = PY_SSIZE_T_MAX ;
132
+ obuflen = DEF_BUF_SIZE ;
133
+ } else {
134
+ // Assume that decompressor is used in file decompression with a fixed
135
+ // block size of max_length. In that case we will reach max_length almost
136
+ // always (except at the end of the file). So it makes sense to allocate
137
+ // max_length.
138
+ hard_limit = max_length ;
128
139
obuflen = max_length ;
129
-
140
+ if (obuflen > DEF_MAX_INITIAL_BUF_SIZE ){
141
+ // Safeguard against memory overflow.
142
+ obuflen == DEF_MAX_INITIAL_BUF_SIZE ;
143
+ }
144
+ }
130
145
131
146
do {
132
147
arrange_input_buffer (& (self -> state .avail_in ), & (self -> avail_in_real ));
@@ -136,7 +151,7 @@ decompress_buf(IgzipDecompressor *self, Py_ssize_t max_length)
136
151
& (self -> state .next_out ),
137
152
& RetVal ,
138
153
obuflen ,
139
- max_length );
154
+ hard_limit );
140
155
if (obuflen == -1 ){
141
156
PyErr_SetString (PyExc_MemoryError ,
142
157
"Unsufficient memory for buffer allocation" );
@@ -176,12 +191,6 @@ decompress(IgzipDecompressor *self, uint8_t *data, size_t len, Py_ssize_t max_le
176
191
char input_buffer_in_use ;
177
192
PyObject * result ;
178
193
179
- Py_ssize_t hard_limit ;
180
- if (max_length < 0 ) {
181
- hard_limit = PY_SSIZE_T_MAX ;
182
- } else {
183
- hard_limit = max_length ;
184
- }
185
194
/* Prepend unconsumed input if necessary */
186
195
if (self -> state .next_in != NULL ) {
187
196
size_t avail_now , avail_total ;
@@ -227,7 +236,7 @@ decompress(IgzipDecompressor *self, uint8_t *data, size_t len, Py_ssize_t max_le
227
236
input_buffer_in_use = 0 ;
228
237
}
229
238
230
- result = decompress_buf (self , hard_limit );
239
+ result = decompress_buf (self , max_length );
231
240
if (result == NULL ) {
232
241
self -> state .next_in = NULL ;
233
242
return NULL ;
0 commit comments