Skip to content

Commit

Permalink
fix(jit): out of index when dump mismatched error
Browse files Browse the repository at this point in the history
  • Loading branch information
liuq19 committed Dec 2, 2024
1 parent e2ff8ac commit 944cbb2
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 8 deletions.
8 changes: 0 additions & 8 deletions internal/decoder/jitdec/assembler_regabi_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -1129,15 +1129,7 @@ func (self *_Assembler) unmarshal_func(t reflect.Type, fn obj.Addr, deref bool)
self.Emit("MOVQ" , _ARG_sv_n, _DI) // MOVQ sv.n, DI
self.call_go(fn) // CALL_GO ${fn}
self.Emit("TESTQ", _ET, _ET) // TESTQ ET, ET
self.Sjmp("JZ" , "_unmarshal_func_end_{n}") // JNZ _error
self.Emit("MOVQ", _I_json_MismatchTypeError, _CX) // MOVQ ET, VAR.et
self.Emit("CMPQ", _ET, _CX) // check if MismatchedError
self.Sjmp("JNE" , _LB_error)
self.Emit("MOVQ", jit.Type(t), _CX) // store current type
self.Emit("MOVQ", _CX, _VAR_et) // store current type
self.Emit("MOVQ", _VAR_ic, _IC) // recover the pos
self.Emit("XORL", _ET, _ET)
self.Link("_unmarshal_func_end_{n}")
}

/** Dynamic Decoding Routine **/
Expand Down
54 changes: 54 additions & 0 deletions issue_test/issue716_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2024 CloudWeGo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


package issue_test

import (
"fmt"
"testing"

"github.com/bytedance/sonic"
"github.com/stretchr/testify/assert"
)

type UnmFoo struct {
Name string
Age int
}

func (p *UnmFoo) UnmarshalJSON(data []byte) error {
var aux struct {
Name string `json:"name"`
Age int `json:"age"`
}

if err := sonic.Unmarshal(data, &aux); err != nil {
return err
}

p.Name = aux.Name
p.Age = aux.Age
return nil
}

func TestIssue716(t *testing.T) {
jsonData := `{"name": "Alice", "age": "30"}`
var obj UnmFoo
err := sonic.Unmarshal([]byte(jsonData), &obj)
assert.Error(t, err)
if err != nil {
fmt.Println("Error:", err)
}
}

0 comments on commit 944cbb2

Please sign in to comment.