FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Reject format-string field index above Py_ssize_t::MAX by changjoon-park · Pull Request #7708 · RustPython/RustPython · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) .rs  (2) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
1 change: 0 additions & 1 deletion Lib/test/test_str.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1469,7 +1469,6 @@ def test_format_huge_width(self):
with self.assertRaises(ValueError):
result = format(2.34, format_string)

@unittest.expectedFailure # TODO: RUSTPYTHON; IndexError: tuple index out of range
def test_format_huge_item_number(self):
format_string = "{{{}:.6f}}".format(sys.maxsize + 1)
with self.assertRaises(ValueError):
Expand Down
13 changes: 13 additions & 0 deletions crates/common/src/format.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ impl ToPyException for FormatParseError {
fn to_pyexception(&self, vm: &VirtualMachine) -> PyBaseExceptionRef {
match self {
Self::UnmatchedBracket => vm.new_value_error("expected '}' before end of string"),
Self::TooManyDecimalDigits => {
vm.new_value_error("Too many decimal digits in format string")
}
_ => vm.new_value_error("Unexpected error parsing format string"),
}
}
Expand Down
Loading

Back | FazBrowse Home | New Git URL