Skip to content

_pickle.UnpicklingError: Memo value not found at index 1 #125756

@erik895

Description

@erik895

Bug report

Bug description:

When pickling a container object. The pickler class will store child-objects to its self.memo even if the container could not be fully pickled (and not written to the output).
This can cause references to memo objects that are not in the output stream.

import sys
import pickle
import io

buf = io.BytesIO()
pickler = pickle.Pickler(buf)
i_will_be_memorized = 'qqqqqqqqqqqq'
i_cant_be_pickled = sys

try:
    pickler.dump([i_will_be_memorized, i_cant_be_pickled])
except TypeError:
    # i_will_be_memorized is now in pickler memory despite the exception
    pass

# this dump will use the memo
pickler.dump(i_will_be_memorized)

data = buf.getbuffer().tobytes()

# so this will fail with:
#   _pickle.UnpicklingError: Memo value not found at index 1
pickle.loads(data)

CPython versions tested on:

3.12

Operating systems tested on:

Windows

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.12only security fixes3.13bugs and security fixes3.14bugs and security fixesdocsDocumentation in the Doc dirstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    Status

    No status

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions