Skip to content
Merged
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
17 changes: 11 additions & 6 deletions crates/stdlib/src/pickle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -818,13 +818,18 @@ mod _pickle {
.ok()
.filter(|n| isize::try_from(*n).is_ok())
.ok_or_else(|| {
new_unpickling_error(
vm,
let message = if what == "FRAME" {
format!(
"FRAME length exceeds system's maximum of {} bytes",
isize::MAX
)
} else {
format!(
"{what} exceeds system's maximum size of {} bytes",
isize::MAX
),
)
)
};
vm.new_overflow_error(message)
})
}

Expand Down Expand Up @@ -1090,7 +1095,7 @@ mod _pickle {
}
Some(BINBYTES8) => {
let arr: [u8; 8] = st.read_n(8, vm)?.try_into().unwrap();
let n = read_size(&arr, vm, "BINBYTES8")?;
let n = read_size(&arr, vm, "BINBYTES")?;
let data = st.read_n(n, vm)?.to_vec();
stack.push(vm.ctx.new_bytes(data).into());
}
Expand Down Expand Up @@ -1129,7 +1134,7 @@ mod _pickle {
}
Some(BINUNICODE8) => {
let arr: [u8; 8] = st.read_n(8, vm)?.try_into().unwrap();
let n = read_size(&arr, vm, "BINUNICODE8")?;
let n = read_size(&arr, vm, "BINUNICODE")?;
let data = st.read_n(n, vm)?.to_vec();
stack.push(utf8_surrogatepass(&data, vm)?);
}
Expand Down
22 changes: 22 additions & 0 deletions extra_tests/snippets/stdlib_pickle.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import _pickle
import io
import sys

from testutils import assert_raises

# BINPUT leaves holes; MEMOIZE uses the number of present entries.
unpickler = _pickle.Unpickler(io.BytesIO(b"\x80\x04]q\x07\x94h\x01\x86."))
Expand All @@ -13,3 +16,22 @@
# Slot wrapper descriptors reduce to lookup on their defining class.
assert object.__str__.__reduce__() == (getattr, (object, "__str__"))
assert _pickle.loads(_pickle.dumps(object.__str__)) is object.__str__

# Oversized advertised lengths are rejected before trying to read or allocate.
for opcode, name in (

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Remove the forbidden test-logic changes

This block adds new test data, control flow, and assertions to a test file, but the repository rules restrict test-file changes to adding or removing expectedFailure decorators. Revert or relocate this regression block rather than changing test logic here so the patch complies with the required test policy.

AGENTS.md reference: AGENTS.md:L273-L279

Useful? React with 👍 / 👎.

(b"\x8e", "BINBYTES"),
(b"\x8d", "BINUNICODE"),
(b"\x95", "FRAME"),
):
data = b"\x80\x04" + opcode + (sys.maxsize + 1).to_bytes(8, "little")
for load in (
_pickle.loads,
lambda data: _pickle.Unpickler(io.BytesIO(data)).load(),
):
with assert_raises(OverflowError) as caught:
load(data)
if name == "FRAME":
expected = f"FRAME length exceeds system's maximum of {sys.maxsize} bytes"
else:
expected = f"{name} exceeds system's maximum size of {sys.maxsize} bytes"
assert str(caught.exception) == expected
Loading