Skip to content

Commit d23dc63

Browse files
authored
Validate lock timeout range before acquire restrictions (#9061)
Preserve CPython signed nanosecond conversion precedence for reentrant and nonblocking acquisition, without changing the platform timeout maximum. Assisted-by: OpenAI Codex:model identifier unavailable
1 parent 29f819e commit d23dc63

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

‎crates/vm/src/stdlib/_thread.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ pub(crate) mod _thread {
106106

107107
fn validate_acquire_args(args: AcquireArgs, vm: &VirtualMachine) -> PyResult<(bool, f64)> {
108108
let timeout = args.timeout.to_secs_f64();
109+
// CPython checks the signed nanosecond range before acquire-specific restrictions.
110+
let timeout_ns = timeout * 1_000_000_000.0;
111+
if !(i64::MIN as f64..-(i64::MIN as f64)).contains(&timeout_ns) {
112+
return Err(vm.new_overflow_error("timestamp out of range for C PyTime_t"));
113+
}
109114
if !args.blocking && timeout != -1.0 {
110115
return Err(vm.new_value_error("can't specify a timeout for a non-blocking call"));
111116
}

‎extra_tests/snippets/builtin_thread.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@
99
(ValueError, {"blocking": False, "timeout": 1}),
1010
(ValueError, {"timeout": -2}),
1111
(OverflowError, {"timeout": _thread.TIMEOUT_MAX + 1}),
12+
(OverflowError, {"blocking": False, "timeout": 2**63 / 1e9}),
13+
(OverflowError, {"blocking": False, "timeout": -9_223_372_037}),
14+
(OverflowError, {"timeout": -9_223_372_037}),
15+
(OverflowError, {"blocking": False, "timeout": float("inf")}),
16+
(OverflowError, {"timeout": float("inf")}),
17+
(OverflowError, {"blocking": False, "timeout": float("-inf")}),
18+
(OverflowError, {"timeout": float("-inf")}),
19+
(ValueError, {"timeout": -(2**63) / 1e9}),
20+
(ValueError, {"blocking": False, "timeout": 4_294_968}),
21+
(ValueError, {"timeout": -4_294_968}),
1222
):
1323
lock = _thread.RLock()
1424
assert lock.acquire()

0 commit comments

Comments
 (0)