Skip to content

Commit 8ae4563

Browse files
committed
w_reserve() sets writer to NULL if Resize() fails
1 parent 09661e7 commit 8ae4563

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

Python/marshal.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ w_reserve(WFILE *p, Py_ssize_t needed)
157157
}
158158
size += delta;
159159
if (PyBytesWriter_Resize(p->writer, size) != 0) {
160+
PyBytesWriter_Discard(p->writer);
161+
p->writer = NULL;
160162
p->end = p->ptr = p->buf = NULL;
161163
return 0;
162164
}
@@ -1919,13 +1921,18 @@ _PyMarshal_WriteObjectToString(PyObject *x, int version, int allow_code)
19191921
}
19201922
w_object(x, &wf);
19211923
w_clear_refs(&wf);
1924+
19221925
if (wf.writer != NULL) {
1926+
assert(wf.ptr != NULL);
19231927
const char *base = PyBytesWriter_GetData(wf.writer);
19241928
if (PyBytesWriter_Resize(wf.writer, (Py_ssize_t)(wf.ptr - base)) < 0) {
19251929
PyBytesWriter_Discard(wf.writer);
1930+
// PyBytesWriter_Resize() sets an exception
1931+
assert(PyErr_Occurred());
19261932
return NULL;
19271933
}
19281934
}
1935+
19291936
if (wf.error != WFERR_OK) {
19301937
PyBytesWriter_Discard(wf.writer);
19311938
switch (wf.error) {
@@ -1948,6 +1955,13 @@ _PyMarshal_WriteObjectToString(PyObject *x, int version, int allow_code)
19481955
}
19491956
return NULL;
19501957
}
1958+
1959+
if (wf.writer == NULL) {
1960+
// In w_reserve(), PyBytesWriter_Resize() failed with an exception set
1961+
assert(PyErr_Occurred());
1962+
return NULL;
1963+
}
1964+
19511965
return PyBytesWriter_Finish(wf.writer);
19521966
}
19531967

0 commit comments

Comments
 (0)