1
0
mirror of https://github.com/golang/go synced 2024-10-03 23:21:22 -06:00

fmt: Scan(&int) was mishandling a lone zero.

It took it as an octal base prefix but assumed more digits were coming.
Fixes #2077.

R=golang-dev, dsymonds
CC=golang-dev
https://golang.org/cl/4764044
This commit is contained in:
Rob Pike 2011-07-18 10:05:35 +10:00
parent 8c5c3c504c
commit f189308fb4
2 changed files with 7 additions and 3 deletions

View File

@ -550,9 +550,11 @@ func (s *ss) getBase(verb int) (base int, digits string) {
// scanNumber returns the numerical string with specified digits starting here.
func (s *ss) scanNumber(digits string, haveDigits bool) string {
s.notEOF()
if !haveDigits && !s.accept(digits) {
s.errorString("expected integer")
if !haveDigits {
s.notEOF()
if !s.accept(digits) {
s.errorString("expected integer")
}
}
for s.accept(digits) {
}

View File

@ -298,6 +298,8 @@ var scanfTests = []ScanfTest{
// Fixed bugs
{"%d\n", "27\n", &intVal, 27}, // ok
{"%d\n", "28 \n", &intVal, 28}, // was: "unexpected newline"
{"%v", "0", &intVal, 0}, // was: "EOF"; 0 was taken as base prefix and not counted.
{"%v", "0", &uintVal, uint(0)}, // was: "EOF"; 0 was taken as base prefix and not counted.
}
var overflowTests = []ScanTest{