mirror of
https://github.com/golang/go.git
synced 2025-11-08 12:41:02 +00:00
encoding/json: check if Number is valid
json.Number is a special case which didn't have any checks and could result in invalid JSON. Fixes #10281 Change-Id: Ie3e726e4d6bf6a6aba535d36f6107013ceac913a Reviewed-on: https://go-review.googlesource.com/12250 Reviewed-by: Russ Cox <rsc@golang.org>
This commit is contained in:
parent
64cc5fd0b3
commit
c4be790c0e
4 changed files with 271 additions and 0 deletions
|
|
@ -14,6 +14,7 @@ import (
|
|||
"bytes"
|
||||
"encoding"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"math"
|
||||
"reflect"
|
||||
"runtime"
|
||||
|
|
@ -529,8 +530,12 @@ var (
|
|||
func stringEncoder(e *encodeState, v reflect.Value, quoted bool) {
|
||||
if v.Type() == numberType {
|
||||
numStr := v.String()
|
||||
// In Go1.5 the empty string encodes to "0", while this is not a valid number literal
|
||||
// we keep compatibility so check validity after this.
|
||||
if numStr == "" {
|
||||
numStr = "0" // Number's zero-val
|
||||
} else if !Number(numStr).IsValid() {
|
||||
e.error(fmt.Errorf("json: invalid number literal, trying to marshal %s", v.String()))
|
||||
}
|
||||
e.WriteString(numStr)
|
||||
return
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue