Skip to content

Commit

Permalink
fix(aarch64): bug when unmarshal into prefilled interface slice (#751)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuq19 authored Feb 24, 2025
1 parent 28755a2 commit e30ac4f
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 1 deletion.
18 changes: 18 additions & 0 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2162,6 +2162,8 @@ func TestSkipArrayObjects(t *testing.T) {
// slices, and arrays.
// Issues 4900 and 8837, among others.
func TestPrefilled(t *testing.T) {
var one int = 1
var two int = 2
// Values here change, cannot reuse table across runs.
var prefillTests = []struct {
in string
Expand All @@ -2183,6 +2185,11 @@ func TestPrefilled(t *testing.T) {
ptr: &[]int{1},
out: &[]int{2},
},
{
in: `[2]`,
ptr: &[]int{1, 3},
out: &[]int{2},
},
{
in: `[2, 3]`,
ptr: &[]int{1},
Expand All @@ -2198,6 +2205,17 @@ func TestPrefilled(t *testing.T) {
ptr: &[...]int{1, 2},
out: &[...]int{3, 0},
},
{
in: `[2]`,
ptr: &[]interface{}{&one, 2.0},
out: &[]interface{}{&two},
},
// TODO(rfc): different with encoding/json
// {
// in: `{"a": 2}`,
// ptr: &map[string]interface{}{"a": &one, "b": int(2)},
// out: &map[string]interface{}{"a": 2.0, "b": int(2)},
// },
}

for _, tt := range prefillTests {
Expand Down
13 changes: 12 additions & 1 deletion internal/decoder/optdec/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,18 @@ func (d *sliceEfaceDecoder) FromDom(vp unsafe.Pointer, node Node, ctx *context)
return nil
}

return node.AsSliceEface(ctx, vp)
/* if slice is empty, just call `AsSliceEface` */
if ((*rt.GoSlice)(vp)).Len == 0 {
return node.AsSliceEface(ctx, vp)
}

decoder := sliceDecoder{
elemType: rt.AnyType,
elemDec: &efaceDecoder{},
typ: rt.SliceEfaceType.Pack(),
}

return decoder.FromDom(vp, node, ctx)
}

type sliceI32Decoder struct {
Expand Down
31 changes: 31 additions & 0 deletions issue_test/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2025 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 (
"testing"

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

func assertUnmarshal(t *testing.T, api sonic.API, input []byte, newfn func() interface{}) {
sv, jv := newfn(), newfn()
serr := api.Unmarshal(input, sv)
jerr := json.Unmarshal(input, jv)
assert.Equal(t, jv, sv)
assert.Equal(t, serr == nil, jerr == nil)
}
31 changes: 31 additions & 0 deletions issue_test/issue750_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2025 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 (
"testing"

"github.com/bytedance/sonic"
)

func genSlice() interface{} {
a := []string{}
return &[]interface{}{&a}
}

func TestSlicePointer_Issue750(t *testing.T) {
assertUnmarshal(t, sonic.ConfigStd, []byte(`[["one","2"]]`), genSlice)

}

0 comments on commit e30ac4f

Please sign in to comment.