From 628eb28debb454e2b15bc469431b34709444dabc Mon Sep 17 00:00:00 2001 From: greg Date: Wed, 21 Feb 2018 03:52:16 -0800 Subject: [PATCH] Fix some integer overflows with binary and hex --- src/schala_lang/parsing.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/schala_lang/parsing.rs b/src/schala_lang/parsing.rs index 04211cc..d8536cb 100644 --- a/src/schala_lang/parsing.rs +++ b/src/schala_lang/parsing.rs @@ -1036,7 +1036,10 @@ fn parse_binary(digits: String) -> ParseResult { '0' => (), _ => return ParseError::new("Encountered a character not '1' or '0 while parsing a binary literal"), } - multiplier *= 2; + multiplier = match multiplier.checked_mul(2) { + Some(m) => m, + None => return ParseError::new("This binary expression will overflow") + } } Ok(result) } @@ -1049,7 +1052,10 @@ fn parse_hex(digits: String) -> ParseResult { Some(n) => result += n as u64 * multiplier, None => return ParseError::new("Encountered a non-hex digit in a hex literal"), } - multiplier *= 16; + multiplier = match multiplier.checked_mul(16) { + Some(m) => m, + None => return ParseError::new("This hex expression will overflow") + } } Ok(result) }