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

Skip to content

Commit b054be4

Browse files
committed
vgetargskeywords:
+ Generally test nkeywords against 0 instead of keywords against NULL (saves a little work if an empty keywords dict is passed, and is conceptually more on-target regardless). + When a call erroneously specifies a keyword argument both by position and by keyword name: - It was easy to provoke this routine into an internal buffer overrun by using a long argument name. Now uses PyErr_format instead (which computes a safe buffer size). - Improved the error msg.
1 parent b0872fc commit b054be4

1 file changed

Lines changed: 9 additions & 16 deletions

File tree

Python/getargs.c

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,16 +1085,16 @@ vgetargskeywords(PyObject *args, PyObject *keywords, char *format,
10851085
/* make sure there are no duplicate values for an argument;
10861086
its not clear when to use the term "keyword argument vs.
10871087
keyword parameter in messages */
1088-
if (keywords) {
1088+
if (nkeywords > 0) {
10891089
for (i = 0; i < nargs; i++) {
10901090
char *thiskw = kwlist[i];
10911091
if (thiskw == NULL)
10921092
break;
10931093
if (PyMapping_HasKeyString(keywords, thiskw)) {
1094-
sprintf(msgbuf,
1095-
"keyword parameter %s redefined",
1094+
PyErr_Format(PyExc_TypeError,
1095+
"keyword parameter '%s' was given "
1096+
"by position and by name",
10961097
thiskw);
1097-
PyErr_SetString(PyExc_TypeError, msgbuf);
10981098
return 0;
10991099
}
11001100
}
@@ -1155,29 +1155,23 @@ vgetargskeywords(PyObject *args, PyObject *keywords, char *format,
11551155
}
11561156

11571157
/* handle no keyword parameters in call */
1158-
1159-
if (!keywords)
1158+
if (nkeywords == 0)
11601159
return 1;
1161-
1160+
11621161
/* make sure the number of keywords in the keyword list matches the
11631162
number of items in the format string */
1164-
11651163
nkwlist = 0;
11661164
p = kwlist;
1167-
for (;;) {
1168-
if (!*(p++)) break;
1165+
while (*p++)
11691166
nkwlist++;
1170-
}
1171-
11721167
if (nkwlist != max) {
11731168
PyErr_SetString(PyExc_SystemError,
11741169
"number of items in format string and keyword list do not match");
11751170
return 0;
11761171
}
1177-
1172+
11781173
/* convert the keyword arguments; this uses the format
11791174
string where it was left after processing args */
1180-
11811175
converted = 0;
11821176
for (i = nargs; i < nkwlist; i++) {
11831177
PyObject *item;
@@ -1202,9 +1196,8 @@ vgetargskeywords(PyObject *args, PyObject *keywords, char *format,
12021196
}
12031197
}
12041198
}
1205-
1199+
12061200
/* make sure there are no extraneous keyword arguments */
1207-
12081201
pos = 0;
12091202
if (converted < nkeywords) {
12101203
while (PyDict_Next(keywords, &pos, &key, &value)) {

0 commit comments

Comments
 (0)