Skip to content

Commit da165d1

Browse files
committed
Allow up to sys.maxsize output length when decoding base 85
Include an integer overflow check for Ascii85.
1 parent 2b2ecc4 commit da165d1

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

Modules/binascii.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -865,15 +865,25 @@ binascii_a2b_ascii85_impl(PyObject *module, Py_buffer *data, int fold_spaces,
865865
}
866866

867867
/* Allocate output buffer. */
868-
Py_ssize_t bin_len = ascii_len;
868+
size_t bin_len = ascii_len;
869869
unsigned char this_ch = 0;
870+
size_t count_yz = 0;
870871
for (Py_ssize_t i = 0; i < ascii_len; i++) {
871872
this_ch = ascii_data[i];
872873
if (this_ch == 'y' || this_ch == 'z') {
873-
bin_len += 4;
874+
count_yz++;
874875
}
875876
}
876-
bin_len = 4 * ((bin_len + 4) / 5);
877+
bin_len = (bin_len - count_yz + 4) / 5 * 4;
878+
if (count_yz > (PY_SSIZE_T_MAX - bin_len) / 4) {
879+
binascii_state *state = get_binascii_state(module);
880+
if (state == NULL) {
881+
return NULL;
882+
}
883+
PyErr_SetString(state->Error, "Too much Ascii85 data");
884+
return NULL;
885+
}
886+
bin_len += count_yz * 4;
877887

878888
PyBytesWriter *writer = PyBytesWriter_Create(bin_len);
879889
if (writer == NULL) {
@@ -1106,7 +1116,7 @@ internal_a2b_base85(PyObject *module, Py_buffer *data, int strict_mode,
11061116
assert(ascii_len >= 0);
11071117

11081118
/* Allocate output buffer. */
1109-
Py_ssize_t bin_len = 4 * ((ascii_len + 4) / 5);
1119+
size_t bin_len = ((size_t)ascii_len + 4) / 5 * 4;
11101120
PyBytesWriter *writer = PyBytesWriter_Create(bin_len);
11111121
if (writer == NULL) {
11121122
return NULL;

0 commit comments

Comments
 (0)