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

Skip to content

Commit 72c2a0f

Browse files
committed
merge 3.2 (closes #23165)
2 parents 7919acb + f18bf6f commit 72c2a0f

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
@@ -23,6 +23,9 @@ Core and Builtins
2323

2424
- Issue #22518: Fix integer overflow issues in latin-1 encoding.
2525

26+
- Issue #23165: Perform overflow checks before allocating memory in the
27+
_Py_char2wchar function.
28+
2629
Library
2730
-------
2831

Python/fileutils.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,11 @@ decode_ascii_surrogateescape(const char *arg, size_t *size)
201201
wchar_t *res;
202202
unsigned char *in;
203203
wchar_t *out;
204+
size_t argsize = strlen(arg) + 1;
204205

205-
res = PyMem_Malloc((strlen(arg)+1)*sizeof(wchar_t));
206+
if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t))
207+
return NULL;
208+
res = PyMem_Malloc(argsize*sizeof(wchar_t));
206209
if (!res)
207210
return NULL;
208211

@@ -284,10 +287,15 @@ _Py_char2wchar(const char* arg, size_t *size)
284287
argsize = mbstowcs(NULL, arg, 0);
285288
#endif
286289
if (argsize != (size_t)-1) {
287-
res = (wchar_t *)PyMem_Malloc((argsize+1)*sizeof(wchar_t));
290+
if (argsize == PY_SSIZE_T_MAX)
291+
goto oom;
292+
argsize += 1;
293+
if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t))
294+
goto oom;
295+
res = (wchar_t *)PyMem_Malloc(argsize*sizeof(wchar_t));
288296
if (!res)
289297
goto oom;
290-
count = mbstowcs(res, arg, argsize+1);
298+
count = mbstowcs(res, arg, argsize);
291299
if (count != (size_t)-1) {
292300
wchar_t *tmp;
293301
/* Only use the result if it contains no
@@ -310,6 +318,8 @@ _Py_char2wchar(const char* arg, size_t *size)
310318
/* Overallocate; as multi-byte characters are in the argument, the
311319
actual output could use less memory. */
312320
argsize = strlen(arg) + 1;
321+
if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t))
322+
goto oom;
313323
res = (wchar_t*)PyMem_Malloc(argsize*sizeof(wchar_t));
314324
if (!res)
315325
goto oom;

0 commit comments

Comments
 (0)