Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-101006: Improve error handling when read marshal data#101007

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Show file tree
Hide file tree
Changes from2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
Improve error handling when read :mod:`marshal` data.
122 changes: 61 additions & 61 deletionsPython/marshal.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -747,23 +747,28 @@ r_string(Py_ssize_t n, RFILE *p)
static int
r_byte(RFILE *p)
{
int c = EOF;

if (p->ptr != NULL) {
if (p->ptr < p->end)
c = (unsigned char) *p->ptr++;
return c;
if (p->ptr < p->end) {
return (unsigned char) *p->ptr++;
}
}
if (!p->readable) {
elseif (!p->readable) {
assert(p->fp);
c = getc(p->fp);
int c = getc(p->fp);
if (c != EOF) {
return c;
}
}
else {
const char *ptr = r_string(1, p);
if (ptr != NULL)
c = *(const unsigned char *) ptr;
if (ptr != NULL) {
return *(const unsigned char *) ptr;
}
return EOF;
}
return c;
PyErr_SetString(PyExc_EOFError,
"EOF read where not expected");
return EOF;
}

static int
Expand DownExpand Up@@ -824,10 +829,10 @@ r_PyLong(RFILE *p)
digit d;

n = r_long(p);
if (PyErr_Occurred())
return NULL;
if (n == 0)
return (PyObject *)_PyLong_New(0);
if (n == -1 && PyErr_Occurred())
return NULL;
if (n < -SIZE32_MAX || n > SIZE32_MAX) {
PyErr_SetString(PyExc_ValueError,
"bad marshal data (long size out of range)");
Expand All@@ -846,10 +851,6 @@ r_PyLong(RFILE *p)
d = 0;
for (j=0; j < PyLong_MARSHAL_RATIO; j++) {
md = r_short(p);
if (PyErr_Occurred()) {
Py_DECREF(ob);
return NULL;
}
if (md < 0 || md > PyLong_MARSHAL_BASE)
goto bad_digit;
d += (digit)md << j*PyLong_MARSHAL_SHIFT;
Expand All@@ -860,10 +861,6 @@ r_PyLong(RFILE *p)
d = 0;
for (j=0; j < shorts_in_top_digit; j++) {
md = r_short(p);
if (PyErr_Occurred()) {
Py_DECREF(ob);
return NULL;
}
if (md < 0 || md > PyLong_MARSHAL_BASE)
goto bad_digit;
/* topmost marshal digit should be nonzero */
Expand All@@ -875,18 +872,16 @@ r_PyLong(RFILE *p)
}
d += (digit)md << j*PyLong_MARSHAL_SHIFT;
}
if (PyErr_Occurred()) {
Py_DECREF(ob);
return NULL;
}
/* top digit should be nonzero, else the resulting PyLong won't be
normalized */
ob->long_value.ob_digit[size-1] = d;
return (PyObject *)ob;
bad_digit:
Py_DECREF(ob);
PyErr_SetString(PyExc_ValueError,
"bad marshal data (digit out of range in long)");
if (!PyErr_Occurred()) {
PyErr_SetString(PyExc_ValueError,
"bad marshal data (digit out of range in long)");
}
return NULL;
}

Expand All@@ -909,8 +904,6 @@ r_float_str(RFILE *p)
const char *ptr;
n = r_byte(p);
if (n == EOF) {
PyErr_SetString(PyExc_EOFError,
"EOF read where object expected");
return -1;
}
ptr = r_string(n, p);
Expand DownExpand Up@@ -988,8 +981,10 @@ r_object(RFILE *p)
PyObject *retval = NULL;

if (code == EOF) {
PyErr_SetString(PyExc_EOFError,
"EOF read where object expected");
if (PyErr_ExceptionMatches(PyExc_EOFError)) {
PyErr_SetString(PyExc_EOFError,
"EOF read where object expected");
}
return NULL;
}

Expand DownExpand Up@@ -1036,7 +1031,9 @@ r_object(RFILE *p)

case TYPE_INT:
n = r_long(p);
retval = PyErr_Occurred() ? NULL : PyLong_FromLong(n);
if (n == -1 && PyErr_Occurred())
break;
retval = PyLong_FromLong(n);
R_REF(retval);
break;

Expand DownExpand Up@@ -1102,10 +1099,10 @@ r_object(RFILE *p)
{
const char *ptr;
n = r_long(p);
if (PyErr_Occurred())
break;
if (n < 0 || n > SIZE32_MAX) {
PyErr_SetString(PyExc_ValueError, "bad marshal data (bytes object size out of range)");
if (!PyErr_Occurred()) {
PyErr_SetString(PyExc_ValueError, "bad marshal data (bytes object size out of range)");
}
break;
}
v = PyBytes_FromStringAndSize((char *)NULL, n);
Expand All@@ -1127,10 +1124,10 @@ r_object(RFILE *p)
/* fall through */
case TYPE_ASCII:
n = r_long(p);
if (PyErr_Occurred())
break;
if (n < 0 || n > SIZE32_MAX) {
PyErr_SetString(PyExc_ValueError, "bad marshal data (string size out of range)");
if (!PyErr_Occurred()) {
PyErr_SetString(PyExc_ValueError, "bad marshal data (string size out of range)");
}
break;
}
goto _read_ascii;
Expand All@@ -1141,8 +1138,6 @@ r_object(RFILE *p)
case TYPE_SHORT_ASCII:
n = r_byte(p);
if (n == EOF) {
PyErr_SetString(PyExc_EOFError,
"EOF read where object expected");
break;
}
_read_ascii:
Expand All@@ -1169,10 +1164,10 @@ r_object(RFILE *p)
const char *buffer;

n = r_long(p);
if (PyErr_Occurred())
break;
if (n < 0 || n > SIZE32_MAX) {
PyErr_SetString(PyExc_ValueError, "bad marshal data (string size out of range)");
if (!PyErr_Occurred()) {
PyErr_SetString(PyExc_ValueError, "bad marshal data (string size out of range)");
}
break;
}
if (n != 0) {
Expand All@@ -1194,16 +1189,17 @@ r_object(RFILE *p)
}

case TYPE_SMALL_TUPLE:
n =(unsigned char)r_byte(p);
if (PyErr_Occurred())
n = r_byte(p);
if (n == EOF) {
break;
}
goto _read_tuple;
case TYPE_TUPLE:
n = r_long(p);
if (PyErr_Occurred())
break;
if (n < 0 || n > SIZE32_MAX) {
PyErr_SetString(PyExc_ValueError, "bad marshal data (tuple size out of range)");
if (!PyErr_Occurred()) {
PyErr_SetString(PyExc_ValueError, "bad marshal data (tuple size out of range)");
}
break;
}
_read_tuple:
Expand All@@ -1228,10 +1224,10 @@ r_object(RFILE *p)

case TYPE_LIST:
n = r_long(p);
if (PyErr_Occurred())
break;
if (n < 0 || n > SIZE32_MAX) {
PyErr_SetString(PyExc_ValueError, "bad marshal data (list size out of range)");
if (!PyErr_Occurred()) {
PyErr_SetString(PyExc_ValueError, "bad marshal data (list size out of range)");
}
break;
}
v = PyList_New(n);
Expand DownExpand Up@@ -1284,10 +1280,10 @@ r_object(RFILE *p)
case TYPE_SET:
case TYPE_FROZENSET:
n = r_long(p);
if (PyErr_Occurred())
break;
if (n < 0 || n > SIZE32_MAX) {
PyErr_SetString(PyExc_ValueError, "bad marshal data (set size out of range)");
if (!PyErr_Occurred()) {
PyErr_SetString(PyExc_ValueError, "bad marshal data (set size out of range)");
}
break;
}

Expand DownExpand Up@@ -1364,20 +1360,20 @@ r_object(RFILE *p)

/* XXX ignore long->int overflows for now */
argcount = (int)r_long(p);
if (PyErr_Occurred())
if (argcount == -1 &&PyErr_Occurred())
goto code_error;
posonlyargcount = (int)r_long(p);
if (PyErr_Occurred()) {
if (posonlyargcount == -1 &&PyErr_Occurred()) {
goto code_error;
}
kwonlyargcount = (int)r_long(p);
if (PyErr_Occurred())
if (kwonlyargcount == -1 &&PyErr_Occurred())
goto code_error;
stacksize = (int)r_long(p);
if (PyErr_Occurred())
if (stacksize == -1 &&PyErr_Occurred())
goto code_error;
flags = (int)r_long(p);
if (PyErr_Occurred())
if (flags == -1 &&PyErr_Occurred())
goto code_error;
code = r_object(p);
if (code == NULL)
Expand DownExpand Up@@ -1450,6 +1446,10 @@ r_object(RFILE *p)
v = r_ref_insert(v, idx, flag, p);

code_error:
if (v == NULL && !PyErr_Occurred()) {
PyErr_SetString(PyExc_TypeError,
"NULL object in marshal data for code object");
}
Py_XDECREF(code);
Py_XDECREF(consts);
Py_XDECREF(names);
Expand All@@ -1467,9 +1467,9 @@ r_object(RFILE *p)
case TYPE_REF:
n = r_long(p);
if (n < 0 || n >= PyList_GET_SIZE(p->refs)) {
if (n == -1 &&PyErr_Occurred())
break;
PyErr_SetString(PyExc_ValueError, "bad marshal data (invalid reference)");
if (!PyErr_Occurred()) {
PyErr_SetString(PyExc_ValueError, "bad marshal data (invalid reference)");
}
break;
}
v = PyList_GET_ITEM(p->refs, n);
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp