Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Lib/pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -1322,12 +1322,17 @@ def load(self):
if not key:
raise EOFError
assert isinstance(key, bytes_types)
dispatch[key[0]](self)
try:
dispatch[key[0]](self)
Copy link
Member

Choose a reason for hiding this comment

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

Do not catch a KeyError for the call. Use a temporary variable for the dispatcher first.

except:
raise UnpicklingError(f"invalid load key, '\\x{key[0]:02x}'.")
except _Stop as stopinst:
return stopinst.value

# Return a list of items pushed in the stack after last MARK instruction.
def pop_mark(self):
if not self.metastack:
raise UnpicklingError("could not find MARK")
items = self.stack
self.stack = self.metastack.pop()
self.append = self.stack.append
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class PyPickleTests(AbstractPickleModuleTests, unittest.TestCase):
class PyUnpicklerTests(AbstractUnpickleTests, unittest.TestCase):

unpickler = pickle._Unpickler
bad_stack_errors = (IndexError,)
bad_stack_errors = (IndexError, pickle.UnpicklingError)
Copy link
Member

Choose a reason for hiding this comment

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

Shpuld we still catch IndexError now?

truncated_errors = (pickle.UnpicklingError, EOFError,
AttributeError, ValueError,
struct.error, IndexError, ImportError)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed incorrect exception types occurring in the CPython Unpickler implementation when loading invalid pickle data. In some cases, the CPython fallback raised internal KeyError and IndexError exceptions instead of UnpicklingError.
Loading