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

Skip to content

Commit 3dec84f

Browse files
bpo-38540: Fix possible leak in PyArg_Parse for "esGH-" and "etGH-". (GH-16869)
(cherry picked from commit 5bc6a7c) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent e945052 commit 3dec84f

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fixed possible leak in :c:func:`PyArg_Parse` and similar functions for
2+
format units ``"es#"`` and ``"et#"`` when the macro
3+
:c:macro:`PY_SSIZE_T_CLEAN` is not defined.

Python/getargs.c

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,7 +1176,19 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
11761176
trailing 0-byte
11771177
11781178
*/
1179-
FETCH_SIZE;
1179+
int *q = NULL; Py_ssize_t *q2 = NULL;
1180+
if (flags & FLAG_SIZE_T) {
1181+
q2 = va_arg(*p_va, Py_ssize_t*);
1182+
}
1183+
else {
1184+
if (PyErr_WarnEx(PyExc_DeprecationWarning,
1185+
"PY_SSIZE_T_CLEAN will be required for '#' formats", 1))
1186+
{
1187+
Py_DECREF(s);
1188+
return NULL;
1189+
}
1190+
q = va_arg(*p_va, int*);
1191+
}
11801192

11811193
format++;
11821194
if (q == NULL && q2 == NULL) {
@@ -1209,7 +1221,19 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
12091221
}
12101222
}
12111223
memcpy(*buffer, ptr, size+1);
1212-
STORE_SIZE(size);
1224+
1225+
if (flags & FLAG_SIZE_T) {
1226+
*q2 = size;
1227+
}
1228+
else {
1229+
if (INT_MAX < size) {
1230+
Py_DECREF(s);
1231+
PyErr_SetString(PyExc_OverflowError,
1232+
"size does not fit in an int");
1233+
return converterr("", arg, msgbuf, bufsize);
1234+
}
1235+
*q = (int)size;
1236+
}
12131237
} else {
12141238
/* Using a 0-terminated buffer:
12151239

0 commit comments

Comments
 (0)