Skip to content

Commit 2c2656d

Browse files
authored
Raise value error on large precision values for float, int __format__ (RustPython#3415)
1 parent 14b4f00 commit 2c2656d

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

Lib/test/test_format.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,8 @@ def test_optimisations(self):
466466
self.assertIs(text % (), text)
467467
self.assertIs(text.format(), text)
468468

469-
@unittest.skip("TODO: RUSTPYTHON, chews up RAM")
469+
# TODO: RustPython missing complex.__format__ implementation
470+
@unittest.expectedFailure
470471
def test_precision(self):
471472
f = 1.2
472473
self.assertEqual(format(f, ".0f"), "1")

vm/src/format.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,11 @@ fn parse_precision(text: &str) -> Result<(Option<usize>, &str), &'static str> {
204204
Ok(match chars.next() {
205205
Some('.') => {
206206
let (size, remaining) = parse_number(chars.as_str())?;
207-
if size.is_some() {
208-
(size, remaining)
207+
if let Some(size) = size {
208+
if size > i32::MAX as usize {
209+
return Err("Precision too big");
210+
}
211+
(Some(size), remaining)
209212
} else {
210213
(None, text)
211214
}

0 commit comments

Comments
 (0)