Fix false validation

closes #73
This commit is contained in:
Josh Baker 2018-06-13 11:46:59 -07:00
parent 3cd3a11923
commit afaeb95620
2 changed files with 77 additions and 69 deletions

View File

@ -1865,8 +1865,14 @@ func validobject(data []byte, i int) (outi int, ok bool) {
if data[i] == '}' { if data[i] == '}' {
return i + 1, true return i + 1, true
} }
i++
for ; i < len(data); i++ { for ; i < len(data); i++ {
if data[i] == '"' { switch data[i] {
default:
return i, false
case ' ', '\t', '\n', '\r':
continue
case '"':
goto key goto key
} }
} }

View File

@ -970,80 +970,82 @@ func TestUnmarshal(t *testing.T) {
assert(t, str == Get(complicatedJSON, "LeftOut").String()) assert(t, str == Get(complicatedJSON, "LeftOut").String())
} }
func testvalid(json string, expect bool) { func testvalid(t *testing.T, json string, expect bool) {
t.Helper()
_, ok := validpayload([]byte(json), 0) _, ok := validpayload([]byte(json), 0)
if ok != expect { if ok != expect {
panic("mismatch") t.Fatal("mismatch")
} }
} }
func TestValidBasic(t *testing.T) { func TestValidBasic(t *testing.T) {
testvalid("0", true) testvalid(t, "0", true)
testvalid("00", false) testvalid(t, "00", false)
testvalid("-00", false) testvalid(t, "-00", false)
testvalid("-.", false) testvalid(t, "-.", false)
testvalid("0.0", true) testvalid(t, "0.0", true)
testvalid("10.0", true) testvalid(t, "10.0", true)
testvalid("10e1", true) testvalid(t, "10e1", true)
testvalid("10EE", false) testvalid(t, "10EE", false)
testvalid("10E-", false) testvalid(t, "10E-", false)
testvalid("10E+", false) testvalid(t, "10E+", false)
testvalid("10E123", true) testvalid(t, "10E123", true)
testvalid("10E-123", true) testvalid(t, "10E-123", true)
testvalid("10E-0123", true) testvalid(t, "10E-0123", true)
testvalid("", false) testvalid(t, "", false)
testvalid(" ", false) testvalid(t, " ", false)
testvalid("{}", true) testvalid(t, "{}", true)
testvalid("{", false) testvalid(t, "{", false)
testvalid("-", false) testvalid(t, "-", false)
testvalid("-1", true) testvalid(t, "-1", true)
testvalid("-1.", false) testvalid(t, "-1.", false)
testvalid("-1.0", true) testvalid(t, "-1.0", true)
testvalid(" -1.0", true) testvalid(t, " -1.0", true)
testvalid(" -1.0 ", true) testvalid(t, " -1.0 ", true)
testvalid("-1.0 ", true) testvalid(t, "-1.0 ", true)
testvalid("-1.0 i", false) testvalid(t, "-1.0 i", false)
testvalid("-1.0 i", false) testvalid(t, "-1.0 i", false)
testvalid("true", true) testvalid(t, "true", true)
testvalid(" true", true) testvalid(t, " true", true)
testvalid(" true ", true) testvalid(t, " true ", true)
testvalid(" True ", false) testvalid(t, " True ", false)
testvalid(" tru", false) testvalid(t, " tru", false)
testvalid("false", true) testvalid(t, "false", true)
testvalid(" false", true) testvalid(t, " false", true)
testvalid(" false ", true) testvalid(t, " false ", true)
testvalid(" False ", false) testvalid(t, " False ", false)
testvalid(" fals", false) testvalid(t, " fals", false)
testvalid("null", true) testvalid(t, "null", true)
testvalid(" null", true) testvalid(t, " null", true)
testvalid(" null ", true) testvalid(t, " null ", true)
testvalid(" Null ", false) testvalid(t, " Null ", false)
testvalid(" nul", false) testvalid(t, " nul", false)
testvalid(" []", true) testvalid(t, " []", true)
testvalid(" [true]", true) testvalid(t, " [true]", true)
testvalid(" [ true, null ]", true) testvalid(t, " [ true, null ]", true)
testvalid(" [ true,]", false) testvalid(t, " [ true,]", false)
testvalid(`{"hello":"world"}`, true) testvalid(t, `{"hello":"world"}`, true)
testvalid(`{ "hello": "world" }`, true) testvalid(t, `{ "hello": "world" }`, true)
testvalid(`{ "hello": "world", }`, false) testvalid(t, `{ "hello": "world", }`, false)
testvalid(`{"a":"b",}`, false) testvalid(t, `{"a":"b",}`, false)
testvalid(`{"a":"b","a"}`, false) testvalid(t, `{"a":"b","a"}`, false)
testvalid(`{"a":"b","a":}`, false) testvalid(t, `{"a":"b","a":}`, false)
testvalid(`{"a":"b","a":1}`, true) testvalid(t, `{"a":"b","a":1}`, true)
testvalid(`{"a":"b","a": 1, "c":{"hi":"there"} }`, true) testvalid(t, `{"a":"b",2"1":2}`, false)
testvalid(`{"a":"b","a": 1, "c":{"hi":"there", "easy":["going",{"mixed":"bag"}]} }`, true) testvalid(t, `{"a":"b","a": 1, "c":{"hi":"there"} }`, true)
testvalid(`""`, true) testvalid(t, `{"a":"b","a": 1, "c":{"hi":"there", "easy":["going",{"mixed":"bag"}]} }`, true)
testvalid(`"`, false) testvalid(t, `""`, true)
testvalid(`"\n"`, true) testvalid(t, `"`, false)
testvalid(`"\"`, false) testvalid(t, `"\n"`, true)
testvalid(`"\\"`, true) testvalid(t, `"\"`, false)
testvalid(`"a\\b"`, true) testvalid(t, `"\\"`, true)
testvalid(`"a\\b\\\"a"`, true) testvalid(t, `"a\\b"`, true)
testvalid(`"a\\b\\\uFFAAa"`, true) testvalid(t, `"a\\b\\\"a"`, true)
testvalid(`"a\\b\\\uFFAZa"`, false) testvalid(t, `"a\\b\\\uFFAAa"`, true)
testvalid(`"a\\b\\\uFFA"`, false) testvalid(t, `"a\\b\\\uFFAZa"`, false)
testvalid(string(complicatedJSON), true) testvalid(t, `"a\\b\\\uFFA"`, false)
testvalid(string(exampleJSON), true) testvalid(t, string(complicatedJSON), true)
testvalid(t, string(exampleJSON), true)
} }
var jsonchars = []string{"{", "[", ",", ":", "}", "]", "1", "0", "true", "false", "null", `""`, `"\""`, `"a"`} var jsonchars = []string{"{", "[", ",", ":", "}", "]", "1", "0", "true", "false", "null", `""`, `"\""`, `"a"`}