FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-101178: Fix possible integer overflow in Ascii85 encoder with wrapcol=1 by serhiy-storchaka · Pull Request #144778 · python/cpython · GitHub

Repository navigation

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
25 changes: 8 additions & 17 deletions Modules/binascii.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -785,22 +785,21 @@ binascii_b2a_base64_impl(PyObject *module, Py_buffer *data, size_t wrapcol,
* Use unsigned integer arithmetic to avoid signed integer overflow.
*/
size_t out_len = ((size_t)bin_len + 2u) / 3u * 4u;
if (out_len > PY_SSIZE_T_MAX) {
goto toolong;
}
if (wrapcol && out_len) {
/* Each line should encode a whole number of bytes. */
wrapcol = wrapcol < 4 ? 4 : wrapcol / 4 * 4;
out_len += (out_len - 1u) / wrapcol;
if (out_len > PY_SSIZE_T_MAX) {
goto toolong;
}
}
if (newline) {
out_len++;
if (out_len > PY_SSIZE_T_MAX) {
goto toolong;
}
if (out_len > PY_SSIZE_T_MAX) {
binascii_state *state = get_binascii_state(module);
if (state == NULL) {
return NULL;
}
PyErr_SetString(state->Error, "Too much data for base64");
return NULL;
}
PyBytesWriter *writer = PyBytesWriter_Create(out_len);
if (writer == NULL) {
Expand Down Expand Up @@ -841,14 +840,6 @@ binascii_b2a_base64_impl(PyObject *module, Py_buffer *data, size_t wrapcol,
*ascii_data++ = '\n'; /* Append a courtesy newline */

return PyBytesWriter_FinishWithPointer(writer, ascii_data);

toolong:;
binascii_state *state = get_binascii_state(module);
if (state == NULL) {
return NULL;
}
PyErr_SetString(state->Error, "Too much data for base64");
return NULL;
}

/*[clinic input]
Expand Down Expand Up @@ -1046,7 +1037,7 @@ binascii_b2a_ascii85_impl(PyObject *module, Py_buffer *data, int foldspaces,
if (!pad && (bin_len % 4)) {
out_len -= 4 - (bin_len % 4);
}
if (wrapcol && out_len) {
if (wrapcol && out_len && out_len <= PY_SSIZE_T_MAX) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Let me know if I'm misunderstanding, but wouldn't we need to "error out" if the out_len would overflow here instead of not expanding? The larger amount of data will still be written outside ascii_data whether out_len is expanded or not?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

We error out in the next "if".

out_len += (out_len - 1) / wrapcol;
}
if (out_len > PY_SSIZE_T_MAX) {
Expand Down
Loading

Back | FazBrowse Home | New Git URL