Skip to content

Commit 251ffa9

Browse files
author
Ma Lin
authored
bpo-41486: Fix initial buffer size can't > UINT32_MAX in zlib module (GH-25738)
* Fix initial buffer size can't > UINT32_MAX in zlib module After commit f9bedb6, in 64-bit build, if the initial buffer size > UINT32_MAX, ValueError will be raised. These two functions are affected: 1. zlib.decompress(data, /, wbits=MAX_WBITS, bufsize=DEF_BUF_SIZE) 2. zlib.Decompress.flush([length]) This commit re-allows the size > UINT32_MAX. * adds curly braces per PEP 7. * Renames `Buffer_*` to `OutputBuffer_*` for clarity
1 parent e467ec4 commit 251ffa9

File tree

3 files changed

+77
-71
lines changed

3 files changed

+77
-71
lines changed

Modules/_bz2module.c

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
/* On success, return value >= 0
1919
On failure, return -1 */
2020
static inline Py_ssize_t
21-
Buffer_InitAndGrow(_BlocksOutputBuffer *buffer, Py_ssize_t max_length,
22-
char **next_out, uint32_t *avail_out)
21+
OutputBuffer_InitAndGrow(_BlocksOutputBuffer *buffer, Py_ssize_t max_length,
22+
char **next_out, uint32_t *avail_out)
2323
{
2424
Py_ssize_t allocated;
2525

@@ -32,8 +32,8 @@ Buffer_InitAndGrow(_BlocksOutputBuffer *buffer, Py_ssize_t max_length,
3232
/* On success, return value >= 0
3333
On failure, return -1 */
3434
static inline Py_ssize_t
35-
Buffer_Grow(_BlocksOutputBuffer *buffer,
36-
char **next_out, uint32_t *avail_out)
35+
OutputBuffer_Grow(_BlocksOutputBuffer *buffer,
36+
char **next_out, uint32_t *avail_out)
3737
{
3838
Py_ssize_t allocated;
3939

@@ -44,19 +44,19 @@ Buffer_Grow(_BlocksOutputBuffer *buffer,
4444
}
4545

4646
static inline Py_ssize_t
47-
Buffer_GetDataSize(_BlocksOutputBuffer *buffer, uint32_t avail_out)
47+
OutputBuffer_GetDataSize(_BlocksOutputBuffer *buffer, uint32_t avail_out)
4848
{
4949
return _BlocksOutputBuffer_GetDataSize(buffer, (Py_ssize_t) avail_out);
5050
}
5151

5252
static inline PyObject *
53-
Buffer_Finish(_BlocksOutputBuffer *buffer, uint32_t avail_out)
53+
OutputBuffer_Finish(_BlocksOutputBuffer *buffer, uint32_t avail_out)
5454
{
5555
return _BlocksOutputBuffer_Finish(buffer, (Py_ssize_t) avail_out);
5656
}
5757

5858
static inline void
59-
Buffer_OnError(_BlocksOutputBuffer *buffer)
59+
OutputBuffer_OnError(_BlocksOutputBuffer *buffer)
6060
{
6161
_BlocksOutputBuffer_OnError(buffer);
6262
}
@@ -177,7 +177,7 @@ compress(BZ2Compressor *c, char *data, size_t len, int action)
177177
PyObject *result;
178178
_BlocksOutputBuffer buffer = {.list = NULL};
179179

180-
if (Buffer_InitAndGrow(&buffer, -1, &c->bzs.next_out, &c->bzs.avail_out) < 0) {
180+
if (OutputBuffer_InitAndGrow(&buffer, -1, &c->bzs.next_out, &c->bzs.avail_out) < 0) {
181181
goto error;
182182
}
183183
c->bzs.next_in = data;
@@ -198,7 +198,7 @@ compress(BZ2Compressor *c, char *data, size_t len, int action)
198198
break;
199199

200200
if (c->bzs.avail_out == 0) {
201-
if (Buffer_Grow(&buffer, &c->bzs.next_out, &c->bzs.avail_out) < 0) {
201+
if (OutputBuffer_Grow(&buffer, &c->bzs.next_out, &c->bzs.avail_out) < 0) {
202202
goto error;
203203
}
204204
}
@@ -215,13 +215,13 @@ compress(BZ2Compressor *c, char *data, size_t len, int action)
215215
break;
216216
}
217217

218-
result = Buffer_Finish(&buffer, c->bzs.avail_out);
218+
result = OutputBuffer_Finish(&buffer, c->bzs.avail_out);
219219
if (result != NULL) {
220220
return result;
221221
}
222222

223223
error:
224-
Buffer_OnError(&buffer);
224+
OutputBuffer_OnError(&buffer);
225225
return NULL;
226226
}
227227

@@ -442,7 +442,7 @@ decompress_buf(BZ2Decompressor *d, Py_ssize_t max_length)
442442
_BlocksOutputBuffer buffer = {.list = NULL};
443443
bz_stream *bzs = &d->bzs;
444444

445-
if (Buffer_InitAndGrow(&buffer, max_length, &bzs->next_out, &bzs->avail_out) < 0) {
445+
if (OutputBuffer_InitAndGrow(&buffer, max_length, &bzs->next_out, &bzs->avail_out) < 0) {
446446
goto error;
447447
}
448448

@@ -469,21 +469,22 @@ decompress_buf(BZ2Decompressor *d, Py_ssize_t max_length)
469469
} else if (d->bzs_avail_in_real == 0) {
470470
break;
471471
} else if (bzs->avail_out == 0) {
472-
if (Buffer_GetDataSize(&buffer, bzs->avail_out) == max_length)
472+
if (OutputBuffer_GetDataSize(&buffer, bzs->avail_out) == max_length) {
473473
break;
474-
if (Buffer_Grow(&buffer, &bzs->next_out, &bzs->avail_out) < 0) {
474+
}
475+
if (OutputBuffer_Grow(&buffer, &bzs->next_out, &bzs->avail_out) < 0) {
475476
goto error;
476477
}
477478
}
478479
}
479480

480-
result = Buffer_Finish(&buffer, bzs->avail_out);
481+
result = OutputBuffer_Finish(&buffer, bzs->avail_out);
481482
if (result != NULL) {
482483
return result;
483484
}
484485

485486
error:
486-
Buffer_OnError(&buffer);
487+
OutputBuffer_OnError(&buffer);
487488
return NULL;
488489
}
489490

Modules/_lzmamodule.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
/* On success, return value >= 0
2626
On failure, return -1 */
2727
static inline Py_ssize_t
28-
Buffer_InitAndGrow(_BlocksOutputBuffer *buffer, Py_ssize_t max_length,
29-
uint8_t **next_out, size_t *avail_out)
28+
OutputBuffer_InitAndGrow(_BlocksOutputBuffer *buffer, Py_ssize_t max_length,
29+
uint8_t **next_out, size_t *avail_out)
3030
{
3131
Py_ssize_t allocated;
3232

@@ -39,8 +39,8 @@ Buffer_InitAndGrow(_BlocksOutputBuffer *buffer, Py_ssize_t max_length,
3939
/* On success, return value >= 0
4040
On failure, return -1 */
4141
static inline Py_ssize_t
42-
Buffer_Grow(_BlocksOutputBuffer *buffer,
43-
uint8_t **next_out, size_t *avail_out)
42+
OutputBuffer_Grow(_BlocksOutputBuffer *buffer,
43+
uint8_t **next_out, size_t *avail_out)
4444
{
4545
Py_ssize_t allocated;
4646

@@ -51,19 +51,19 @@ Buffer_Grow(_BlocksOutputBuffer *buffer,
5151
}
5252

5353
static inline Py_ssize_t
54-
Buffer_GetDataSize(_BlocksOutputBuffer *buffer, size_t avail_out)
54+
OutputBuffer_GetDataSize(_BlocksOutputBuffer *buffer, size_t avail_out)
5555
{
5656
return _BlocksOutputBuffer_GetDataSize(buffer, (Py_ssize_t) avail_out);
5757
}
5858

5959
static inline PyObject *
60-
Buffer_Finish(_BlocksOutputBuffer *buffer, size_t avail_out)
60+
OutputBuffer_Finish(_BlocksOutputBuffer *buffer, size_t avail_out)
6161
{
6262
return _BlocksOutputBuffer_Finish(buffer, (Py_ssize_t) avail_out);
6363
}
6464

6565
static inline void
66-
Buffer_OnError(_BlocksOutputBuffer *buffer)
66+
OutputBuffer_OnError(_BlocksOutputBuffer *buffer)
6767
{
6868
_BlocksOutputBuffer_OnError(buffer);
6969
}
@@ -550,7 +550,7 @@ compress(Compressor *c, uint8_t *data, size_t len, lzma_action action)
550550
_lzma_state *state = PyType_GetModuleState(Py_TYPE(c));
551551
assert(state != NULL);
552552

553-
if (Buffer_InitAndGrow(&buffer, -1, &c->lzs.next_out, &c->lzs.avail_out) < 0) {
553+
if (OutputBuffer_InitAndGrow(&buffer, -1, &c->lzs.next_out, &c->lzs.avail_out) < 0) {
554554
goto error;
555555
}
556556
c->lzs.next_in = data;
@@ -573,19 +573,19 @@ compress(Compressor *c, uint8_t *data, size_t len, lzma_action action)
573573
(action == LZMA_FINISH && lzret == LZMA_STREAM_END)) {
574574
break;
575575
} else if (c->lzs.avail_out == 0) {
576-
if (Buffer_Grow(&buffer, &c->lzs.next_out, &c->lzs.avail_out) < 0) {
576+
if (OutputBuffer_Grow(&buffer, &c->lzs.next_out, &c->lzs.avail_out) < 0) {
577577
goto error;
578578
}
579579
}
580580
}
581581

582-
result = Buffer_Finish(&buffer, c->lzs.avail_out);
582+
result = OutputBuffer_Finish(&buffer, c->lzs.avail_out);
583583
if (result != NULL) {
584584
return result;
585585
}
586586

587587
error:
588-
Buffer_OnError(&buffer);
588+
OutputBuffer_OnError(&buffer);
589589
return NULL;
590590
}
591591

@@ -934,7 +934,7 @@ decompress_buf(Decompressor *d, Py_ssize_t max_length)
934934
_lzma_state *state = PyType_GetModuleState(Py_TYPE(d));
935935
assert(state != NULL);
936936

937-
if (Buffer_InitAndGrow(&buffer, max_length, &lzs->next_out, &lzs->avail_out) < 0) {
937+
if (OutputBuffer_InitAndGrow(&buffer, max_length, &lzs->next_out, &lzs->avail_out) < 0) {
938938
goto error;
939939
}
940940

@@ -962,24 +962,24 @@ decompress_buf(Decompressor *d, Py_ssize_t max_length)
962962
Maybe lzs's internal state still have a few bytes
963963
can be output, grow the output buffer and continue
964964
if max_lengh < 0. */
965-
if (Buffer_GetDataSize(&buffer, lzs->avail_out) == max_length) {
965+
if (OutputBuffer_GetDataSize(&buffer, lzs->avail_out) == max_length) {
966966
break;
967967
}
968-
if (Buffer_Grow(&buffer, &lzs->next_out, &lzs->avail_out) < 0) {
968+
if (OutputBuffer_Grow(&buffer, &lzs->next_out, &lzs->avail_out) < 0) {
969969
goto error;
970970
}
971971
} else if (lzs->avail_in == 0) {
972972
break;
973973
}
974974
}
975975

976-
result = Buffer_Finish(&buffer, lzs->avail_out);
976+
result = OutputBuffer_Finish(&buffer, lzs->avail_out);
977977
if (result != NULL) {
978978
return result;
979979
}
980980

981981
error:
982-
Buffer_OnError(&buffer);
982+
OutputBuffer_OnError(&buffer);
983983
return NULL;
984984
}
985985

0 commit comments

Comments
 (0)