From f098d70145d6f2fe10855a74f5c70618e1ba88fe Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 15 Jan 2015 15:36:54 -0800 Subject: [PATCH] Disallow leading 0s in integers/floats --- src/parser.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/parser.rs b/src/parser.rs index e2e116f..e4dc7f1 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -466,9 +466,22 @@ impl<'a> Parser<'a> { fn number_or_datetime(&mut self, mut start: usize) -> Option { let sign = if self.eat('+') { start += 1; true } else {self.eat('-')}; let mut is_float = false; + if self.eat('0') { + match self.peek(0) { + Some((pos, c)) if '0' <= c && c <= '9' => { + self.errors.push(ParserError { + lo: start, + hi: pos, + desc: format!("leading zeroes are not allowed"), + }); + return None + } + _ => {} + } + } loop { match self.cur.clone().next() { - Some((_, ch)) if ch.is_digit(10) => { self.cur.next(); } + Some((_, ch)) if '0' <= ch && ch <= '9' => { self.cur.next(); } Some((_, '.')) if !is_float => { is_float = true; self.cur.next(); @@ -983,4 +996,16 @@ trimmed in raw strings. assert!(Parser::new("a = \"\n\"").parse().is_none()); assert!(Parser::new("a = '\n'").parse().is_none()); } + + #[test] + fn bad_leading_zeros() { + assert!(Parser::new("a = 00").parse().is_none()); + assert!(Parser::new("a = -00").parse().is_none()); + assert!(Parser::new("a = +00").parse().is_none()); + assert!(Parser::new("a = 00.0").parse().is_none()); + assert!(Parser::new("a = -00.0").parse().is_none()); + assert!(Parser::new("a = +00.0").parse().is_none()); + assert!(Parser::new("a = 9223372036854775808").parse().is_none()); + assert!(Parser::new("a = -9223372036854775809").parse().is_none()); + } }