From 9381d9fbd0a1bf07c265ca0badc261bf82411bf4 Mon Sep 17 00:00:00 2001 From: Yuri Konotopov Date: Fri, 28 Aug 2026 07:50:37 +0000 Subject: [PATCH 2/5] Fixed buffer sizes handling on 32 bit platforms Signed-off-by: Yuri Konotopov Forwarded: https://github.com/mosquito/caio/pull/76 --- caio/linux_aio.c | 15 +++++++++++++-- caio/linux_uring.c | 9 +++++++++ caio/thread_aio.c | 15 ++++++++++++--- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/caio/linux_aio.c b/caio/linux_aio.c index f610d83..a519b88 100644 --- a/caio/linux_aio.c +++ b/caio/linux_aio.c @@ -804,12 +804,21 @@ static PyObject* AIOOperation_read( return NULL; } + if (nbytes > (uint64_t) PY_SSIZE_T_MAX) { + Py_DECREF(self); + PyErr_SetString( + PyExc_OverflowError, + "nbytes does not fit in Py_ssize_t" + ); + return NULL; + } + /* PyMem_Calloc can return NULL for a large enough (or just OOM-at-the- * time) nbytes - proceeding with a NULL buf would hand the kernel (via * aio_buf) and PyMemoryView_FromMemory a NULL pointer with a nonzero * declared size, corrupting memory instead of raising a catchable * error. */ - self->buffer = PyMem_Calloc(nbytes, sizeof(char)); + self->buffer = PyMem_Calloc((size_t) nbytes, sizeof(char)); if (self->buffer == NULL && nbytes > 0) { Py_DECREF(self); PyErr_NoMemory(); @@ -817,7 +826,9 @@ static PyObject* AIOOperation_read( } self->iocb.aio_buf = (uint64_t)(uintptr_t) self->buffer; self->iocb.aio_nbytes = nbytes; - self->py_buffer = PyMemoryView_FromMemory(self->buffer, nbytes, PyBUF_READ); + self->py_buffer = PyMemoryView_FromMemory( + self->buffer, (Py_ssize_t) nbytes, PyBUF_READ + ); if (self->py_buffer == NULL) { Py_DECREF(self); return NULL; diff --git a/caio/linux_uring.c b/caio/linux_uring.c index 876a951..abd5200 100644 --- a/caio/linux_uring.c +++ b/caio/linux_uring.c @@ -219,6 +219,15 @@ static PyObject *AIOOperation_read( return NULL; } + if (nbytes > UINT32_MAX || nbytes > (uint64_t) PY_SSIZE_T_MAX) { + Py_DECREF(self); + PyErr_SetString( + PyExc_OverflowError, + "nbytes exceeds the io_uring read limit" + ); + return NULL; + } + /* Allocate the result bytes object directly — the kernel writes into * its internal buffer, so get_value() can return it with no copy. * PyBytes_FromStringAndSize(NULL, n) leaves the memory uninitialized diff --git a/caio/thread_aio.c b/caio/thread_aio.c index 3666c96..b8d96d8 100644 --- a/caio/thread_aio.c +++ b/caio/thread_aio.c @@ -637,18 +637,27 @@ static PyObject* AIOOperation_read( return NULL; } + if (nbytes > (uint64_t) PY_SSIZE_T_MAX) { + Py_DECREF(self); + PyErr_SetString( + PyExc_OverflowError, + "nbytes does not fit in Py_ssize_t" + ); + return NULL; + } + // PyMem_Calloc can return NULL for a large enough (or just // OOM-at-the-time) nbytes - proceeding with a NULL buf would hand the // kernel (via pread() in worker()) and PyMemoryView_FromMemory a NULL // pointer with a nonzero declared size, corrupting memory instead of // raising a catchable error. - self->buf = PyMem_Calloc(nbytes, sizeof(char)); + self->buf = PyMem_Calloc((size_t) nbytes, sizeof(char)); if (self->buf == NULL && nbytes > 0) { Py_DECREF(self); PyErr_NoMemory(); return NULL; } - self->buf_size = nbytes; + self->buf_size = (Py_ssize_t) nbytes; self->py_buffer = PyMemoryView_FromMemory( self->buf, @@ -993,7 +1002,7 @@ static PyMemberDef AIOOperation_members[] = { READONLY, "offset" }, { - "nbytes", T_ULONGLONG, + "nbytes", T_PYSSIZET, offsetof(AIOOperation, buf_size), READONLY, "nbytes" }, -- 2.55.0