]> Cypherpunks repositories - gostls13.git/commitdiff
fmt: diagnose invalid verb applied to pointer
authorRuss Cox <rsc@golang.org>
Wed, 8 Feb 2012 04:37:05 +0000 (23:37 -0500)
committerRuss Cox <rsc@golang.org>
Wed, 8 Feb 2012 04:37:05 +0000 (23:37 -0500)
Fixes #2851.

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/5644048

src/pkg/fmt/fmt_test.go
src/pkg/fmt/print.go

index 44dcae46cedfeb0e50014a5373a3e636673717a3..86db9bc67c7655850cfef05423012c659c0203c6 100644 (file)
@@ -423,6 +423,7 @@ var fmttests = []struct {
        {"p0=%p", new(int), "p0=0xPTR"},
        {"p1=%s", &pValue, "p1=String(p)"}, // String method...
        {"p2=%p", &pValue, "p2=0xPTR"},     // ... not called with %p
+       {"p3=%p", (*int)(nil), "p3=0x0"},
        {"p4=%#p", new(int), "p4=PTR"},
 
        // %p on non-pointers
@@ -431,6 +432,14 @@ var fmttests = []struct {
        {"%p", make([]int, 1), "0xPTR"},
        {"%p", 27, "%!p(int=27)"}, // not a pointer at all
 
+       // %q on pointers
+       {"%q", (*int)(nil), "%!q(*int=<nil>)"},
+       {"%q", new(int), "%!q(*int=0xPTR)"},
+
+       // %v on pointers formats 0 as <nil>
+       {"%v", (*int)(nil), "<nil>"},
+       {"%v", new(int), "0xPTR"},
+
        // %d on Stringer should give integer if possible
        {"%s", time.Time{}.Month(), "January"},
        {"%d", time.Time{}.Month(), "1"},
index 3b7d3464e209a215e4d65fa7e88bca721e218c21..c3ba2f339e511bc3ccedfe68d8eab0f8e721fefc 100644 (file)
@@ -553,6 +553,14 @@ func (p *pp) fmtBytes(v []byte, verb rune, goSyntax bool, depth int) {
 }
 
 func (p *pp) fmtPointer(value reflect.Value, verb rune, goSyntax bool) {
+       switch verb {
+       case 'p', 'v', 'b', 'd', 'o', 'x', 'X':
+               // ok
+       default:
+               p.badVerb(verb)
+               return
+       }
+
        var u uintptr
        switch value.Kind() {
        case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.Slice, reflect.UnsafePointer:
@@ -561,6 +569,7 @@ func (p *pp) fmtPointer(value reflect.Value, verb rune, goSyntax bool) {
                p.badVerb(verb)
                return
        }
+
        if goSyntax {
                p.add('(')
                p.buf.WriteString(value.Type().String())
@@ -572,6 +581,8 @@ func (p *pp) fmtPointer(value reflect.Value, verb rune, goSyntax bool) {
                        p.fmt0x64(uint64(u), true)
                }
                p.add(')')
+       } else if verb == 'v' && u == 0 {
+               p.buf.Write(nilAngleBytes)
        } else {
                p.fmt0x64(uint64(u), !p.fmt.sharp)
        }
@@ -929,24 +940,7 @@ BigSwitch:
                                break BigSwitch
                        }
                }
-               if goSyntax {
-                       p.buf.WriteByte('(')
-                       p.buf.WriteString(value.Type().String())
-                       p.buf.WriteByte(')')
-                       p.buf.WriteByte('(')
-                       if v == 0 {
-                               p.buf.Write(nilBytes)
-                       } else {
-                               p.fmt0x64(uint64(v), true)
-                       }
-                       p.buf.WriteByte(')')
-                       break
-               }
-               if v == 0 {
-                       p.buf.Write(nilAngleBytes)
-                       break
-               }
-               p.fmt0x64(uint64(v), true)
+               fallthrough
        case reflect.Chan, reflect.Func, reflect.UnsafePointer:
                p.fmtPointer(value, verb, goSyntax)
        default: