Skip to content

Commit 59b08c1

Browse files
committed
use safe allocation and reallocation macros
1 parent 614bfcc commit 59b08c1

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
lines changed

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Core and Builtins
2222
Library
2323
-------
2424

25+
- Fix possible integer overflows in the pickle module.
26+
2527
- Issue #22931: Allow '[' and ']' in cookie values.
2628

2729
- Issue #24094: Fix possible crash in json.encode with poorly behaved dict

Modules/_pickle.c

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,7 @@ Pdata_grow(Pdata *self)
218218
if (new_allocated > PY_SSIZE_T_MAX - allocated)
219219
goto nomemory;
220220
new_allocated += allocated;
221-
if (new_allocated > (PY_SSIZE_T_MAX / sizeof(PyObject *)))
222-
goto nomemory;
223-
data = PyMem_REALLOC(data, new_allocated * sizeof(PyObject *));
221+
PyMem_RESIZE(data, PyObject *, new_allocated);
224222
if (data == NULL)
225223
goto nomemory;
226224

@@ -433,7 +431,7 @@ PyMemoTable_Copy(PyMemoTable *self)
433431
/* The table we get from _New() is probably smaller than we wanted.
434432
Free it and allocate one that's the right size. */
435433
PyMem_FREE(new->mt_table);
436-
new->mt_table = PyMem_MALLOC(self->mt_allocated * sizeof(PyMemoEntry));
434+
new->mt_table = PyMem_NEW(PyMemoEntry, self->mt_allocated);
437435
if (new->mt_table == NULL) {
438436
PyMem_FREE(new);
439437
return NULL;
@@ -527,7 +525,7 @@ _PyMemoTable_ResizeTable(PyMemoTable *self, Py_ssize_t min_size)
527525

528526
/* Allocate new table. */
529527
oldtable = self->mt_table;
530-
self->mt_table = PyMem_MALLOC(new_size * sizeof(PyMemoEntry));
528+
self->mt_table = PyMem_NEW(PyMemoEntry, new_size);
531529
if (self->mt_table == NULL) {
532530
PyMem_FREE(oldtable);
533531
PyErr_NoMemory();
@@ -1055,16 +1053,14 @@ static int
10551053
_Unpickler_ResizeMemoList(UnpicklerObject *self, Py_ssize_t new_size)
10561054
{
10571055
Py_ssize_t i;
1058-
PyObject **memo;
10591056

10601057
assert(new_size > self->memo_size);
10611058

1062-
memo = PyMem_REALLOC(self->memo, new_size * sizeof(PyObject *));
1063-
if (memo == NULL) {
1059+
PyMem_RESIZE(self->memo, PyObject *, new_size);
1060+
if (self->memo == NULL) {
10641061
PyErr_NoMemory();
10651062
return -1;
10661063
}
1067-
self->memo = memo;
10681064
for (i = self->memo_size; i < new_size; i++)
10691065
self->memo[i] = NULL;
10701066
self->memo_size = new_size;
@@ -1103,7 +1099,7 @@ _Unpickler_MemoPut(UnpicklerObject *self, Py_ssize_t idx, PyObject *value)
11031099
static PyObject **
11041100
_Unpickler_NewMemo(Py_ssize_t new_size)
11051101
{
1106-
PyObject **memo = PyMem_MALLOC(new_size * sizeof(PyObject *));
1102+
PyObject **memo = PyMem_NEW(PyObject *, new_size);
11071103
if (memo == NULL)
11081104
return NULL;
11091105
memset(memo, 0, new_size * sizeof(PyObject *));
@@ -5270,7 +5266,6 @@ load_mark(UnpicklerObject *self)
52705266

52715267
if ((self->num_marks + 1) >= self->marks_size) {
52725268
size_t alloc;
5273-
Py_ssize_t *marks;
52745269

52755270
/* Use the size_t type to check for overflow. */
52765271
alloc = ((size_t)self->num_marks << 1) + 20;
@@ -5281,15 +5276,14 @@ load_mark(UnpicklerObject *self)
52815276
}
52825277

52835278
if (self->marks == NULL)
5284-
marks = (Py_ssize_t *) PyMem_Malloc(alloc * sizeof(Py_ssize_t));
5279+
self->marks = PyMem_NEW(Py_ssize_t, alloc);
52855280
else
5286-
marks = (Py_ssize_t *) PyMem_Realloc(self->marks,
5287-
alloc * sizeof(Py_ssize_t));
5288-
if (marks == NULL) {
5281+
PyMem_RESIZE(self->marks, Py_ssize_t, alloc);
5282+
if (self->marks == NULL) {
5283+
self->marks_size = 0;
52895284
PyErr_NoMemory();
52905285
return -1;
52915286
}
5292-
self->marks = marks;
52935287
self->marks_size = (Py_ssize_t)alloc;
52945288
}
52955289

0 commit comments

Comments
 (0)