summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2022-07-28 06:52:00 (GMT)
committerGitHub <noreply@github.com>2022-07-28 06:52:00 (GMT)
commit563f0584c9aa80457f3478c2470b603721479eb0 (patch)
tree60099946f54a6c2189f08d9505c0ddc9c86d0595 /Python
parentfb422147d39eba4b56b9bb9d9be73d07e748dda9 (diff)
downloadcpython-563f0584c9aa80457f3478c2470b603721479eb0.zip
cpython-563f0584c9aa80457f3478c2470b603721479eb0.tar.gz
cpython-563f0584c9aa80457f3478c2470b603721479eb0.tar.bz2
[3.10] gh-94938: Fix errror detection of unexpected keyword arguments (GH-94999) (GH-95354)
When keyword argument name is an instance of a str subclass with overloaded methods __eq__ and __hash__, the former code could not find the name of an extraneous keyword argument to report an error, and _PyArg_UnpackKeywords() returned success without setting the corresponding cell in the linearized arguments array. But since the number of expected initialized cells is determined as the total number of passed arguments, this lead to reading NULL as a keyword parameter value, that caused SystemError or crash or other undesired behavior.. (cherry picked from commit ebad53a4dc1bb591820724a22cef9b8459185b5f) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Diffstat (limited to 'Python')
-rw-r--r--Python/getargs.c111
1 files changed, 55 insertions, 56 deletions
diff --git a/Python/getargs.c b/Python/getargs.c
index d92ea2d..fd49ee0 100644
--- a/Python/getargs.c
+++ b/Python/getargs.c
@@ -1553,6 +1553,50 @@ _PyArg_VaParseTupleAndKeywordsFast_SizeT(PyObject *args, PyObject *keywords,
return retval;
}
+static void
+error_unexpected_keyword_arg(PyObject *kwargs, PyObject *kwnames, PyObject *kwtuple, const char *fname)
+{
+ /* make sure there are no extraneous keyword arguments */
+ Py_ssize_t j = 0;
+ while (1) {
+ PyObject *keyword;
+ if (kwargs != NULL) {
+ if (!PyDict_Next(kwargs, &j, &keyword, NULL))
+ break;
+ }
+ else {
+ if (j >= PyTuple_GET_SIZE(kwnames))
+ break;
+ keyword = PyTuple_GET_ITEM(kwnames, j);
+ j++;
+ }
+ if (!PyUnicode_Check(keyword)) {
+ PyErr_SetString(PyExc_TypeError,
+ "keywords must be strings");
+ return;
+ }
+
+ int match = PySequence_Contains(kwtuple, keyword);
+ if (match <= 0) {
+ if (!match) {
+ PyErr_Format(PyExc_TypeError,
+ "'%S' is an invalid keyword "
+ "argument for %.200s%s",
+ keyword,
+ (fname == NULL) ? "this function" : fname,
+ (fname == NULL) ? "" : "()");
+ }
+ return;
+ }
+ }
+ /* Something wrong happened. There are extraneous keyword arguments,
+ * but we don't know what. And we don't bother. */
+ PyErr_Format(PyExc_TypeError,
+ "invalid keyword argument for %.200s%s",
+ (fname == NULL) ? "this function" : fname,
+ (fname == NULL) ? "" : "()");
+}
+
int
PyArg_ValidateKeywordArguments(PyObject *kwargs)
{
@@ -1841,6 +1885,13 @@ vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format,
return cleanreturn(0, &freelist);
}
}
+ /* Something wrong happened. There are extraneous keyword arguments,
+ * but we don't know what. And we don't bother. */
+ PyErr_Format(PyExc_TypeError,
+ "invalid keyword argument for %.200s%s",
+ (fname == NULL) ? "this function" : fname,
+ (fname == NULL) ? "" : "()");
+ return cleanreturn(0, &freelist);
}
return cleanreturn(1, &freelist);
@@ -2183,7 +2234,6 @@ vgetargskeywordsfast_impl(PyObject *const *args, Py_ssize_t nargs,
assert(IS_END_OF_FORMAT(*format) || (*format == '|') || (*format == '$'));
if (nkwargs > 0) {
- Py_ssize_t j;
/* make sure there are no arguments given by name and position */
for (i = pos; i < nargs; i++) {
keyword = PyTuple_GET_ITEM(kwtuple, i - pos);
@@ -2207,34 +2257,9 @@ vgetargskeywordsfast_impl(PyObject *const *args, Py_ssize_t nargs,
return cleanreturn(0, &freelist);
}
}
- /* make sure there are no extraneous keyword arguments */
- j = 0;
- while (1) {
- int match;
- if (kwargs != NULL) {
- if (!PyDict_Next(kwargs, &j, &keyword, NULL))
- break;
- }
- else {
- if (j >= PyTuple_GET_SIZE(kwnames))
- break;
- keyword = PyTuple_GET_ITEM(kwnames, j);
- j++;
- }
- match = PySequence_Contains(kwtuple, keyword);
- if (match <= 0) {
- if (!match) {
- PyErr_Format(PyExc_TypeError,
- "'%S' is an invalid keyword "
- "argument for %.200s%s",
- keyword,
- (parser->fname == NULL) ? "this function" : parser->fname,
- (parser->fname == NULL) ? "" : "()");
- }
- return cleanreturn(0, &freelist);
- }
- }
+ error_unexpected_keyword_arg(kwargs, kwnames, kwtuple, parser->fname);
+ return cleanreturn(0, &freelist);
}
return cleanreturn(1, &freelist);
@@ -2408,7 +2433,6 @@ _PyArg_UnpackKeywords(PyObject *const *args, Py_ssize_t nargs,
}
if (nkwargs > 0) {
- Py_ssize_t j;
/* make sure there are no arguments given by name and position */
for (i = posonly; i < nargs; i++) {
keyword = PyTuple_GET_ITEM(kwtuple, i - posonly);
@@ -2432,34 +2456,9 @@ _PyArg_UnpackKeywords(PyObject *const *args, Py_ssize_t nargs,
return NULL;
}
}
- /* make sure there are no extraneous keyword arguments */
- j = 0;
- while (1) {
- int match;
- if (kwargs != NULL) {
- if (!PyDict_Next(kwargs, &j, &keyword, NULL))
- break;
- }
- else {
- if (j >= PyTuple_GET_SIZE(kwnames))
- break;
- keyword = PyTuple_GET_ITEM(kwnames, j);
- j++;
- }
- match = PySequence_Contains(kwtuple, keyword);
- if (match <= 0) {
- if (!match) {
- PyErr_Format(PyExc_TypeError,
- "'%S' is an invalid keyword "
- "argument for %.200s%s",
- keyword,
- (parser->fname == NULL) ? "this function" : parser->fname,
- (parser->fname == NULL) ? "" : "()");
- }
- return NULL;
- }
- }
+ error_unexpected_keyword_arg(kwargs, kwnames, kwtuple, parser->fname);
+ return NULL;
}
return buf;