Skip to content

Commit

Permalink
libsyntax: fail lexing with an error message on an int literal larger…
Browse files Browse the repository at this point in the history
… than 2^64.

Stops an ICE.

Closes #5544.
  • Loading branch information
huonw committed Apr 8, 2013
1 parent 41c6f67 commit 0c2ceb1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/libsyntax/parse/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,11 @@ fn scan_number(c: char, rdr: @mut StringReader) -> token::Token {
if str::len(num_str) == 0u {
rdr.fatal(~"no valid digits found for number");
}
let parsed = u64::from_str_radix(num_str, base as uint).get();
let parsed = match u64::from_str_radix(num_str, base as uint) {
Some(p) => p,
None => rdr.fatal(~"int literal is too large")
};

match tp {
either::Left(t) => return token::LIT_INT(parsed as i64, t),
either::Right(t) => return token::LIT_UINT(parsed, t)
Expand Down Expand Up @@ -503,7 +507,10 @@ fn scan_number(c: char, rdr: @mut StringReader) -> token::Token {
if str::len(num_str) == 0u {
rdr.fatal(~"no valid digits found for number");
}
let parsed = u64::from_str_radix(num_str, base as uint).get();
let parsed = match u64::from_str_radix(num_str, base as uint) {
Some(p) => p,
None => rdr.fatal(~"int literal is too large")
};

debug!("lexing %s as an unsuffixed integer literal",
num_str);
Expand Down
14 changes: 14 additions & 0 deletions src/test/compile-fail/issue-5544-a.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn main() {
let _i = 18446744073709551616; // 2^64
//~^ ERROR int literal is too large
}
14 changes: 14 additions & 0 deletions src/test/compile-fail/issue-5544-b.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn main() {
let _i = 0xff_ffff_ffff_ffff_ffff;
//~^ ERROR int literal is too large
}

5 comments on commit 0c2ceb1

@bors
Copy link
Contributor

@bors bors commented on 0c2ceb1 Apr 8, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 0c2ceb1 Apr 8, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging dbaupp/rust/syntax-parse-large-number = 0c2ceb1 into auto

@bors
Copy link
Contributor

@bors bors commented on 0c2ceb1 Apr 8, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dbaupp/rust/syntax-parse-large-number = 0c2ceb1 merged ok, testing candidate = 913ca08

@bors
Copy link
Contributor

@bors bors commented on 0c2ceb1 Apr 8, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 0c2ceb1 Apr 8, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding incoming to auto = 913ca08

Please sign in to comment.