summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorTim Peters <tim.peters@gmail.com>2001-10-27 07:25:06 (GMT)
committerTim Peters <tim.peters@gmail.com>2001-10-27 07:25:06 (GMT)
commitc2f011201ae374da4544b93aba09d0279c139c18 (patch)
tree56c9247f7855e32691c240cb27a3ba1e930801eb /Python
parentb639d49798ed25d6a79175696a0786da442a6fcc (diff)
downloadcpython-c2f011201ae374da4544b93aba09d0279c139c18.zip
cpython-c2f011201ae374da4544b93aba09d0279c139c18.tar.gz
cpython-c2f011201ae374da4544b93aba09d0279c139c18.tar.bz2
vgetargskeywords()
+ Squash another potential buffer overrun. + Simplify the keyword-arg loop by decrementing the count of keywords remaining instead of incrementing Yet Another Variable; also break out early if the number of keyword args remaining hits 0. Since I hit the function's closing curly brace with this patch, that's enough of this for now <wink>.
Diffstat (limited to 'Python')
-rw-r--r--Python/getargs.c33
1 files changed, 17 insertions, 16 deletions
diff --git a/Python/getargs.c b/Python/getargs.c
index c65c149..248def3 100644
--- a/Python/getargs.c
+++ b/Python/getargs.c
@@ -1033,9 +1033,7 @@ vgetargskeywords(PyObject *args, PyObject *keywords, char *format,
int min, max;
char *formatsave;
int i, len, nargs, nkeywords;
- char *msg, *ks, **p;
- int pos, match, converted;
- PyObject *key, *value;
+ char *msg, **p;
assert(args != NULL && PyTuple_Check(args));
assert(keywords == NULL || PyDict_Check(keywords));
@@ -1150,7 +1148,8 @@ vgetargskeywords(PyObject *args, PyObject *keywords, char *format,
PyErr_SetString(PyExc_TypeError, message);
return 0;
}
-
+
+ /* convert the positional arguments */
for (i = 0; i < nargs; i++) {
if (*format == '|')
format++;
@@ -1162,13 +1161,12 @@ vgetargskeywords(PyObject *args, PyObject *keywords, char *format,
}
}
- /* handle no keyword parameters in call */
+ /* handle no keyword parameters in call */
if (nkeywords == 0)
return 1;
/* convert the keyword arguments; this uses the format
string where it was left after processing args */
- converted = 0;
for (i = nargs; i < max; i++) {
PyObject *item;
if (*format == '|')
@@ -1182,7 +1180,9 @@ vgetargskeywords(PyObject *args, PyObject *keywords, char *format,
seterror(i+1, msg, levels, fname, message);
return 0;
}
- converted++;
+ --nkeywords;
+ if (nkeywords == 0)
+ break;
}
else if (PyErr_Occurred())
return 0;
@@ -1196,11 +1196,12 @@ vgetargskeywords(PyObject *args, PyObject *keywords, char *format,
}
/* make sure there are no extraneous keyword arguments */
- pos = 0;
- if (converted < nkeywords) {
+ if (nkeywords > 0) {
+ PyObject *key, *value;
+ int pos = 0;
while (PyDict_Next(keywords, &pos, &key, &value)) {
- match = 0;
- ks = PyString_AsString(key);
+ int match = 0;
+ char *ks = PyString_AsString(key);
for (i = 0; i < max; i++) {
if (!strcmp(ks, kwlist[i])) {
match = 1;
@@ -1208,15 +1209,15 @@ vgetargskeywords(PyObject *args, PyObject *keywords, char *format,
}
}
if (!match) {
- sprintf(msgbuf,
- "%s is an invalid keyword argument for this function",
- ks);
- PyErr_SetString(PyExc_TypeError, msgbuf);
+ PyErr_Format(PyExc_TypeError,
+ "'%s' is an invalid keyword "
+ "argument for this function",
+ ks);
return 0;
}
}
}
-
+
return 1;
}