From be96719f990978a867f52c48f29d43f6b591da28 Mon Sep 17 00:00:00 2001 From: Josh Baker Date: Wed, 30 Aug 2017 10:08:10 -0700 Subject: [PATCH] incomplete surrogate codepoints, fixes #38 --- gjson.go | 2 +- gjson_test.go | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/gjson.go b/gjson.go index 90b4929..f9fe002 100644 --- a/gjson.go +++ b/gjson.go @@ -1448,7 +1448,7 @@ func unescape(json string) string { //, error) { i += 5 if utf16.IsSurrogate(r) { // need another code - if len(json) >= 6 && json[i] == '\\' && json[i+1] == 'u' { + if len(json[i:]) >= 6 && json[i] == '\\' && json[i+1] == 'u' { // we expect it to be correct so just consume it r = utf16.DecodeRune(r, runeit(json[i+2:])) i += 6 diff --git a/gjson_test.go b/gjson_test.go index 2949774..57f0425 100644 --- a/gjson_test.go +++ b/gjson_test.go @@ -285,7 +285,17 @@ func TestPlus53BitInts(t *testing.T) { // flip the number to the negative sign. assert(t, Get(json, "overflow_int64").Int() == -9223372036854775808) } - +func TestIssue38(t *testing.T) { + // These should not fail, even though the unicode is invalid. + Get(`["S3O PEDRO DO BUTI\udf93"]`, "0") + Get(`["S3O PEDRO DO BUTI\udf93asdf"]`, "0") + Get(`["S3O PEDRO DO BUTI\udf93\u"]`, "0") + Get(`["S3O PEDRO DO BUTI\udf93\u1"]`, "0") + Get(`["S3O PEDRO DO BUTI\udf93\u13"]`, "0") + Get(`["S3O PEDRO DO BUTI\udf93\u134"]`, "0") + Get(`["S3O PEDRO DO BUTI\udf93\u1345"]`, "0") + Get(`["S3O PEDRO DO BUTI\udf93\u1345asd"]`, "0") +} func TestTypes(t *testing.T) { assert(t, (Result{Type: String}).Type.String() == "String") assert(t, (Result{Type: Number}).Type.String() == "Number")