[Python-checkins] CVS: python/dist/src/Python getargs.c,2.83,2.84

Tim Peters tim_one@users.sourceforge.net
Sat, 27 Oct 2001 00:25:11 -0700


Update of /cvsroot/python/python/dist/src/Python
In directory usw-pr-cvs1:/tmp/cvs-serv22673/python/Python

Modified Files:
	getargs.c 
Log Message:
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>.


Index: getargs.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/getargs.c,v
retrieving revision 2.83
retrieving revision 2.84
diff -C2 -d -r2.83 -r2.84
*** getargs.c	2001/10/27 07:00:56	2.83
--- getargs.c	2001/10/27 07:25:06	2.84
***************
*** 1034,1040 ****
  	char *formatsave;
  	int i, len, nargs, nkeywords;
! 	char *msg, *ks, **p;
! 	int pos, match, converted;
! 	PyObject *key, *value;
  
  	assert(args != NULL && PyTuple_Check(args));
--- 1034,1038 ----
  	char *formatsave;
  	int i, len, nargs, nkeywords;
! 	char *msg, **p;
  
  	assert(args != NULL && PyTuple_Check(args));
***************
*** 1151,1155 ****
  		return 0;
  	}
! 	
  	for (i = 0; i < nargs; i++) {
  		if (*format == '|')
--- 1149,1154 ----
  		return 0;
  	}
! 
! 	/* convert the positional arguments */
  	for (i = 0; i < nargs; i++) {
  		if (*format == '|')
***************
*** 1163,1167 ****
  	}
  
! 	/* handle no keyword parameters in call  */	
  	if (nkeywords == 0)
  		return 1; 
--- 1162,1166 ----
  	}
  
! 	/* handle no keyword parameters in call */	
  	if (nkeywords == 0)
  		return 1; 
***************
*** 1169,1173 ****
  	/* 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;
--- 1168,1171 ----
***************
*** 1183,1187 ****
  				return 0;
  			}
! 			converted++;
  		}
  		else if (PyErr_Occurred())
--- 1181,1187 ----
  				return 0;
  			}
! 			--nkeywords;
! 			if (nkeywords == 0)
! 				break;
  		}
  		else if (PyErr_Occurred())
***************
*** 1197,1205 ****
  
  	/* make sure there are no extraneous keyword arguments */
! 	pos = 0;
! 	if (converted < nkeywords) {
  		while (PyDict_Next(keywords, &pos, &key, &value)) {
! 			match = 0;
! 			ks = PyString_AsString(key);
  			for (i = 0; i < max; i++) {
  				if (!strcmp(ks, kwlist[i])) {
--- 1197,1206 ----
  
  	/* make sure there are no extraneous keyword arguments */
! 	if (nkeywords > 0) {
! 		PyObject *key, *value;
! 		int pos = 0;
  		while (PyDict_Next(keywords, &pos, &key, &value)) {
! 			int match = 0;
! 			char *ks = PyString_AsString(key);
  			for (i = 0; i < max; i++) {
  				if (!strcmp(ks, kwlist[i])) {
***************
*** 1209,1221 ****
  			}
  			if (!match) {
! 				sprintf(msgbuf,
! 			"%s is an invalid keyword argument for this function",
! 					ks);
! 				PyErr_SetString(PyExc_TypeError, msgbuf);
  				return 0;
  			}
  		}
  	}
! 	
  	return 1;
  }
--- 1210,1222 ----
  			}
  			if (!match) {
! 				PyErr_Format(PyExc_TypeError,
! 					     "'%s' is an invalid keyword "
! 					     "argument for this function",
! 					     ks);
  				return 0;
  			}
  		}
  	}
! 
  	return 1;
  }