Reject format-string field index above Py_ssize_t::MAX by changjoon-park · Pull Request #7708 · RustPython/RustPython · GitHub
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
1 change: 0 additions & 1 deletion Lib/test/test_str.py
13 changes: 13 additions & 0 deletions crates/common/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,7 @@ pub enum FormatParseError {
EmptyAttribute,
MissingRightBracket,
InvalidCharacterAfterRightBracket,
TooManyDecimalDigits,
}

impl FromStr for FormatSpec {
Expand Down Expand Up @@ -1185,7 +1186,19 @@ impl FieldName {
let field_type = if first.is_empty() {
FieldType::Auto
} else if let Some(index) = parse_usize(&first) {
// Match CPython's get_integer: digits-only segment must fit in
// Py_ssize_t. Above that, raise ValueError at parse time.
if index > isize::MAX as usize {
return Err(FormatParseError::TooManyDecimalDigits);
}
FieldType::Index(index)
} else if first
.as_str()
.ok()
.is_some_and(|s| s.bytes().all(|b| b.is_ascii_digit()))
{
// All-digit segment whose value overflows usize itself.
return Err(FormatParseError::TooManyDecimalDigits);
} else {
FieldType::Keyword(first)
};
Expand Down
3 changes: 3 additions & 0 deletions crates/vm/src/format.rs
Loading