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

fix(aarch64): skip number for object value #719

Merged
merged 1 commit into from
Dec 3, 2024
Merged
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
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
Loading