From 4d2747e4b8c8588b7d34d11dcd622e89b8babfde Mon Sep 17 00:00:00 2001 From: Daniel Watkins Date: Fri, 29 Oct 2021 08:56:44 -0400 Subject: [PATCH] cformat.rs: reject %c args > 255 when byte formatting This matches CPython's behaviour. Thanks to @DimitrisJim for the improved implementation here. --- vm/src/cformat.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/vm/src/cformat.rs b/vm/src/cformat.rs index 6fb383c71..6da6a1bbc 100644 --- a/vm/src/cformat.rs +++ b/vm/src/cformat.rs @@ -440,12 +440,9 @@ impl CFormatSpec { CFormatType::Character => { if let Some(i) = obj.payload::() { let ch = i - .as_bigint() - .to_u32() - .and_then(std::char::from_u32) - .ok_or_else(|| { - vm.new_overflow_error("%c arg not in range(0x110000)".to_owned()) - })?; + .try_to_primitive::(vm) + .map_err(|_| vm.new_overflow_error("%c arg not in range(256)".to_owned()))? + as char; return Ok(self.format_char(ch).into_bytes()); } if let Some(b) = obj.payload::() {