Fix corrupt json bug in jsn package

This commit is contained in:
Vikram Rangnekar 2020-02-24 01:59:50 +05:30
parent 90694f8803
commit 51e105699e
8 changed files with 77 additions and 26 deletions

View File

@ -27,9 +27,14 @@ func Filter(w *bytes.Buffer, b []byte, keys []string) error {
var k []byte
state := expectKey
instr := false
for i := 0; i < len(b); i++ {
if state == expectObjClose || state == expectListClose {
if b[i-1] != '\\' && b[i] == '"' {
instr = !instr
}
if !instr {
switch b[i] {
case '{', '[':
d++
@ -37,6 +42,7 @@ func Filter(w *bytes.Buffer, b []byte, keys []string) error {
d--
}
}
}
if state == expectKey {
switch b[i] {

View File

@ -51,8 +51,14 @@ func Get(b []byte, keys [][]byte) []Field {
state := expectKey
n := 0
instr := false
for i := 0; i < len(b); i++ {
if state == expectObjClose || state == expectListClose {
if b[i-1] != '\\' && b[i] == '"' {
instr = !instr
}
if !instr {
switch b[i] {
case '{', '[':
d++
@ -60,6 +66,7 @@ func Get(b []byte, keys [][]byte) []Field {
d--
}
}
}
switch {
case state == expectKey && b[i] == '"':

View File

@ -2,6 +2,7 @@ package jsn
import (
"bytes"
"io/ioutil"
"testing"
)
@ -161,6 +162,8 @@ var (
input6 = `
{"users" : [{"id" : 1, "email" : "vicram@gmail.com", "slug" : "vikram-rangnekar", "threads" : [], "threads_cursor" : null}, {"id" : 3, "email" : "marareilly@lang.name", "slug" : "raymundo-corwin", "threads" : [{"id" : 9, "title" : "Et alias et aut porro praesentium nam in voluptatem reiciendis quisquam perspiciatis inventore eos quia et et enim qui amet."}, {"id" : 25, "title" : "Ipsam quam nemo culpa tempore amet optio sit sed eligendi autem consequatur quaerat rem velit quibusdam quibusdam optio a voluptatem."}], "threads_cursor" : 25}], "users_cursor" : 3}`
input7, _ = ioutil.ReadFile("test.json")
)
func TestGet(t *testing.T) {
@ -256,6 +259,15 @@ func TestGet2(t *testing.T) {
}
}
func TestGet3(t *testing.T) {
values := Get(input7, [][]byte{[]byte("data")})
v := values[0].Value
if !bytes.Equal(v[len(v)-11:], []byte(`Rangnekar"}`)) {
t.Fatal("corrupt ending")
}
}
func TestValue(t *testing.T) {
v1 := []byte("12345")
if !bytes.Equal(Value(v1), v1) {

View File

@ -10,10 +10,14 @@ func Keys(b []byte) [][]byte {
st := NewStack()
ae := 0
instr := false
for i := 0; i < len(b); i++ {
if state == expectObjClose || state == expectListClose {
if b[i-1] != '\\' && b[i] == '"' {
instr = !instr
}
if !instr {
switch b[i] {
case '{', '[':
d++
@ -21,6 +25,7 @@ func Keys(b []byte) [][]byte {
d--
}
}
}
si := st.Peek()

View File

@ -32,6 +32,8 @@ func Replace(w *bytes.Buffer, b []byte, from, to []Field) error {
state := expectKey
ws, we := -1, len(b)
instr := false
for i := 0; i < len(b); i++ {
// skip any left padding whitespace
if ws == -1 && (b[i] == '{' || b[i] == '[') {
@ -39,6 +41,10 @@ func Replace(w *bytes.Buffer, b []byte, from, to []Field) error {
}
if state == expectObjClose || state == expectListClose {
if b[i-1] != '\\' && b[i] == '"' {
instr = !instr
}
if !instr {
switch b[i] {
case '{', '[':
d++
@ -46,6 +52,7 @@ func Replace(w *bytes.Buffer, b []byte, from, to []Field) error {
d--
}
}
}
switch {
case state == expectKey && b[i] == '"':

View File

@ -11,9 +11,14 @@ func Strip(b []byte, path [][]byte) []byte {
pi := 0
pm := false
state := expectKey
instr := false
for i := 0; i < len(b); i++ {
if state == expectObjClose || state == expectListClose {
if b[i-1] != '\\' && b[i] == '"' {
instr = !instr
}
if !instr {
switch b[i] {
case '{', '[':
d++
@ -21,6 +26,7 @@ func Strip(b []byte, path [][]byte) []byte {
d--
}
}
}
switch {
case state == expectKey && b[i] == '"':

1
jsn/test.json Normal file

File diff suppressed because one or more lines are too long

View File

@ -39,11 +39,16 @@ func argMap(ctx context.Context, vars []byte) func(w io.Writer, tag string) (int
}
v := fields[0].Value
// Open and close quotes
if len(v) >= 2 && v[0] == '"' && v[len(v)-1] == '"' {
fields[0].Value = v[1 : len(v)-1]
}
if tag == "cursor" {
if bytes.EqualFold(v, []byte("null")) {
return io.WriteString(w, ``)
}
v1, err := decrypt(string(fields[0].Value))
if err != nil {
return 0, err
@ -52,6 +57,8 @@ func argMap(ctx context.Context, vars []byte) func(w io.Writer, tag string) (int
return w.Write(v1)
}
fmt.Println(">>>", tag, string(v))
return w.Write(escQuote(fields[0].Value))
}
}