[Python-checkins] CVS: python/dist/src/Python getargs.c,2.80,2.81

Tim Peters tim_one@users.sourceforge.net
Fri, 26 Oct 2001 23:42:18 -0700


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

Modified Files:
	getargs.c 
Log Message:
vgetargskeywords:  Verify kwlist has the required length while parsing
the format, instead of waiting until after we can overindex it by
mistake.


Index: getargs.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/getargs.c,v
retrieving revision 2.80
retrieving revision 2.81
diff -C2 -d -r2.80 -r2.81
*** getargs.c	2001/10/27 06:14:32	2.80
--- getargs.c	2001/10/27 06:42:16	2.81
***************
*** 1033,1039 ****
  	int min, max;
  	char *formatsave;
! 	int i, len, nargs, nkeywords;
  	char *msg, *ks, **p;
! 	int nkwlist, pos, match, converted;
  	PyObject *key, *value;
  
--- 1033,1039 ----
  	int min, max;
  	char *formatsave;
! 	int i, len, nargs, nkeywords, nkwlist;
  	char *msg, *ks, **p;
! 	int pos, match, converted;
  	PyObject *key, *value;
  
***************
*** 1049,1061 ****
  	   min <- # of required arguments, or -1 if all are required.
  	   max <- most arguments (required + optional).
  	   Raise error if a tuple arg spec is found.
  	*/
  	fname = message = NULL;
  	formatsave = format;
  	min = -1;
  	max = 0;
  	while ((i = *format++) != '\0') {
! 		if (isalpha(i) && i != 'e')
  			max++;
  		else if (i == '|')
  			min = max;
--- 1049,1070 ----
  	   min <- # of required arguments, or -1 if all are required.
  	   max <- most arguments (required + optional).
+ 	   Check that kwlist has a non-NULL entry for each arg.
  	   Raise error if a tuple arg spec is found.
  	*/
  	fname = message = NULL;
  	formatsave = format;
+ 	p = kwlist;
  	min = -1;
  	max = 0;
  	while ((i = *format++) != '\0') {
! 		if (isalpha(i) && i != 'e') {
  			max++;
+ 			if (*p == NULL) {
+ 				/* kwlist is too short */
+ 				PyErr_BadInternalCall();
+ 				return 0;
+ 			}
+ 			p++;
+ 		}
  		else if (i == '|')
  			min = max;
***************
*** 1073,1083 ****
  			return 0;
  		}
! 	}	
  	if (min < 0) {
  		/* All arguments are required. */
  		min = max;
  	}
- 	format = formatsave;
  
  	nargs = PyTuple_GET_SIZE(args);
  	nkeywords = keywords == NULL ? 0 : PyDict_Size(keywords);
--- 1082,1098 ----
  			return 0;
  		}
! 	}
! 	format = formatsave;
! 	if (*p != NULL) {
! 		/* kwlist is too long */
! 		PyErr_BadInternalCall();
! 		return 0;
! 	}
  	if (min < 0) {
  		/* All arguments are required. */
  		min = max;
  	}
  
+ 	nkwlist = max;
  	nargs = PyTuple_GET_SIZE(args);
  	nkeywords = keywords == NULL ? 0 : PyDict_Size(keywords);
***************
*** 1104,1110 ****
  
  	/* required arguments missing from args can be supplied by keyword 
! 	   arguments */
  	len = nargs;
! 	if (keywords && nargs < min) {
  		for (i = nargs; i < min; i++) {
  			if (PyDict_GetItemString(keywords, kwlist[i]))
--- 1119,1127 ----
  
  	/* required arguments missing from args can be supplied by keyword 
! 	   arguments; set len to the number of posiitional arguments, and,
! 	   if that's less than the minimum required, add in the number of
! 	   required arguments that are supplied by keywords */
  	len = nargs;
! 	if (nkeywords > 0 && nargs < min) {
  		for (i = nargs; i < min; i++) {
  			if (PyDict_GetItemString(keywords, kwlist[i]))
***************
*** 1119,1123 ****
  	   which are supplied, but don't match the required arguments
  	   are not included in the "%d given" part of the message */
- 
  	if (len < min || max < len) {
  		if (message == NULL) {
--- 1136,1139 ----
***************
*** 1151,1166 ****
  	if (nkeywords == 0)
  		return 1; 
- 
- 	/* make sure the number of keywords in the keyword list matches the 
- 	   number of items in the format string */
- 	nkwlist = 0;
- 	p =  kwlist;
- 	while (*p++)
- 		nkwlist++;
- 	if (nkwlist != max) {
- 		PyErr_SetString(PyExc_SystemError,
- 	  "number of items in format string and keyword list do not match");
- 		return 0;
- 	}	  	  
  
  	/* convert the keyword arguments; this uses the format 
--- 1167,1170 ----