Skip to content

Commit 10ecaa2

Browse files
committed
merge 3.3 (closes #23165)
2 parents 5719ef1 + 72c2a0f commit 10ecaa2

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

‎Misc/NEWS‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ Core and Builtins
3838

3939
- Issue #22518: Fix integer overflow issues in latin-1 encoding.
4040

41+
- Issue #23165: Perform overflow checks before allocating memory in the
42+
_Py_char2wchar function.
43+
4144
Library
4245
-------
4346

‎Python/fileutils.c‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,11 @@ decode_ascii_surrogateescape(const char *arg, size_t *size)
220220
wchar_t *res;
221221
unsigned char *in;
222222
wchar_t *out;
223+
size_t argsize = strlen(arg) + 1;
223224

224-
res = PyMem_RawMalloc((strlen(arg)+1)*sizeof(wchar_t));
225+
if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t))
226+
return NULL;
227+
res = PyMem_RawMalloc(argsize*sizeof(wchar_t));
225228
if (!res)
226229
return NULL;
227230

@@ -303,10 +306,15 @@ _Py_char2wchar(const char* arg, size_t *size)
303306
argsize = mbstowcs(NULL, arg, 0);
304307
#endif
305308
if (argsize != (size_t)-1) {
306-
res = (wchar_t *)PyMem_RawMalloc((argsize+1)*sizeof(wchar_t));
309+
if (argsize == PY_SSIZE_T_MAX)
310+
goto oom;
311+
argsize += 1;
312+
if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t))
313+
goto oom;
314+
res = (wchar_t *)PyMem_RawMalloc(argsize*sizeof(wchar_t));
307315
if (!res)
308316
goto oom;
309-
count = mbstowcs(res, arg, argsize+1);
317+
count = mbstowcs(res, arg, argsize);
310318
if (count != (size_t)-1) {
311319
wchar_t *tmp;
312320
/* Only use the result if it contains no
@@ -329,6 +337,8 @@ _Py_char2wchar(const char* arg, size_t *size)
329337
/* Overallocate; as multi-byte characters are in the argument, the
330338
actual output could use less memory. */
331339
argsize = strlen(arg) + 1;
340+
if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t))
341+
goto oom;
332342
res = (wchar_t*)PyMem_RawMalloc(argsize*sizeof(wchar_t));
333343
if (!res)
334344
goto oom;

0 commit comments

Comments
 (0)