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

Skip to content

Commit dfde215

Browse files
Fix reference leaks introduced by the patch for issue #5308.
1 parent cca40ff commit dfde215

1 file changed

Lines changed: 11 additions & 12 deletions

File tree

Python/marshal.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ w_more(int c, WFILE *p)
9595
}
9696

9797
static void
98-
w_string(char *s, Py_ssize_t n, WFILE *p)
98+
w_string(const char *s, Py_ssize_t n, WFILE *p)
9999
{
100100
if (p->fp != NULL) {
101101
fwrite(s, 1, n, p->fp);
@@ -139,6 +139,13 @@ w_long(long x, WFILE *p)
139139
# define W_SIZE w_long
140140
#endif
141141

142+
static void
143+
w_pstring(const char *s, Py_ssize_t n, WFILE *p)
144+
{
145+
W_SIZE(n, p);
146+
w_string(s, n, p);
147+
}
148+
142149
/* We assume that Python longs are stored internally in base some power of
143150
2**15; for the sake of portability we'll always read and write them in base
144151
exactly 2**15. */
@@ -313,9 +320,7 @@ w_object(PyObject *v, WFILE *p)
313320
}
314321
else if (PyBytes_CheckExact(v)) {
315322
w_byte(TYPE_STRING, p);
316-
n = PyBytes_GET_SIZE(v);
317-
W_SIZE(n, p);
318-
w_string(PyBytes_AS_STRING(v), n, p);
323+
w_pstring(PyBytes_AS_STRING(v), PyBytes_GET_SIZE(v), p);
319324
}
320325
else if (PyUnicode_CheckExact(v)) {
321326
PyObject *utf8;
@@ -326,9 +331,7 @@ w_object(PyObject *v, WFILE *p)
326331
return;
327332
}
328333
w_byte(TYPE_UNICODE, p);
329-
n = PyBytes_GET_SIZE(utf8);
330-
W_SIZE(n, p);
331-
w_string(PyBytes_AS_STRING(utf8), n, p);
334+
w_pstring(PyBytes_AS_STRING(utf8), PyBytes_GET_SIZE(utf8), p);
332335
Py_DECREF(utf8);
333336
}
334337
else if (PyTuple_CheckExact(v)) {
@@ -411,7 +414,6 @@ w_object(PyObject *v, WFILE *p)
411414
}
412415
else if (PyObject_CheckBuffer(v)) {
413416
/* Write unknown buffer-style objects as a string */
414-
char *s;
415417
Py_buffer view;
416418
if (PyObject_GetBuffer(v, &view, PyBUF_SIMPLE) != 0) {
417419
w_byte(TYPE_UNKNOWN, p);
@@ -420,10 +422,7 @@ w_object(PyObject *v, WFILE *p)
420422
return;
421423
}
422424
w_byte(TYPE_STRING, p);
423-
n = view.len;
424-
s = view.buf;
425-
W_SIZE(n, p);
426-
w_string(s, n, p);
425+
w_pstring(view.buf, view.len, p);
427426
PyBuffer_Release(&view);
428427
}
429428
else {

0 commit comments

Comments
 (0)