aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Pike <r@golang.org>2013-04-29 13:52:07 -0700
committerRob Pike <r@golang.org>2013-04-29 13:52:07 -0700
commitb42c8294ebe5bde8e7368716f909385cf7be148d (patch)
tree422599bdcb211e6155739bb166005aed80c57d2c
parent5b78cee3764ea71722a56dc2e1b33ae7e90e5427 (diff)
downloadgo-b42c8294ebe5bde8e7368716f909385cf7be148d.tar.gz
go-b42c8294ebe5bde8e7368716f909385cf7be148d.zip
fmt: fix crash for Printf("%.", 3)
Fixes #5311 R=golang-dev, bradfitz, iant CC=golang-dev https://golang.org/cl/8961050
-rw-r--r--src/pkg/fmt/fmt_test.go3
-rw-r--r--src/pkg/fmt/print.go2
2 files changed, 4 insertions, 1 deletions
diff --git a/src/pkg/fmt/fmt_test.go b/src/pkg/fmt/fmt_test.go
index 552f76931b..20b723a99b 100644
--- a/src/pkg/fmt/fmt_test.go
+++ b/src/pkg/fmt/fmt_test.go
@@ -497,6 +497,9 @@ var fmttests = []struct {
// causing +2+0i and +3+0i instead of 2+0i and 3+0i.
{"%v", []complex64{1, 2, 3}, "[(1+0i) (2+0i) (3+0i)]"},
{"%v", []complex128{1, 2, 3}, "[(1+0i) (2+0i) (3+0i)]"},
+
+ // Incomplete format specification caused crash.
+ {"%.", 3, "%!.(int=3)"},
}
func TestSprintf(t *testing.T) {
diff --git a/src/pkg/fmt/print.go b/src/pkg/fmt/print.go
index 7229313196..5f37fd1208 100644
--- a/src/pkg/fmt/print.go
+++ b/src/pkg/fmt/print.go
@@ -1072,7 +1072,7 @@ func (p *pp) doPrintf(format string, a []interface{}) {
p.fmt.wid, p.fmt.widPresent, i = parsenum(format, i, end)
}
// do we have precision?
- if i < end && format[i] == '.' {
+ if i+1 < end && format[i] == '.' {
if format[i+1] == '*' {
p.fmt.prec, p.fmt.precPresent, i, fieldnum = intFromArg(a, end, i+1, fieldnum)
if !p.fmt.precPresent {