Skip to content

Commit 5ce1ef3

Browse files
authored
gh-159739: Keep SimpleQueue valid after buffer allocation fails (#160165)
If allocating a SimpleQueue buffer fails, the partially constructed object reports nonzero capacity despite having no storage. A subclass finalizer that calls put() then writes through a NULL pointer and crashes; publish the capacity only after allocation succeeds. Backport to 3.15 and 3.14.
1 parent 0a57626 commit 5ce1ef3

2 files changed

Lines changed: 5 additions & 2 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Avoid a crash when a :class:`queue.SimpleQueue` subclass finalizer adds an
2+
item after construction fails.

‎Modules/_queuemodule.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,14 @@ RingBuf_Init(RingBuf *buf)
5151
{
5252
buf->put_idx = 0;
5353
buf->get_idx = 0;
54-
buf->items_cap = INITIAL_RING_BUF_CAPACITY;
54+
buf->items_cap = 0;
5555
buf->num_items = 0;
56-
buf->items = PyMem_Calloc(buf->items_cap, sizeof(PyObject *));
56+
buf->items = PyMem_Calloc(INITIAL_RING_BUF_CAPACITY, sizeof(PyObject *));
5757
if (buf->items == NULL) {
5858
PyErr_NoMemory();
5959
return -1;
6060
}
61+
buf->items_cap = INITIAL_RING_BUF_CAPACITY;
6162
return 0;
6263
}
6364

0 commit comments

Comments
 (0)