[Python-checkins] python/dist/src/Objects abstract.c,2.125,2.126

bcannon at users.sourceforge.net bcannon at users.sourceforge.net
Sat Mar 20 17:52:23 EST 2004


Update of /cvsroot/python/python/dist/src/Objects
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv16155/Objects
Modified Files:
	abstract.c 
Log Message:
Limit the nesting depth of a tuple passed as the second argument to
isinstance() or issubclass() to the recursion limit of the interpreter.
Index: abstract.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/abstract.c,v
retrieving revision 2.125
retrieving revision 2.126
diff -C2 -d -r2.125 -r2.126
*** abstract.c	17 Mar 2004 05:24:23 -0000	2.125
--- abstract.c	20 Mar 2004 22:52:14 -0000	2.126
***************
*** 1993,1998 ****
 }
 
! int
! PyObject_IsInstance(PyObject *inst, PyObject *cls)
 {
 	PyObject *icls;
--- 1993,1998 ----
 }
 
! static int
! recursive_isinstance(PyObject *inst, PyObject *cls, int recursion_depth)
 {
 	PyObject *icls;
***************
*** 2029,2040 ****
 	}
 	else if (PyTuple_Check(cls)) {
- 		/* Not a general sequence -- that opens up the road to
- 		 recursion and stack overflow. */
 		int i, n;
 
 		n = PyTuple_GET_SIZE(cls);
 		for (i = 0; i < n; i++) {
! 			retval = PyObject_IsInstance(
! 				inst, PyTuple_GET_ITEM(cls, i));
 			if (retval != 0)
 				break;
--- 2029,2046 ----
 	}
 	else if (PyTuple_Check(cls)) {
 		int i, n;
 
+ if (!recursion_depth) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "nest level of tuple too deep");
+ return NULL;
+ }
+ 
 		n = PyTuple_GET_SIZE(cls);
 		for (i = 0; i < n; i++) {
! 			retval = recursive_isinstance(
! inst,
! PyTuple_GET_ITEM(cls, i),
! recursion_depth-1);
 			if (retval != 0)
 				break;
***************
*** 2061,2065 ****
 
 int
! PyObject_IsSubclass(PyObject *derived, PyObject *cls)
 {
 	int retval;
--- 2067,2077 ----
 
 int
! PyObject_IsInstance(PyObject *inst, PyObject *cls)
! {
! return recursive_isinstance(inst, cls, Py_GetRecursionLimit());
! }
! 
! static int
! recursive_issubclass(PyObject *derived, PyObject *cls, int recursion_depth)
 {
 	int retval;
***************
*** 2073,2079 ****
 			int i;
 			int n = PyTuple_GET_SIZE(cls);
 			for (i = 0; i < n; ++i) {
! 				retval = PyObject_IsSubclass(
! 					derived, PyTuple_GET_ITEM(cls, i));
 				if (retval != 0) {
 					/* either found it, or got an error */
--- 2085,2099 ----
 			int i;
 			int n = PyTuple_GET_SIZE(cls);
+ 
+ if (!recursion_depth) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "nest level of tuple too deep");
+ return NULL;
+ }
 			for (i = 0; i < n; ++i) {
! 				retval = recursive_issubclass(
! derived,
! PyTuple_GET_ITEM(cls, i),
! recursion_depth-1);
 				if (retval != 0) {
 					/* either found it, or got an error */
***************
*** 2101,2104 ****
--- 2121,2131 ----
 }
 
+ int
+ PyObject_IsSubclass(PyObject *derived, PyObject *cls)
+ {
+ return recursive_issubclass(derived, cls, Py_GetRecursionLimit());
+ }
+ 
+ 
 PyObject *
 PyObject_GetIter(PyObject *o)


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /