Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
24 changes: 16 additions & 8 deletions Include/internal/pycore_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,17 +192,25 @@ _PyFrame_LocalsToFast(_PyInterpreterFrame *frame, int clear);
extern _PyInterpreterFrame *
_PyThreadState_BumpFramePointerSlow(PyThreadState *tstate, size_t size);

static inline bool
_PyThreadState_HasStackSpace(PyThreadState *tstate, size_t size)
{
assert(
(tstate->datastack_top == NULL && tstate->datastack_limit == NULL)
||
(tstate->datastack_top != NULL && tstate->datastack_limit != NULL)
);
return tstate->datastack_top != NULL &&
size < (size_t)(tstate->datastack_limit - tstate->datastack_top);
}

static inline _PyInterpreterFrame *
_PyThreadState_BumpFramePointer(PyThreadState *tstate, size_t size)
{
PyObject **base = tstate->datastack_top;
if (base) {
PyObject **top = base + size;
assert(tstate->datastack_limit);
if (top < tstate->datastack_limit) {
tstate->datastack_top = top;
return (_PyInterpreterFrame *)base;
}
if (_PyThreadState_HasStackSpace(tstate, size)) {
_PyInterpreterFrame *res = (_PyInterpreterFrame *)tstate->datastack_top;
tstate->datastack_top += size;
return res;
}
return _PyThreadState_BumpFramePointerSlow(tstate, size);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remove two cases of undefined behavior, by adding NULL checks.
16 changes: 8 additions & 8 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -2178,16 +2178,16 @@ push_chunk(PyThreadState *tstate, int size)
_PyInterpreterFrame *
_PyThreadState_BumpFramePointerSlow(PyThreadState *tstate, size_t size)
{
assert(size < INT_MAX/sizeof(PyObject *));
PyObject **base = tstate->datastack_top;
PyObject **top = base + size;
if (top >= tstate->datastack_limit) {
base = push_chunk(tstate, (int)size);
if (_PyThreadState_HasStackSpace(tstate, size)) {
_PyInterpreterFrame *res = (_PyInterpreterFrame *)tstate->datastack_top;
tstate->datastack_top += size;
return res;
}
else {
tstate->datastack_top = top;
if (size > INT_MAX/2) {
PyErr_NoMemory();
return NULL;
}
return (_PyInterpreterFrame *)base;
return (_PyInterpreterFrame *)push_chunk(tstate, (int)size);
}

void
Expand Down