From 7c08f0162cf6254d101e5f04dd58993ae05fa97e Mon Sep 17 00:00:00 2001 From: Thomas Kowalski Date: Sun, 23 Aug 2026 16:35:21 +0200 Subject: [PATCH] gh-151126: Check strdup result in readline on_completion Add a NULL check and an explicit PyErr_NoMemory() before goto error. --- .../Library/2026-08-23-16-36-00.gh-issue-151126.m3YbXU.rst | 2 ++ Modules/readline.c | 4 ++++ 2 files changed, 6 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-08-23-16-36-00.gh-issue-151126.m3YbXU.rst diff --git a/Misc/NEWS.d/next/Library/2026-08-23-16-36-00.gh-issue-151126.m3YbXU.rst b/Misc/NEWS.d/next/Library/2026-08-23-16-36-00.gh-issue-151126.m3YbXU.rst new file mode 100644 index 000000000000000..4a17834c7f2a907 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-08-23-16-36-00.gh-issue-151126.m3YbXU.rst @@ -0,0 +1,2 @@ +Raise :exc:`MemoryError` on allocation failure in the :mod:`!readline` +completion callback. diff --git a/Modules/readline.c b/Modules/readline.c index fc79a5866dfd38c..cbc8c872e7b3d64 100644 --- a/Modules/readline.c +++ b/Modules/readline.c @@ -1272,6 +1272,10 @@ on_completion(const char *text, int state) goto error; result = strdup(PyBytes_AS_STRING(encoded)); Py_DECREF(encoded); + if (result == NULL) { + PyErr_NoMemory(); + goto error; + } } Py_DECREF(r); goto done;