Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support windows escaped windows paths in embedded json #166

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions decode_embedded_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ func TestDecodeEmbeddedJSONUnmarshalAPI(t *testing.T) {
expectedEmbedded string
err bool
}{
{
name: "windows paths escaped",
json: []byte(`{"id":"someid","method":"getmydata","params":"C:\\\\ProgramData\\\\MySQL\\\\MySQL Server 8.0\\\\Data\\\\#innodb_temp\\\\", "more":123}`),
expectedEmbedded: `"C:\\\\ProgramData\\\\MySQL\\\\MySQL Server 8.0\\\\Data\\\\#innodb_temp\\\\"`,
err: false,
},
{
name: "windows paths escaped",
json: []byte(`{"id":"someid","method":"getmydata","params":"C:\\\\ProgramData\\\\MySQL\\\\MySQL Server 8.0\\\\Data\\\\#innodb_temp\\\\jjjj", "more":123}`),
expectedEmbedded: `"C:\\\\ProgramData\\\\MySQL\\\\MySQL Server 8.0\\\\Data\\\\#innodb_temp\\\\jjjj"`,
err: false,
},
{
name: "decode-basic-string",
json: []byte(`{"id":"someid","method":"getmydata","params":"raw data", "more":123}`),
Expand Down
9 changes: 7 additions & 2 deletions decode_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ func (dec *Decoder) getString() (int, int, error) {
return 0, 0, dec.raiseInvalidJSONErr(dec.cursor)
}

// expecting the dec.cursor to be after the opening slash
func (dec *Decoder) skipEscapedString() error {
start := dec.cursor
for ; dec.cursor < dec.length || dec.read(); dec.cursor++ {
Expand All @@ -173,9 +174,12 @@ func (dec *Decoder) skipEscapedString() error {
nSlash := dec.cursor - start
switch d {
case '"':
// nSlash must be odd
// since we expecting the start to be after the opening slash
// if nSlash is odd then the escaped string contains the quote
// if nSlash is even, the quote is outside the escaped string
if nSlash&1 != 1 {
return dec.raiseInvalidJSONErr(dec.cursor)
// return dec.raiseInvalidJSONErr(dec.cursor)
dec.cursor--
}
return nil
case 'u': // is unicode, we skip the following characters and place the cursor one one byte backward to avoid it breaking when returning to skipString
Expand All @@ -198,6 +202,7 @@ func (dec *Decoder) skipEscapedString() error {
return dec.raiseInvalidJSONErr(dec.cursor)
}

// expecting dec.cusror to be after the opening quote
func (dec *Decoder) skipString() error {
for dec.cursor < dec.length || dec.read() {
switch dec.data[dec.cursor] {
Expand Down
25 changes: 19 additions & 6 deletions decode_string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ func TestDecoderString(t *testing.T) {
err bool
errType interface{}
}{
{
name: "windows paths escaped",
json: `"C:\\\\ProgramData\\\\MySQL\\\\MySQL Server 8.0\\\\Data\\\\#innodb_temp\\\\"`,
expectedResult: `C:\\ProgramData\\MySQL\\MySQL Server 8.0\\Data\\#innodb_temp\\`,
err: false,
},
{
name: "basic-string",
json: `"string"`,
Expand Down Expand Up @@ -328,6 +334,12 @@ func TestDecoderStringNull(t *testing.T) {
errType interface{}
resultIsNil bool
}{
{
name: "windows paths escaped",
json: `"C:\\\\ProgramData\\\\MySQL\\\\MySQL Server 8.0\\\\Data\\\\#innodb_temp\\\\"`,
expectedResult: `C:\\ProgramData\\MySQL\\MySQL Server 8.0\\Data\\#innodb_temp\\`,
err: false,
},
{
name: "basic-string",
json: `"string"`,
Expand Down Expand Up @@ -690,8 +702,9 @@ func TestDecoderSkipEscapedStringError2(t *testing.T) {
dec := NewDecoder(strings.NewReader(`\"`))
defer dec.Release()
err := dec.skipEscapedString()
assert.NotNil(t, err, "Err must be nil")
assert.IsType(t, InvalidJSONError(""), err, "err must be of type InvalidJSONError")
assert.Nil(t, err, "Err must be nil")
// assert.IsType(t, InvalidJSONError(""), err, "err must be of type InvalidJSONError")
assert.Equal(t, 1, dec.cursor, "dec.cursor must be 2")
}

func TestDecoderSkipEscapedStringError3(t *testing.T) {
Expand Down Expand Up @@ -727,14 +740,14 @@ func TestSkipString(t *testing.T) {
errType interface{}
}{
{
name: "escape quote err",
name: "escape quote err0",
json: `test string \\" escaped"`,
expectedResult: ``,
err: true,
errType: InvalidJSONError(""),
err: false,
// errType: InvalidJSONError(""),
},
{
name: "escape quote err",
name: "escape quote err1",
json: `test string \\\l escaped"`,
expectedResult: ``,
err: true,
Expand Down
10 changes: 3 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
module github.com/francoispqt/gojay
module github.com/armosec/gojay

go 1.12

require (
cloud.google.com/go v0.37.0 // indirect
cloud.google.com/go v0.50.0 // indirect
github.com/buger/jsonparser v0.0.0-20181115193947-bf1c66bbce23
github.com/go-errors/errors v1.0.1
github.com/golang/protobuf v1.3.1 // indirect
github.com/json-iterator/go v1.1.6
github.com/lunixbochs/vtclean v1.0.0 // indirect
github.com/mailru/easyjson v0.0.0-20190312143242-1de009706dbe
Expand All @@ -16,9 +15,6 @@ require (
github.com/stretchr/testify v1.2.2
github.com/viant/assertly v0.4.8
github.com/viant/toolbox v0.24.0
golang.org/x/crypto v0.0.0-20190313024323-a1f597ede03a // indirect
golang.org/x/net v0.0.0-20190313220215-9f648a60d977
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421 // indirect
golang.org/x/sys v0.0.0-20190316082340-a2f829d7f35f // indirect
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80
gopkg.in/yaml.v2 v2.2.2 // indirect
)
Loading