Skip to content

Commit

Permalink
compiler: Accept numeric literals with leading zeroes.
Browse files Browse the repository at this point in the history
When a numeric literal with leading zeroes was seen in the parser, it
would only be accepted if it were a valid hex or octal literal.  Any
invalid numeric literal would be split up into multiple tokens: the
valid hex/octal literal followed by the rest of the characters.
Instead, when scanning a numeric literal with leading zeroes, always
accept the number and give an appropriate error if the accepted number
does not fit in the expected base.

Fixes golang/go#11532, golang/go#11533.

Change-Id: I2cac2348a0ab67fe9b97b77476cd7706b63a9db3
Reviewed-on: https://go-review.googlesource.com/13791
Reviewed-by: Ian Lance Taylor <iant@golang.org>
  • Loading branch information
Chris Manghane authored and ianlancetaylor committed Aug 25, 2015
1 parent 14ca4b6 commit f97d579
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions go/lex.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,7 @@ Lex::gather_number()
pnum = p;
while (p < pend)
{
if (*p < '0' || *p > '7')
if (*p < '0' || *p > '9')
break;
++p;
}
Expand All @@ -1060,7 +1060,13 @@ Lex::gather_number()
std::string s(pnum, p - pnum);
mpz_t val;
int r = mpz_init_set_str(val, s.c_str(), base);
go_assert(r == 0);
if (r != 0)
{
if (base == 8)
error_at(this->location(), "invalid octal literal");
else
error_at(this->location(), "invalid hex literal");
}

if (neg)
mpz_neg(val, val);
Expand Down

0 comments on commit f97d579

Please sign in to comment.