gh-151862: Fix NULL deref on non-str AttributeError when unpickling#151863
Open
tonghuaroot wants to merge 2 commits into
Open
gh-151862: Fix NULL deref on non-str AttributeError when unpickling#151863tonghuaroot wants to merge 2 commits into
tonghuaroot wants to merge 2 commits into
Conversation
…ling An object crossing interpreters via a queue or channel is unpickled on the receive side. When that unpickling raised an ``AttributeError`` whose first argument was not a UTF-8-encodable string, ``PyUnicode_AsUTF8()`` returned ``NULL`` and the subsequent ``strncmp(NULL, ...)`` in ``check_missing___main___attr()`` dereferenced it, crashing the interpreter. Guard against the ``NULL`` result and treat it as not a missing-``__main__``-attribute error.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Python/crossinterp.c'scheck_missing___main___attr()passed the result ofPyUnicode_AsUTF8()straight tostrncmp()without aNULLcheck.msgobjisargs[0]of anAttributeErrorraised on the receive side of the cross-interpreter pickle fallback (aconcurrent.interpretersqueue or channel). Whenargs[0]is not astr(AttributeError(42),AttributeError(b'x'),AttributeError(None)) or is astrwith lone surrogates (AttributeError('\ud800')),PyUnicode_AsUTF8()returnsNULLandstrncmp(NULL, ...)crashes the interpreter.This adds a
NULLguard matching the checked sibling helper_copy_string_obj_raw(). BecausePyUnicode_AsUTF8sets an exception on failure and the function asserts!PyErr_Occurred()on entry, the guard clears it,Py_DECREF(msgobj)(mirroring the success-path decref), and returns 0 (not a missing-__main__attribute), so the failure degrades to the normalNotShareableError.A regression test is added in
Lib/test/test_interpreters/test_queues.pycovering all fourargs[0]shapes (42,b'x',None,'\ud800'— the surrogate case is a distinct branch:args[0]is unicode but fails UTF-8 encoding), plus a positive control with normalstrargs (including the genuine missing-__main__-attribute message shape) to lock in the non-NULLstrncmp()path. Without the fix the test crashes; with it the queue raisesNotShareableError.This fixes the
check_missing___main___attr()crash reached through the queue/channel receive path. A separate crash on theInterpreter.call()result-preserve path is reported independently.