Skip to content

Commit 596bb76

Browse files
committed
cmd/compile: reject p-notation floats in Go source files
Use pkgimport == nil (or not) to distinguish between parsing .go source files where "p" exponent specifier is not allowed and parsing .a or .o export data where it is. Use that to control error when p-exponent is seen. Fixes #9036 Change-Id: I8924f09c91d4945ef3f20e80a6e544008a94a7e4 Reviewed-on: https://go-review.googlesource.com/10450 Reviewed-by: Russ Cox <rsc@golang.org>
1 parent bd8bb67 commit 596bb76

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/cmd/compile/internal/gc/lex.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1434,6 +1434,11 @@ casedot:
14341434
}
14351435

14361436
caseep:
1437+
if importpkg == nil && (c == 'p' || c == 'P') {
1438+
// <mantissa>p<base-2-exponent> is allowed in .a/.o imports,
1439+
// but not in .go sources. See #9036.
1440+
Yyerror("malformed floating point constant")
1441+
}
14371442
cp.WriteByte(byte(c))
14381443
c = getc()
14391444
if c == '+' || c == '-' {
@@ -1442,7 +1447,7 @@ caseep:
14421447
}
14431448

14441449
if !yy_isdigit(c) {
1445-
Yyerror("malformed fp constant exponent")
1450+
Yyerror("malformed floating point constant exponent")
14461451
}
14471452
for yy_isdigit(c) {
14481453
cp.WriteByte(byte(c))

test/fixedbugs/issue9036.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// errorcheck
2+
3+
// Copyright 2015 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
// Expects to see error messages on "p" exponents.
8+
9+
package main
10+
11+
import "fmt"
12+
13+
const (
14+
x1 = 1.1 // float
15+
x2 = 1e10 // float
16+
x3 = 0x1e10 // integer (e is a hex digit)
17+
x4 = 0x1p10 // ERROR "malformed floating point constant"
18+
x5 = 1p10 // ERROR "malformed floating point constant"
19+
x6 = 0p0 // ERROR "malformed floating point constant"
20+
)
21+
22+
func main() {
23+
fmt.Printf("%g %T\n", x1, x1)
24+
fmt.Printf("%g %T\n", x2, x2)
25+
fmt.Printf("%g %T\n", x3, x3)
26+
fmt.Printf("%g %T\n", x4, x4)
27+
fmt.Printf("%g %T\n", x5, x5)
28+
fmt.Printf("%g %T\n", x6, x6)
29+
}

0 commit comments

Comments
 (0)