Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a45cb45

Browse files
committed
When unmarshalling, add test for negative lengths on strings, tuples
and lists; if the size is negative, raise an exception. Also raise an exception when an undefined type is found -- all this to increase the chance that garbage input causes an exception instead of a core dump.
1 parent a63eff6 commit a45cb45

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

Python/marshal.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,10 @@ r_object(p)
463463

464464
case TYPE_STRING:
465465
n = r_long(p);
466+
if (n < 0) {
467+
PyErr_SetString(PyExc_ValueError, "bad marshal data");
468+
return NULL;
469+
}
466470
v = PyString_FromStringAndSize((char *)NULL, n);
467471
if (v != NULL) {
468472
if (r_string(PyString_AsString(v), (int)n, p) != n) {
@@ -476,6 +480,10 @@ r_object(p)
476480

477481
case TYPE_TUPLE:
478482
n = r_long(p);
483+
if (n < 0) {
484+
PyErr_SetString(PyExc_ValueError, "bad marshal data");
485+
return NULL;
486+
}
479487
v = PyTuple_New((int)n);
480488
if (v == NULL)
481489
return v;
@@ -492,6 +500,10 @@ r_object(p)
492500

493501
case TYPE_LIST:
494502
n = r_long(p);
503+
if (n < 0) {
504+
PyErr_SetString(PyExc_ValueError, "bad marshal data");
505+
return NULL;
506+
}
495507
v = PyList_New((int)n);
496508
if (v == NULL)
497509
return v;
@@ -571,8 +583,8 @@ r_object(p)
571583
default:
572584
/* Bogus data got written, which isn't ideal.
573585
This will let you keep working and recover. */
574-
Py_INCREF(Py_None);
575-
return Py_None;
586+
PyErr_SetString(PyExc_ValueError, "bad marshal data");
587+
return NULL;
576588

577589
}
578590
}

0 commit comments

Comments
 (0)