Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8ce6806

Browse files
committed
add overflow checking (closes #23361)
1 parent dee948b commit 8ce6806

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Core and Builtins
1616
Library
1717
-------
1818

19+
- Issue #23361: Fix possible overflow in Windows subprocess creation code.
20+
1921
- Issue #23363: Fix possible overflow in itertools.permutations.
2022

2123
- Issue #23364: Fix possible overflow in itertools.product.

Modules/_winapi.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -513,13 +513,23 @@ getenvironment(PyObject* environment)
513513
"environment can only contain strings");
514514
goto error;
515515
}
516+
if (totalsize > PY_SSIZE_T_MAX - PyUnicode_GET_LENGTH(key) - 1) {
517+
PyErr_SetString(PyExc_OverflowError, "environment too long");
518+
goto error;
519+
}
516520
totalsize += PyUnicode_GET_LENGTH(key) + 1; /* +1 for '=' */
521+
if (totalsize > PY_SSIZE_T_MAX - PyUnicode_GET_LENGTH(value) - 1) {
522+
PyErr_SetString(PyExc_OverflowError, "environment too long");
523+
goto error;
524+
}
517525
totalsize += PyUnicode_GET_LENGTH(value) + 1; /* +1 for '\0' */
518526
}
519527

520-
buffer = PyMem_Malloc(totalsize * sizeof(Py_UCS4));
521-
if (! buffer)
528+
buffer = PyMem_NEW(Py_UCS4, totalsize);
529+
if (! buffer) {
530+
PyErr_NoMemory();
522531
goto error;
532+
}
523533
p = buffer;
524534
end = buffer + totalsize;
525535

0 commit comments

Comments
 (0)