[Python-checkins] CVS: python/dist/src/Python marshal.c,1.47,1.48

Fred L. Drake python-dev@python.org
2000年6月28日 11:47:59 -0700


Update of /cvsroot/python/python/dist/src/Python
In directory slayer.i.sourceforge.net:/tmp/cvs-serv8840
Modified Files:
	marshal.c 
Log Message:
Michael Hudson <mwh21@cam.ac.uk>:
As I really do not have anything better to do at the moment, I have written
a patch to Python/marshal.c that prevents Python dumping core when trying
to marshal stack bustingly deep (or recursive) data structure.
It just throws an exception; even slightly clever handling of recursive
data is what pickle is for...
[Fred Drake:] Moved magic constant 5000 to a #define.
This closes SourceForge patch #100645.
Index: marshal.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/marshal.c,v
retrieving revision 1.47
retrieving revision 1.48
diff -C2 -r1.47 -r1.48
*** marshal.c	2000年05月03日 23:44:39	1.47
--- marshal.c	2000年06月28日 18:47:56	1.48
***************
*** 40,43 ****
--- 40,49 ----
 #include "marshal.h"
 
+ /* High water mark to determine when the marshalled object is dangerously deep
+ * and risks coring the interpreter. When the object stack gets this deep,
+ * raise an exception instead of continuing.
+ */
+ #define MAX_MARSHAL_STACK_DEPTH 5000
+ 
 #define TYPE_NULL	'0'
 #define TYPE_NONE	'N'
***************
*** 59,62 ****
--- 65,69 ----
 	FILE *fp;
 	int error;
+ 	int depth;
 	/* If fp == NULL, the following are valid: */
 	PyObject *str;
***************
*** 145,150 ****
 	int i, n;
 	PyBufferProcs *pb;
 	
! 	if (v == NULL) {
 		w_byte(TYPE_NULL, p);
 	}
--- 152,162 ----
 	int i, n;
 	PyBufferProcs *pb;
+ 
+ 	p->depth++;
 	
! 	if (p->depth > MAX_MARSHAL_STACK_DEPTH) {
! 		p->error = 2;
! 	} 
! 	else if (v == NULL) {
 		w_byte(TYPE_NULL, p);
 	}
***************
*** 302,305 ****
--- 314,318 ----
 	wf.fp = fp;
 	wf.error = 0;
+ 	wf.depth = 0;
 	w_long(x, &wf);
 }
***************
*** 691,694 ****
--- 704,708 ----
 	wf.end = wf.ptr + PyString_Size(wf.str);
 	wf.error = 0;
+ 	wf.depth = 0;
 	w_object(x, &wf);
 	if (wf.str != NULL)
***************
*** 698,702 ****
 	if (wf.error) {
 		Py_XDECREF(wf.str);
! 		PyErr_SetString(PyExc_ValueError, "unmarshallable object");
 		return NULL;
 	}
--- 712,718 ----
 	if (wf.error) {
 		Py_XDECREF(wf.str);
! 		PyErr_SetString(PyExc_ValueError, 
! 				(wf.error==1)?"unmarshallable object"
! 				:"object too deeply nested to marshal");
 		return NULL;
 	}
***************
*** 725,731 ****
 	wf.ptr = wf.end = NULL;
 	wf.error = 0;
 	w_object(x, &wf);
 	if (wf.error) {
! 		PyErr_SetString(PyExc_ValueError, "unmarshallable object");
 		return NULL;
 	}
--- 741,750 ----
 	wf.ptr = wf.end = NULL;
 	wf.error = 0;
+ 	wf.depth = 0;
 	w_object(x, &wf);
 	if (wf.error) {
! 		PyErr_SetString(PyExc_ValueError, 
! 				(wf.error==1)?"unmarshallable object"
! 				:"object too deeply nested to marshal");
 		return NULL;
 	}

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