diff options
author | Guido van Rossum <guido@python.org> | 2007-05-22 18:11:13 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2007-05-22 18:11:13 (GMT) |
commit | d59da4b4325d189b93f1d46405b97f2435094699 (patch) | |
tree | d71bd85e141d8d88384b0cfa5866c8f05a1487e9 /Python/marshal.c | |
parent | dc19f0966195c711407cb710225cab55ba1f4be9 (diff) | |
download | cpython-d59da4b4325d189b93f1d46405b97f2435094699.zip cpython-d59da4b4325d189b93f1d46405b97f2435094699.tar.gz cpython-d59da4b4325d189b93f1d46405b97f2435094699.tar.bz2 |
Merged revisions 55407-55513 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/p3yk
................
r55413 | fred.drake | 2007-05-17 12:30:10 -0700 (Thu, 17 May 2007) | 1 line
fix argument name in documentation; match the implementation
................
r55430 | jack.diederich | 2007-05-18 06:39:59 -0700 (Fri, 18 May 2007) | 1 line
Implements class decorators, PEP 3129.
................
r55432 | guido.van.rossum | 2007-05-18 08:09:41 -0700 (Fri, 18 May 2007) | 2 lines
obsubmit.
................
r55434 | guido.van.rossum | 2007-05-18 09:39:10 -0700 (Fri, 18 May 2007) | 3 lines
Fix bug in test_inspect. (I presume this is how it should be fixed;
Jack Diedrich, please verify.)
................
r55460 | brett.cannon | 2007-05-20 00:31:57 -0700 (Sun, 20 May 2007) | 4 lines
Remove the imageop module. With imgfile already removed in Python 3.0 and
rgbimg gone in Python 2.6 the unit tests themselves were made worthless. Plus
third-party libraries perform the same function much better.
................
r55469 | neal.norwitz | 2007-05-20 11:28:20 -0700 (Sun, 20 May 2007) | 118 lines
Merged revisions 55324-55467 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r55348 | georg.brandl | 2007-05-15 13:19:34 -0700 (Tue, 15 May 2007) | 4 lines
HTML-escape the plain traceback in cgitb's HTML output, to prevent
the traceback inadvertently or maliciously closing the comment and
injecting HTML into the error page.
........
r55372 | neal.norwitz | 2007-05-15 21:33:50 -0700 (Tue, 15 May 2007) | 6 lines
Port rev 55353 from Guido:
Add what looks like a necessary call to PyErr_NoMemory() when PyMem_MALLOC()
fails.
Will backport.
........
r55377 | neal.norwitz | 2007-05-15 22:06:33 -0700 (Tue, 15 May 2007) | 1 line
Mention removal of some directories for obsolete platforms
........
r55380 | brett.cannon | 2007-05-15 22:50:03 -0700 (Tue, 15 May 2007) | 2 lines
Change the maintainer of the BeOS port.
........
r55383 | georg.brandl | 2007-05-16 06:44:18 -0700 (Wed, 16 May 2007) | 2 lines
Bug #1719995: don't use deprecated method in sets example.
........
r55386 | neal.norwitz | 2007-05-16 13:05:11 -0700 (Wed, 16 May 2007) | 5 lines
Fix bug in marshal where bad data would cause a segfault due to
lack of an infinite recursion check.
Contributed by Damien Miller at Google.
........
r55389 | brett.cannon | 2007-05-16 15:42:29 -0700 (Wed, 16 May 2007) | 6 lines
Remove the gopherlib module. It has been raising a DeprecationWarning since
Python 2.5.
Also remove gopher support from urllib/urllib2. As both imported gopherlib the
usage of the support would have raised a DeprecationWarning.
........
r55394 | raymond.hettinger | 2007-05-16 18:08:04 -0700 (Wed, 16 May 2007) | 1 line
calendar.py gets no benefit from xrange() instead of range()
........
r55395 | brett.cannon | 2007-05-16 19:02:56 -0700 (Wed, 16 May 2007) | 3 lines
Complete deprecation of BaseException.message. Some subclasses were directly
accessing the message attribute instead of using the descriptor.
........
r55396 | neal.norwitz | 2007-05-16 23:11:36 -0700 (Wed, 16 May 2007) | 4 lines
Reduce the max stack depth to see if this fixes the segfaults on
Windows and some other boxes. If this is successful, this rev should
be backported. I'm not sure how close to the limit we should push this.
........
r55397 | neal.norwitz | 2007-05-16 23:23:50 -0700 (Wed, 16 May 2007) | 4 lines
Set the depth to something very small to try to determine if the
crashes on Windows are really due to the stack size or possibly
some other problem.
........
r55398 | neal.norwitz | 2007-05-17 00:04:46 -0700 (Thu, 17 May 2007) | 4 lines
Last try for tweaking the max stack depth. 5000 was the original value,
4000 didn't work either. 1000 does work on Windows. If 2000 works,
that will hopefully be a reasonable balance.
........
r55412 | fred.drake | 2007-05-17 12:29:58 -0700 (Thu, 17 May 2007) | 1 line
fix argument name in documentation; match the implementation
........
r55427 | neal.norwitz | 2007-05-17 22:47:16 -0700 (Thu, 17 May 2007) | 1 line
Verify neither dumps or loads overflow the stack and segfault.
........
r55446 | collin.winter | 2007-05-18 16:11:24 -0700 (Fri, 18 May 2007) | 1 line
Backport PEP 3110's new 'except' syntax to 2.6.
........
r55448 | raymond.hettinger | 2007-05-18 18:11:16 -0700 (Fri, 18 May 2007) | 1 line
Improvements to NamedTuple's implementation, tests, and documentation
........
r55449 | raymond.hettinger | 2007-05-18 18:50:11 -0700 (Fri, 18 May 2007) | 1 line
Fix beginner mistake -- don't mix spaces and tabs.
........
r55450 | neal.norwitz | 2007-05-18 20:48:47 -0700 (Fri, 18 May 2007) | 1 line
Clear data so random memory does not get freed. Will backport.
........
r55452 | neal.norwitz | 2007-05-18 21:34:55 -0700 (Fri, 18 May 2007) | 3 lines
Whoops, need to pay attention to those test failures.
Move the clear to *before* the first use, not after.
........
r55453 | neal.norwitz | 2007-05-18 21:35:52 -0700 (Fri, 18 May 2007) | 1 line
Give some clue as to what happened if the test fails.
........
r55455 | georg.brandl | 2007-05-19 11:09:26 -0700 (Sat, 19 May 2007) | 2 lines
Fix docstring for add_package in site.py.
........
r55458 | brett.cannon | 2007-05-20 00:09:50 -0700 (Sun, 20 May 2007) | 2 lines
Remove the rgbimg module. It has been deprecated since Python 2.5.
........
r55465 | nick.coghlan | 2007-05-20 04:12:49 -0700 (Sun, 20 May 2007) | 1 line
Fix typo in example (should be backported, but my maintenance branch is woefully out of date)
........
................
r55472 | brett.cannon | 2007-05-20 12:06:18 -0700 (Sun, 20 May 2007) | 2 lines
Remove imageop from the Windows build process.
................
r55486 | neal.norwitz | 2007-05-20 23:59:52 -0700 (Sun, 20 May 2007) | 1 line
Remove callable() builtin
................
r55506 | neal.norwitz | 2007-05-22 00:43:29 -0700 (Tue, 22 May 2007) | 78 lines
Merged revisions 55468-55505 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r55468 | neal.norwitz | 2007-05-20 11:06:27 -0700 (Sun, 20 May 2007) | 1 line
rotor is long gone.
........
r55470 | neal.norwitz | 2007-05-20 11:43:00 -0700 (Sun, 20 May 2007) | 1 line
Update directories/files at the top-level.
........
r55471 | brett.cannon | 2007-05-20 12:05:06 -0700 (Sun, 20 May 2007) | 2 lines
Try to remove rgbimg from Windows builds.
........
r55474 | brett.cannon | 2007-05-20 16:17:38 -0700 (Sun, 20 May 2007) | 4 lines
Remove the macfs module. This led to the deprecation of macostools.touched();
it completely relied on macfs and is a no-op on OS X according to code
comments.
........
r55476 | brett.cannon | 2007-05-20 16:56:18 -0700 (Sun, 20 May 2007) | 3 lines
Move imgfile import to the global namespace to trigger an import error ASAP to
prevent creation of a test file.
........
r55477 | brett.cannon | 2007-05-20 16:57:38 -0700 (Sun, 20 May 2007) | 3 lines
Cause posixfile to raise a DeprecationWarning. Documented as deprecated since
Ptyhon 1.5.
........
r55479 | andrew.kuchling | 2007-05-20 17:03:15 -0700 (Sun, 20 May 2007) | 1 line
Note removed modules
........
r55481 | martin.v.loewis | 2007-05-20 21:35:47 -0700 (Sun, 20 May 2007) | 2 lines
Add Alexandre Vassalotti.
........
r55482 | george.yoshida | 2007-05-20 21:41:21 -0700 (Sun, 20 May 2007) | 4 lines
fix against r55474 [Remove the macfs module]
Remove "libmacfs.tex" from Makefile.deps and mac/mac.tex.
........
r55487 | raymond.hettinger | 2007-05-21 01:13:35 -0700 (Mon, 21 May 2007) | 1 line
Replace assertion with straight error-checking.
........
r55489 | raymond.hettinger | 2007-05-21 09:40:10 -0700 (Mon, 21 May 2007) | 1 line
Allow all alphanumeric and underscores in type and field names.
........
r55490 | facundo.batista | 2007-05-21 10:32:32 -0700 (Mon, 21 May 2007) | 5 lines
Added timeout support to HTTPSConnection, through the
socket.create_connection function. Also added a small
test for this, and updated NEWS file.
........
r55495 | georg.brandl | 2007-05-21 13:34:16 -0700 (Mon, 21 May 2007) | 2 lines
Patch #1686487: you can now pass any mapping after '**' in function calls.
........
r55502 | neal.norwitz | 2007-05-21 23:03:36 -0700 (Mon, 21 May 2007) | 1 line
Document new params to HTTPSConnection
........
r55504 | neal.norwitz | 2007-05-22 00:16:10 -0700 (Tue, 22 May 2007) | 1 line
Stop using METH_OLDARGS
........
r55505 | neal.norwitz | 2007-05-22 00:16:44 -0700 (Tue, 22 May 2007) | 1 line
Stop using METH_OLDARGS implicitly
........
................
Diffstat (limited to 'Python/marshal.c')
-rw-r--r-- | Python/marshal.c | 223 |
1 files changed, 155 insertions, 68 deletions
diff --git a/Python/marshal.c b/Python/marshal.c index d00ac8d..262c185 100644 --- a/Python/marshal.c +++ b/Python/marshal.c @@ -15,7 +15,7 @@ * 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 MAX_MARSHAL_STACK_DEPTH 2000 #define TYPE_NULL '0' #define TYPE_NONE 'N' @@ -235,9 +235,16 @@ w_object(PyObject *v, WFILE *p) goto exit; } else { + int ok; o = PyInt_FromSsize_t(PyDict_Size(p->strings)); - PyDict_SetItem(p->strings, v, o); - Py_DECREF(o); + ok = o && + PyDict_SetItem(p->strings, v, o) >= 0; + Py_XDECREF(o); + if (!ok) { + p->depth--; + p->error = 1; + return; + } w_byte(TYPE_INTERNED, p); } } @@ -401,7 +408,7 @@ PyMarshal_WriteObjectToFile(PyObject *x, FILE *fp, int version) typedef WFILE RFILE; /* Same struct with different invariants */ -#define rs_byte(p) (((p)->ptr != (p)->end) ? (unsigned char)*(p)->ptr++ : EOF) +#define rs_byte(p) (((p)->ptr < (p)->end) ? (unsigned char)*(p)->ptr++ : EOF) #define r_byte(p) ((p)->fp ? getc((p)->fp) : rs_byte(p)) @@ -492,42 +499,60 @@ r_object(RFILE *p) PyObject *v, *v2, *v3; long i, n; int type = r_byte(p); + PyObject *retval; + + p->depth++; + + if (p->depth > MAX_MARSHAL_STACK_DEPTH) { + p->depth--; + PyErr_SetString(PyExc_ValueError, "recursion limit exceeded"); + return NULL; + } switch (type) { case EOF: PyErr_SetString(PyExc_EOFError, "EOF read where object expected"); - return NULL; + retval = NULL; + break; case TYPE_NULL: - return NULL; + retval = NULL; + break; case TYPE_NONE: Py_INCREF(Py_None); - return Py_None; + retval = Py_None; + break; case TYPE_STOPITER: Py_INCREF(PyExc_StopIteration); - return PyExc_StopIteration; + retval = PyExc_StopIteration; + break; case TYPE_ELLIPSIS: Py_INCREF(Py_Ellipsis); - return Py_Ellipsis; + retval = Py_Ellipsis; + break; case TYPE_FALSE: Py_INCREF(Py_False); - return Py_False; + retval = Py_False; + break; case TYPE_TRUE: Py_INCREF(Py_True); - return Py_True; + retval = Py_True; + break; case TYPE_INT: - return PyInt_FromLong(r_long(p)); + retval = PyInt_FromLong(r_long(p)); + break; case TYPE_INT64: - return r_long64(p); + retval = r_long64(p); + break; case TYPE_LONG: { @@ -537,12 +562,15 @@ r_object(RFILE *p) if (n < -INT_MAX || n > INT_MAX) { PyErr_SetString(PyExc_ValueError, "bad marshal data"); - return NULL; + retval = NULL; + break; } size = n<0 ? -n : n; ob = _PyLong_New(size); - if (ob == NULL) - return NULL; + if (ob == NULL) { + retval = NULL; + break; + } ob->ob_size = n; for (i = 0; i < size; i++) { int digit = r_short(p); @@ -550,11 +578,14 @@ r_object(RFILE *p) Py_DECREF(ob); PyErr_SetString(PyExc_ValueError, "bad marshal data"); - return NULL; + ob = NULL; + break; } - ob->ob_digit[i] = digit; + if (ob != NULL) + ob->ob_digit[i] = digit; } - return (PyObject *)ob; + retval = (PyObject *)ob; + break; } case TYPE_FLOAT: @@ -565,13 +596,16 @@ r_object(RFILE *p) if (n == EOF || r_string(buf, (int)n, p) != n) { PyErr_SetString(PyExc_EOFError, "EOF read where object expected"); - return NULL; + retval = NULL; + break; } buf[n] = '\0'; - PyFPE_START_PROTECT("atof", return 0) + retval = NULL; + PyFPE_START_PROTECT("atof", break) dx = PyOS_ascii_atof(buf); PyFPE_END_PROTECT(dx) - return PyFloat_FromDouble(dx); + retval = PyFloat_FromDouble(dx); + break; } case TYPE_BINARY_FLOAT: @@ -581,13 +615,16 @@ r_object(RFILE *p) if (r_string((char*)buf, 8, p) != 8) { PyErr_SetString(PyExc_EOFError, "EOF read where object expected"); - return NULL; + retval = NULL; + break; } x = _PyFloat_Unpack8(buf, 1); if (x == -1.0 && PyErr_Occurred()) { - return NULL; + retval = NULL; + break; } - return PyFloat_FromDouble(x); + retval = PyFloat_FromDouble(x); + break; } #ifndef WITHOUT_COMPLEX @@ -599,23 +636,27 @@ r_object(RFILE *p) if (n == EOF || r_string(buf, (int)n, p) != n) { PyErr_SetString(PyExc_EOFError, "EOF read where object expected"); - return NULL; + retval = NULL; + break; } buf[n] = '\0'; - PyFPE_START_PROTECT("atof", return 0) + retval = NULL; + PyFPE_START_PROTECT("atof", break;) c.real = PyOS_ascii_atof(buf); PyFPE_END_PROTECT(c) n = r_byte(p); if (n == EOF || r_string(buf, (int)n, p) != n) { PyErr_SetString(PyExc_EOFError, "EOF read where object expected"); - return NULL; + retval = NULL; + break; } buf[n] = '\0'; - PyFPE_START_PROTECT("atof", return 0) + PyFPE_START_PROTECT("atof", break) c.imag = PyOS_ascii_atof(buf); PyFPE_END_PROTECT(c) - return PyComplex_FromCComplex(c); + retval = PyComplex_FromCComplex(c); + break; } case TYPE_BINARY_COMPLEX: @@ -625,22 +666,27 @@ r_object(RFILE *p) if (r_string((char*)buf, 8, p) != 8) { PyErr_SetString(PyExc_EOFError, "EOF read where object expected"); - return NULL; + retval = NULL; + break; } c.real = _PyFloat_Unpack8(buf, 1); if (c.real == -1.0 && PyErr_Occurred()) { - return NULL; + retval = NULL; + break; } if (r_string((char*)buf, 8, p) != 8) { PyErr_SetString(PyExc_EOFError, "EOF read where object expected"); - return NULL; + retval = NULL; + break; } c.imag = _PyFloat_Unpack8(buf, 1); if (c.imag == -1.0 && PyErr_Occurred()) { - return NULL; + retval = NULL; + break; } - return PyComplex_FromCComplex(c); + retval = PyComplex_FromCComplex(c); + break; } #endif @@ -649,32 +695,42 @@ r_object(RFILE *p) n = r_long(p); if (n < 0 || n > INT_MAX) { PyErr_SetString(PyExc_ValueError, "bad marshal data"); - return NULL; + retval = NULL; + break; } v = PyString_FromStringAndSize((char *)NULL, n); - if (v == NULL) - return v; + if (v == NULL) { + retval = NULL; + break; + } if (r_string(PyString_AS_STRING(v), (int)n, p) != n) { Py_DECREF(v); PyErr_SetString(PyExc_EOFError, "EOF read where object expected"); - return NULL; + retval = NULL; + break; } if (type == TYPE_INTERNED) { PyString_InternInPlace(&v); - PyList_Append(p->strings, v); + if (PyList_Append(p->strings, v) < 0) { + retval = NULL; + break; + } } - return v; + retval = v; + break; case TYPE_STRINGREF: n = r_long(p); if (n < 0 || n >= PyList_GET_SIZE(p->strings)) { PyErr_SetString(PyExc_ValueError, "bad marshal data"); - return NULL; + retval = NULL; + break; } v = PyList_GET_ITEM(p->strings, n); Py_INCREF(v); - return v; + retval = v; + break; case TYPE_UNICODE: { @@ -683,31 +739,39 @@ r_object(RFILE *p) n = r_long(p); if (n < 0 || n > INT_MAX) { PyErr_SetString(PyExc_ValueError, "bad marshal data"); - return NULL; + retval = NULL; + break; } buffer = PyMem_NEW(char, n); - if (buffer == NULL) - return PyErr_NoMemory(); + if (buffer == NULL) { + retval = PyErr_NoMemory(); + break; + } if (r_string(buffer, (int)n, p) != n) { PyMem_DEL(buffer); PyErr_SetString(PyExc_EOFError, "EOF read where object expected"); - return NULL; + retval = NULL; + break; } v = PyUnicode_DecodeUTF8(buffer, n, NULL); PyMem_DEL(buffer); - return v; + retval = v; + break; } case TYPE_TUPLE: n = r_long(p); if (n < 0 || n > INT_MAX) { PyErr_SetString(PyExc_ValueError, "bad marshal data"); - return NULL; + retval = NULL; + break; } v = PyTuple_New((int)n); - if (v == NULL) - return v; + if (v == NULL) { + retval = NULL; + break; + } for (i = 0; i < n; i++) { v2 = r_object(p); if ( v2 == NULL ) { @@ -720,17 +784,21 @@ r_object(RFILE *p) } PyTuple_SET_ITEM(v, (int)i, v2); } - return v; + retval = v; + break; case TYPE_LIST: n = r_long(p); if (n < 0 || n > INT_MAX) { PyErr_SetString(PyExc_ValueError, "bad marshal data"); - return NULL; + retval = NULL; + break; } v = PyList_New((int)n); - if (v == NULL) - return v; + if (v == NULL) { + retval = NULL; + break; + } for (i = 0; i < n; i++) { v2 = r_object(p); if ( v2 == NULL ) { @@ -741,14 +809,17 @@ r_object(RFILE *p) v = NULL; break; } - PyList_SetItem(v, (int)i, v2); + PyList_SET_ITEM(v, (int)i, v2); } - return v; + retval = v; + break; case TYPE_DICT: v = PyDict_New(); - if (v == NULL) - return NULL; + if (v == NULL) { + retval = NULL; + break; + } for (;;) { PyObject *key, *val; key = r_object(p); @@ -764,18 +835,22 @@ r_object(RFILE *p) Py_DECREF(v); v = NULL; } - return v; + retval = v; + break; case TYPE_SET: case TYPE_FROZENSET: n = r_long(p); - if (n < 0) { + if (n < 0 || n > INT_MAX) { PyErr_SetString(PyExc_ValueError, "bad marshal data"); - return NULL; + retval = NULL; + break; } v = PyTuple_New((int)n); - if (v == NULL) - return v; + if (v == NULL) { + retval = NULL; + break; + } for (i = 0; i < n; i++) { v2 = r_object(p); if ( v2 == NULL ) { @@ -788,14 +863,17 @@ r_object(RFILE *p) } PyTuple_SET_ITEM(v, (int)i, v2); } - if (v == NULL) - return v; + if (v == NULL) { + retval = NULL; + break; + } if (type == TYPE_SET) v3 = PySet_New(v); else v3 = PyFrozenSet_New(v); Py_DECREF(v); - return v3; + retval = v3; + break; case TYPE_CODE: { @@ -872,14 +950,19 @@ r_object(RFILE *p) return v; } + retval = v; + break; default: /* Bogus data got written, which isn't ideal. This will let you keep working and recover. */ PyErr_SetString(PyExc_ValueError, "bad marshal data"); - return NULL; + retval = NULL; + break; } + p->depth--; + return retval; } static PyObject * @@ -985,6 +1068,7 @@ PyMarshal_ReadObjectFromFile(FILE *fp) PyObject *result; rf.fp = fp; rf.strings = PyList_New(0); + rf.depth = 0; result = r_object(&rf); Py_DECREF(rf.strings); return result; @@ -999,6 +1083,7 @@ PyMarshal_ReadObjectFromString(char *str, Py_ssize_t len) rf.ptr = str; rf.end = str + len; rf.strings = PyList_New(0); + rf.depth = 0; result = r_object(&rf); Py_DECREF(rf.strings); return result; @@ -1125,6 +1210,7 @@ marshal_load(PyObject *self, PyObject *f) } rf.fp = PyFile_AsFile(f); rf.strings = PyList_New(0); + rf.depth = 0; result = read_object(&rf); Py_DECREF(rf.strings); return result; @@ -1153,6 +1239,7 @@ marshal_loads(PyObject *self, PyObject *args) rf.ptr = s; rf.end = s + n; rf.strings = PyList_New(0); + rf.depth = 0; result = read_object(&rf); Py_DECREF(rf.strings); return result; |