Skip to content

Commit

Permalink
Fix handling of NULL buffers in fopencookie functions
Browse files Browse the repository at this point in the history
  • Loading branch information
althonos committed Mar 18, 2024
1 parent ccdb403 commit 8d28bca
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pyhmmer/fileobj/linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

Py_ssize_t fileobj_linux_write(void* cookie, const char* buf, size_t size) {
PyObject* file = (PyObject*) cookie;

if (buf == NULL)
return 0;

PyObject* out = PyObject_CallMethod(file, "write", "y#", buf, (Py_ssize_t) size);
if (out == NULL)
Expand All @@ -34,6 +37,9 @@ Py_ssize_t fileobj_linux_write(void* cookie, const char* buf, size_t size) {
Py_ssize_t fileobj_linux_read(void* cookie, char* buf, size_t size) {
PyObject* file = (PyObject*) cookie;

if (buf == NULL)
return 0;

PyObject* chunk = PyObject_CallMethod(file, "read", "n", size);
if (chunk == NULL)
return _COOKIE_ERROR_READ;
Expand All @@ -60,6 +66,9 @@ Py_ssize_t fileobj_linux_read(void* cookie, char* buf, size_t size) {
Py_ssize_t fileobj_linux_readinto(void* cookie, char* buf, size_t size) {
PyObject* file = (PyObject*) cookie;

if (buf == NULL)
return 0;

PyObject* mem = PyMemoryView_FromMemory(buf, (Py_ssize_t) size, PyBUF_WRITE);
if (mem == NULL)
return _COOKIE_ERROR_READ;
Expand Down

0 comments on commit 8d28bca

Please sign in to comment.