From 1f8e6fe303afa60459d3c85418172241842f9d45 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 13 Nov 2026 03:44:04 +0000 Subject: [PATCH 1/3] fix(int): allow maximum-length decimal conversions The bit-length estimate can overcount by one, rejecting valid values at the configured digit limit. Check the exact decimal length only for ambiguous boundary cases, while keeping the fast rejection of larger integers. Cover accepted and rejected limits across decimal formatting paths. Assisted-by: ChatGPT:GPT-6 --- crates/vm/src/builtins/int.rs | 9 ++++++++- extra_tests/snippets/builtin_int.py | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/crates/vm/src/builtins/int.rs b/crates/vm/src/builtins/int.rs index e93887f9f15..c11a687f889 100644 --- a/crates/vm/src/builtins/int.rs +++ b/crates/vm/src/builtins/int.rs @@ -850,7 +850,14 @@ pub(crate) fn check_int_to_str_digits(value: &BigInt, vm: &VirtualMachine) -> Py // 32-bit (wasm32) once bits ≳ 142_500, which silently accepts over-limit // conversions. let digits_upper = bits.saturating_mul(30103) / 100_000 + 1; - if digits_upper > u64::try_from(limit).unwrap_or(u64::MAX) { + let limit_u64 = u64::try_from(limit).unwrap_or(u64::MAX); + // The bit-count estimate may be one digit higher than the actual count. + // Only turn the boundary value into decimal text; reject larger values + // immediately without performing an expensive conversion. + if digits_upper > limit_u64 + && (digits_upper > limit_u64.saturating_add(1) + || format!("{value}").len() - usize::from(value.is_negative()) > limit) + { return Err(vm.new_value_error(format!( "Exceeds the limit ({limit} digits) for integer string conversion; \ use sys.set_int_max_str_digits() to increase the limit" diff --git a/extra_tests/snippets/builtin_int.py b/extra_tests/snippets/builtin_int.py index a3994476530..50dcd51f8d8 100644 --- a/extra_tests/snippets/builtin_int.py +++ b/extra_tests/snippets/builtin_int.py @@ -397,6 +397,28 @@ class SubInt(int): assert format(huge, "b") # Limit disabled: no check. + # The bit-length estimate can overstate the decimal length by one. Even + # values whose leading digit is 9 must round-trip at the configured limit. + for max_digits in (640, 4300, 5000): + sys.set_int_max_str_digits(max_digits) + digits = "9" * max_digits + boundary = int(digits) + assert str(boundary) == digits + assert repr(boundary) == digits + assert format(boundary, "d") == digits + assert format(-boundary, "d") == "-" + digits + assert "%d" % boundary == digits + assert b"%d" % boundary == digits.encode() + assert format(boundary, ",d").replace(",", "") == digits + assert f"{boundary}" == digits + if max_digits == 4300: + assert len(str(2**14284)) == 4300 + + too_many = 10**max_digits + for formatter in (str, repr, lambda n: format(n, "d"), lambda n: "%d" % n): + with assert_raises(ValueError): + formatter(too_many) + sys.set_int_max_str_digits(0) assert str(huge) finally: From 5d5db72813c5cb84e4932c2bbd5cfeb3d325e07e Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 13 Nov 2026 03:53:31 +0000 Subject: [PATCH 2/3] perf(int): use power-of-ten threshold at digit boundary Compare ambiguous values with 10 to the configured power instead of materializing decimal text solely to count it. Keep a fallback when the requested limit exceeds the exponent representation and cover negative oversized values. Assisted-by: ChatGPT:GPT-6 --- crates/vm/src/builtins/int.rs | 26 +++++++++++++++----------- extra_tests/snippets/builtin_int.py | 8 ++++---- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/crates/vm/src/builtins/int.rs b/crates/vm/src/builtins/int.rs index c11a687f889..6941e793d17 100644 --- a/crates/vm/src/builtins/int.rs +++ b/crates/vm/src/builtins/int.rs @@ -851,17 +851,21 @@ pub(crate) fn check_int_to_str_digits(value: &BigInt, vm: &VirtualMachine) -> Py // conversions. let digits_upper = bits.saturating_mul(30103) / 100_000 + 1; let limit_u64 = u64::try_from(limit).unwrap_or(u64::MAX); - // The bit-count estimate may be one digit higher than the actual count. - // Only turn the boundary value into decimal text; reject larger values - // immediately without performing an expensive conversion. - if digits_upper > limit_u64 - && (digits_upper > limit_u64.saturating_add(1) - || format!("{value}").len() - usize::from(value.is_negative()) > limit) - { - return Err(vm.new_value_error(format!( - "Exceeds the limit ({limit} digits) for integer string conversion; \ - use sys.set_int_max_str_digits() to increase the limit" - ))); + if digits_upper > limit_u64 { + // An estimate one digit over the limit may still represent a valid + // number. Comparing against 10**limit is cheaper than formatting + // the integer a second time just to count its decimal digits. + let over_limit = digits_upper > limit_u64.saturating_add(1) + || match u32::try_from(limit) { + Ok(exponent) => value.abs() >= BigInt::from(10).pow(exponent), + Err(_) => format!("{value}").len() - usize::from(value.is_negative()) > limit, + }; + if over_limit { + return Err(vm.new_value_error(format!( + "Exceeds the limit ({limit} digits) for integer string conversion; \ + use sys.set_int_max_str_digits() to increase the limit" + ))); + } } Ok(()) } diff --git a/extra_tests/snippets/builtin_int.py b/extra_tests/snippets/builtin_int.py index 50dcd51f8d8..f222db7badc 100644 --- a/extra_tests/snippets/builtin_int.py +++ b/extra_tests/snippets/builtin_int.py @@ -414,10 +414,10 @@ class SubInt(int): if max_digits == 4300: assert len(str(2**14284)) == 4300 - too_many = 10**max_digits - for formatter in (str, repr, lambda n: format(n, "d"), lambda n: "%d" % n): - with assert_raises(ValueError): - formatter(too_many) + for too_many in (10**max_digits, -(10**max_digits)): + for formatter in (str, repr, lambda n: format(n, "d"), lambda n: "%d" % n): + with assert_raises(ValueError): + formatter(too_many) sys.set_int_max_str_digits(0) assert str(huge) From e5227bcb3d8872eba2f0985d3bdb4904438d908a Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 13 Nov 2026 04:00:16 +0000 Subject: [PATCH 3/3] fix(int): avoid rejecting multi-digit estimate errors At very large bit counts the coarse base-10 upper estimate can overstate the real length by more than one. Compare against the exact power-of-ten threshold for any estimated over-limit value, preserving fast skips for clearly valid values and avoiding unnecessary decimal conversion. Assisted-by: ChatGPT:GPT-6 --- crates/vm/src/builtins/int.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/crates/vm/src/builtins/int.rs b/crates/vm/src/builtins/int.rs index 6941e793d17..8280ef0214f 100644 --- a/crates/vm/src/builtins/int.rs +++ b/crates/vm/src/builtins/int.rs @@ -852,14 +852,13 @@ pub(crate) fn check_int_to_str_digits(value: &BigInt, vm: &VirtualMachine) -> Py let digits_upper = bits.saturating_mul(30103) / 100_000 + 1; let limit_u64 = u64::try_from(limit).unwrap_or(u64::MAX); if digits_upper > limit_u64 { - // An estimate one digit over the limit may still represent a valid - // number. Comparing against 10**limit is cheaper than formatting - // the integer a second time just to count its decimal digits. - let over_limit = digits_upper > limit_u64.saturating_add(1) - || match u32::try_from(limit) { - Ok(exponent) => value.abs() >= BigInt::from(10).pow(exponent), - Err(_) => format!("{value}").len() - usize::from(value.is_negative()) > limit, - }; + // The bit-count estimate can exceed the actual decimal length by + // more than one digit for very large values. Compare with the exact + // boundary instead of rejecting based on the estimate alone. + let over_limit = match u32::try_from(limit) { + Ok(exponent) => value.abs() >= BigInt::from(10).pow(exponent), + Err(_) => format!("{value}").len() - usize::from(value.is_negative()) > limit, + }; if over_limit { return Err(vm.new_value_error(format!( "Exceeds the limit ({limit} digits) for integer string conversion; \