Skip to content

gh-151126: Check strdup result in readline on_completion - #156299

Closed
KowalskiThomas wants to merge 1 commit into
python:mainfrom
KowalskiThomas:kowalski/add-check-readline-on-completion
Closed

KowalskiThomas wants to merge 1 commit into
python:mainfrom
KowalskiThomas:kowalski/add-check-readline-on-completion

Conversation

@KowalskiThomas

@KowalskiThomas KowalskiThomas commented Aug 23, 2026 •

Copy link
Copy Markdown
Contributor

Add a NULL check and an explicit PyErr_NoMemory() before goto
error.
Comment thread Modules/readline.c
@@ -1272,6 +1272,10 @@ on_completion(const char *text, int state)
goto error;
result = strdup(PyBytes_AS_STRING(encoded));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should actually change to _PyMem_Strdup while we're here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do depending on whether we just close the PR or not!

Comment thread Modules/readline.c
result = strdup(PyBytes_AS_STRING(encoded));
Py_DECREF(encoded);
if (result == NULL) {
PyErr_NoMemory();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this works right as the goto err will clear the memory error set

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 You're right, indeed.

Looking a bit more at it, I'm starting to wonder if the lack of checking strdup result is an oversight or on purpose, since the function ignores all errors that can happen and doesn't seem to ever return / raise them.

So I'm not quite sure what I should do here. It seems like actually bubbling the exception all the way up to input would require "setting the exception" somewhere, so that the actual "reading loop" can detect it and abort/actually raise the exception.

Alternatively, it might make sense to just explicitly ignore the result of strdup (since at this point it's likely the user impact is none or near none), and if strdup it's also likely things will blow up not much later...

@KowalskiThomas

Copy link
Copy Markdown
Contributor Author

Seems like this really is an unlikely edge case and there's no straightforward fix to it (see this comment) so I will just close the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants