Skip to content

Commit

Permalink
fix(aarch64): skip number for object value
Browse files Browse the repository at this point in the history
  • Loading branch information
liuq19 committed Dec 3, 2024
1 parent 2b7839b commit d07d3f3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
13 changes: 13 additions & 0 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,8 @@ var unmarshalTests = []unmarshalTest{
ptr: new(map[string]json.Number),
err: fmt.Errorf("json: invalid number literal, trying to unmarshal %q into Number", `"invalid"`),
},

// UTF-8 and string validation tests
{in: `\u`, ptr: new(interface{}), err: fmt.Errorf("json: invald char"), validateString: true},
{in: `\u`, ptr: new(string), err: fmt.Errorf("json: invald char"), validateString: true},

Expand All @@ -1020,6 +1022,17 @@ var unmarshalTests = []unmarshalTest{
{in: "\"\x00\"", ptr: new(string), out: "\x00", validateString: false},
{in: "\"\xff\"", ptr: new(interface{}), out: interface{}("\xff"), validateString: false},
{in: "\"\xff\"", ptr: new(string), out: "\xff", validateString: false},

// cases found by fuzz
{
in: `{"H":{"A": {}}}`,
ptr: new(struct {
F0 struct {
F1 json.Number "json:\"a,omitempty\""
} "json:\"H,\""
}),
err: fmt.Errorf("Mismatch type json.Number with value object.."),
},
}

func trim(b []byte) []byte {
Expand Down
9 changes: 7 additions & 2 deletions internal/decoder/optdec/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)


func SkipNumberFast(json string, start int) (int, error) {
func SkipNumberFast(json string, start int) (int, bool) {
// find the number ending, we pasred in native, it alway valid
pos := start
for pos < len(json) && json[pos] != ']' && json[pos] != '}' && json[pos] != ',' {
Expand All @@ -20,7 +20,12 @@ func SkipNumberFast(json string, start int) (int, error) {
break
}
}
return pos, nil

// if not found number, return false
if pos == start {
return pos, false
}
return pos, true
}


Expand Down
8 changes: 4 additions & 4 deletions internal/decoder/optdec/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,9 +401,9 @@ func (val Node) ParseNumber(ctx *Context) (json.Number, bool) {
return json.Number(""), true
}

end, err := SkipNumberFast(s, 0)
end, ok := SkipNumberFast(s, 0)
// has error or trailing chars
if err != nil || end != len(s) {
if !ok || end != len(s) {
return json.Number(""), false
}
return json.Number(s), true
Expand Down Expand Up @@ -531,8 +531,8 @@ func (val Node) NonstrAsNumber(ctx *Context) (json.Number, bool) {
}

start := val.Position()
end, err := SkipNumberFast(ctx.Parser.Json, start)
if err != nil {
end, ok := SkipNumberFast(ctx.Parser.Json, start)
if !ok {
return "", false
}
return json.Number(ctx.Parser.Json[start:end]), true
Expand Down
4 changes: 2 additions & 2 deletions internal/decoder/optdec/stringopts.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,9 @@ func (d *numberStringDecoder) FromDom(vp unsafe.Pointer, node Node, ctx *context
return error_mismatch(node, ctx, jsonNumberType)
}

end, err := SkipNumberFast(s, 0)
end, ok := SkipNumberFast(s, 0)
// has error or trailing chars
if err != nil || end != len(s) {
if !ok || end != len(s) {
return error_mismatch(node, ctx, jsonNumberType)
}

Expand Down

0 comments on commit d07d3f3

Please sign in to comment.