Skip to content

pickletools.dis() doesn't allow redefinition of memo items while pickle itself doesn't mind #123309

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

Closed
birkenfeld opened this issue Aug 25, 2024 · 3 comments
Labels
3.14 bugs and security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@birkenfeld
Copy link
Member

birkenfeld commented Aug 25, 2024

Bug report

Bug description:

pickletools' disassembler has a check here that doesn't allow memo indices to be redefined, while the pickle.Unpickler doesn't care about it, an existing item is handled without complaint here. FWIW, the opcode doc doesn't mention a restriction either.

Such pickles aren't produced by the Pickler, but it is strange for a debug tool to be more restrictive than the thing it is supposed to help debug. (I found this while fuzzing a non-Python unpickling library.) I think the check in pickletools L2497 can just be removed. A warning could also be emitted, but that would be the first such warning in pickletools.

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Linked PRs

@birkenfeld birkenfeld added type-bug An unexpected behavior, bug, or error stdlib Python modules in the Lib dir labels Aug 25, 2024
@picnixz
Copy link
Member

picnixz commented Aug 25, 2024

cc @serhiy-storchaka as someone who works on pickle a lot recently

@serhiy-storchaka
Copy link
Member

I agree, this check can be removed. It is perhaps not worth to emit a warning, as no warnings are emitted for other weird code.

The error message is not even always correct. For example, if there is "PUT 1" and then "MEMOIZE", the error message will be "memo key None already defined".

>>> import pickletools
>>> pickletools.dis(b'Np1\nN\x94')
    0: N    NONE
    1: p    PUT        1
    4: N    NONE
    5: \x94 MEMOIZE    (as 1)
Traceback (most recent call last):
  File "<python-input-1>", line 1, in <module>
    pickletools.dis(b'Np1\nN\x94')
    ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^
  File "/home/serhiy/py/cpython/Lib/pickletools.py", line 2531, in dis
    raise ValueError(errormsg)
ValueError: memo key None already defined

@serhiy-storchaka
Copy link
Member

Surprisingly, there are not any tests for pickletools.dis(), except doctests, which do not cover all corner cases. So I am going first to add tests.

This check itself is not a bug, it is even explicitly mentioned as a feature in the module comments. We can remove it because current unpickling implementations support redefinition of memo items, and this does not left unpickler in inconsistent state. All other checks are necessary for correct work of dis().

serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Aug 26, 2024
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Aug 27, 2024
….dis()

Such pickles are supported by the Unpickler even if the Pickler does not
produce them.
serhiy-storchaka added a commit that referenced this issue Aug 31, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Aug 31, 2024
…123355)

Add tests for genops() and dis().
(cherry picked from commit e5a567b)

Co-authored-by: Serhiy Storchaka <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Aug 31, 2024
…123355)

Add tests for genops() and dis().
(cherry picked from commit e5a567b)

Co-authored-by: Serhiy Storchaka <[email protected]>
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Aug 31, 2024
….dis()

Such pickles are supported by the Unpickler even if the Pickler does not
produce them.
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Aug 31, 2024
….dis()

Such pickles are supported by the Unpickler even if the Pickler does not
produce them.
serhiy-storchaka added a commit that referenced this issue Aug 31, 2024
…GH-123374)

Such pickles are supported by the Unpickler even if the Pickler does not
produce them.
Yhg1s pushed a commit that referenced this issue Sep 2, 2024
… (#123533)

gh-123309: Add more tests for the pickletools module (GH-123355)

Add tests for genops() and dis().
(cherry picked from commit e5a567b)

Co-authored-by: Serhiy Storchaka <[email protected]>
ambv pushed a commit that referenced this issue Sep 6, 2024
… (#123534)

Add tests for genops() and dis().
(cherry picked from commit e5a567b)

Co-authored-by: Serhiy Storchaka <[email protected]>
@serhiy-storchaka serhiy-storchaka added the 3.14 bugs and security fixes label Nov 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.14 bugs and security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
Status: Done
Development

No branches or pull requests

3 participants