mirror of
https://github.com/golang/go
synced 2024-11-12 06:20:22 -07:00
expvar: Ensure strings are written as valid JSON.
Change-Id: I5147dbf4e85cf42cd1f32c57861e4c16d9dbd049 Reviewed-on: https://go-review.googlesource.com/21529 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Andrew Gerrand <adg@golang.org>
This commit is contained in:
parent
870d997ab4
commit
6f2a8810b0
@ -219,8 +219,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) {
|
||||
|
@ -142,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)
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user