[Python-checkins] GH-100342: check for allocation failure in AC `*args` parsing (#100343)

kumaraditya303 webhook-mailer at python.org
Tue Dec 27 23:16:34 EST 2022


https://github.com/python/cpython/commit/7cf164ad5e3c8c6af5ae8813ad6a784448605418
commit: 7cf164ad5e3c8c6af5ae8813ad6a784448605418
branch: main
author: Kumar Aditya <59607654+kumaraditya303 at users.noreply.github.com>
committer: kumaraditya303 <59607654+kumaraditya303 at users.noreply.github.com>
date: 2022-12-28T09:46:28+05:30
summary:

GH-100342: check for allocation failure in AC `*args` parsing (#100343)

files:
A Misc/NEWS.d/next/Tools-Demos/2022-12-19-10-08-53.gh-issue-100342.qDFlQG.rst
M Lib/test/clinic.test
M Modules/clinic/_testclinic.c.h
M Tools/clinic/clinic.py

diff --git a/Lib/test/clinic.test b/Lib/test/clinic.test
index 0d844234d9d1..53e5df5ba872 100644
--- a/Lib/test/clinic.test
+++ b/Lib/test/clinic.test
@@ -3781,6 +3781,9 @@ test_vararg_and_posonly(PyObject *module, PyObject *const *args, Py_ssize_t narg
     }
     a = args[0];
     __clinic_args = PyTuple_New(nargs - 1);
+    if (!__clinic_args) {
+        goto exit;
+    }
     for (Py_ssize_t i = 0; i < nargs - 1; ++i) {
         PyTuple_SET_ITEM(__clinic_args, i, Py_NewRef(args[1 + i]));
     }
@@ -3793,7 +3796,7 @@ exit:
 
 static PyObject *
 test_vararg_and_posonly_impl(PyObject *module, PyObject *a, PyObject *args)
-/*[clinic end generated code: output=081a953b8cbe7617 input=08dc2bf7afbf1613]*/
+/*[clinic end generated code: output=79b75dc07decc8d6 input=08dc2bf7afbf1613]*/
 
 /*[clinic input]
 test_vararg
diff --git a/Misc/NEWS.d/next/Tools-Demos/2022-12-19-10-08-53.gh-issue-100342.qDFlQG.rst b/Misc/NEWS.d/next/Tools-Demos/2022-12-19-10-08-53.gh-issue-100342.qDFlQG.rst
new file mode 100644
index 000000000000..28f736337526
--- /dev/null
+++ b/Misc/NEWS.d/next/Tools-Demos/2022-12-19-10-08-53.gh-issue-100342.qDFlQG.rst
@@ -0,0 +1 @@
+Add missing ``NULL`` check for possible allocation failure in ``*args`` parsing in Argument Clinic.
diff --git a/Modules/clinic/_testclinic.c.h b/Modules/clinic/_testclinic.c.h
index 21bde5294702..831f58ca650a 100644
--- a/Modules/clinic/_testclinic.c.h
+++ b/Modules/clinic/_testclinic.c.h
@@ -2409,6 +2409,9 @@ vararg_and_posonly(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
     }
     a = args[0];
     __clinic_args = PyTuple_New(nargs - 1);
+    if (!__clinic_args) {
+        goto exit;
+    }
     for (Py_ssize_t i = 0; i < nargs - 1; ++i) {
         PyTuple_SET_ITEM(__clinic_args, i, Py_NewRef(args[1 + i]));
     }
@@ -2769,6 +2772,9 @@ gh_99233_refcount(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
         goto exit;
     }
     __clinic_args = PyTuple_New(nargs - 0);
+    if (!__clinic_args) {
+        goto exit;
+    }
     for (Py_ssize_t i = 0; i < nargs - 0; ++i) {
         PyTuple_SET_ITEM(__clinic_args, i, Py_NewRef(args[0 + i]));
     }
@@ -2811,4 +2817,4 @@ gh_99240_double_free(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
 exit:
     return return_value;
 }
-/*[clinic end generated code: output=9a5ca5909c087102 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=e8211606b03d733a input=a9049054013a1b77]*/
diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py
index 2fb1902a5b54..552ed2c889a7 100755
--- a/Tools/clinic/clinic.py
+++ b/Tools/clinic/clinic.py
@@ -960,12 +960,16 @@ def parser_body(prototype, *fields, declarations=''):
                     if not new_or_init:
                         parser_code.append(normalize_snippet("""
                             %s = PyTuple_New(%s);
+                            if (!%s) {{
+                                goto exit;
+                            }}
                             for (Py_ssize_t i = 0; i < %s; ++i) {{
                                 PyTuple_SET_ITEM(%s, i, Py_NewRef(args[%d + i]));
                             }}
                             """ % (
                                 p.converter.parser_name,
                                 left_args,
+                                p.converter.parser_name,
                                 left_args,
                                 p.converter.parser_name,
                                 max_pos



More information about the Python-checkins mailing list