diff options
author | ian <ian@138bc75d-0d04-0410-961f-82ee72b054a4> | 2016-07-22 18:15:38 +0000 |
---|---|---|
committer | ian <ian@138bc75d-0d04-0410-961f-82ee72b054a4> | 2016-07-22 18:15:38 +0000 |
commit | be239ed2ba619747b64629895116f209b58baee8 (patch) | |
tree | abdbd898676e1f853fca2d7e031d105d7ebcf676 /libgo/go/expvar | |
parent | 8f60bf3b0c426d469b5e65e1ad943e21ad42d957 (diff) | |
download | gcc-be239ed2ba619747b64629895116f209b58baee8.tar.gz |
libgo: update to go1.7rc3
Reviewed-on: https://go-review.googlesource.com/25150
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@238662 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libgo/go/expvar')
-rw-r--r-- | libgo/go/expvar/expvar.go | 14 | ||||
-rw-r--r-- | libgo/go/expvar/expvar_test.go | 18 |
2 files changed, 26 insertions, 6 deletions
diff --git a/libgo/go/expvar/expvar.go b/libgo/go/expvar/expvar.go index 24c2d6b29ab..d5465c518f8 100644 --- a/libgo/go/expvar/expvar.go +++ b/libgo/go/expvar/expvar.go @@ -15,7 +15,7 @@ // memstats runtime.Memstats // // The package is sometimes only imported for the side effect of -// registering its HTTP handler and the above variables. To use it +// registering its HTTP handler and the above variables. To use it // this way, link this package into your program: // import _ "expvar" // @@ -38,6 +38,9 @@ import ( // Var is an abstract type for all exported variables. type Var interface { + // String returns a valid JSON value for the variable. + // Types with String methods that do not return valid JSON + // (such as time.Time) must not be used as a Var. String() string } @@ -218,8 +221,10 @@ type String struct { func (v *String) String() string { v.mu.RLock() - defer v.mu.RUnlock() - return strconv.Quote(v.s) + s := v.s + v.mu.RUnlock() + b, _ := json.Marshal(s) + return string(b) } func (v *String) Set(value string) { @@ -258,7 +263,8 @@ func Publish(name string, v Var) { sort.Strings(varKeys) } -// Get retrieves a named exported variable. +// Get retrieves a named exported variable. It returns nil if the name has +// not been registered. func Get(name string) Var { mutex.RLock() defer mutex.RUnlock() diff --git a/libgo/go/expvar/expvar_test.go b/libgo/go/expvar/expvar_test.go index 8bc633e4a97..7b1c9dfc4f8 100644 --- a/libgo/go/expvar/expvar_test.go +++ b/libgo/go/expvar/expvar_test.go @@ -26,6 +26,14 @@ func RemoveAll() { varKeys = nil } +func TestNil(t *testing.T) { + RemoveAll() + val := Get("missing") + if val != nil { + t.Errorf("got %v, want nil", val) + } +} + func TestInt(t *testing.T) { RemoveAll() reqs := NewInt("requests") @@ -134,8 +142,14 @@ func TestString(t *testing.T) { t.Errorf("name.s = %q, want \"Mike\"", name.s) } - if s := name.String(); s != "\"Mike\"" { - t.Errorf("reqs.String() = %q, want \"\"Mike\"\"", s) + if s, want := name.String(), `"Mike"`; s != want { + t.Errorf("from %q, name.String() = %q, want %q", name.s, s, want) + } + + // Make sure we produce safe JSON output. + name.Set(`<`) + if s, want := name.String(), "\"\\u003c\""; s != want { + t.Errorf("from %q, name.String() = %q, want %q", name.s, s, want) } } |