Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-128965: pickle load_build function checks if state is None, not False #128966

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Legoclones
Copy link
Contributor

@Legoclones Legoclones commented Jan 17, 2025

Inside of the load_build() function for pickle's BUILD opcode, the C accelerator at one point checks if state is Py_None, while the Python version only checks if state.

if (state != Py_None) {

if state:

This means if state is something like an empty dictionary or tuple, the code block under the if statement WILL be run in _pickle.c, but NOT in pickle.py.

As an example, the bytestream b']]b.' has the following disassembly:

    0: ]    EMPTY_LIST
    1: ]    EMPTY_LIST
    2: b    BUILD
    3: .    STOP
highest protocol among opcodes = 1

This will do nothing in pickle.py but error out in _pickle.c with the message state is not a dictionary. The easy solution is to change if state to if state != None, and it shouldn't break any existing functionality. I've attached a pull request.

Copy link
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

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

Can we have tests that check this code path? Namely check that non-dict objects raise a TypeError and that falsey dicts do not raise exceptions? In addition, we need a NEWS entry for the bugfix, e.g:

Fix :meth:`!pickle._Pickler.load_build` for non-dictionary states.

(My NEWS entry is definitely poorly worded but I don't have a better suggestion for now. Maybe you can come up with a better formulation).

@@ -1837,7 +1837,7 @@ def load_build(self):
slotstate = None
if isinstance(state, tuple) and len(state) == 2:
state, slotstate = state
if state:
if state != None:
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if state != None:
if state is not None:

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