Skip to content

Commit

Permalink
fix:(encoder) insufficient buffer size check for OP_i32
Browse files Browse the repository at this point in the history
  • Loading branch information
AsterDY committed Jul 28, 2022
1 parent 2eae594 commit 00942b0
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 deletions.
4 changes: 2 additions & 2 deletions encoder/assembler_amd64_go116.go
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ func (self *_Assembler) _asm_OP_i16(_ *_Instr) {
}

func (self *_Assembler) _asm_OP_i32(_ *_Instr) {
self.store_int(11, _F_i64toa, "MOVLQSX")
self.store_int(17, _F_i64toa, "MOVLQSX")
}

func (self *_Assembler) _asm_OP_i64(_ *_Instr) {
Expand All @@ -835,7 +835,7 @@ func (self *_Assembler) _asm_OP_u16(_ *_Instr) {
}

func (self *_Assembler) _asm_OP_u32(_ *_Instr) {
self.store_int(10, _F_u64toa, "MOVLQZX")
self.store_int(16, _F_u64toa, "MOVLQZX")
}

func (self *_Assembler) _asm_OP_u64(_ *_Instr) {
Expand Down
4 changes: 2 additions & 2 deletions encoder/assembler_amd64_go117.go
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ func (self *_Assembler) _asm_OP_i16(_ *_Instr) {
}

func (self *_Assembler) _asm_OP_i32(_ *_Instr) {
self.store_int(11, _F_i64toa, "MOVLQSX")
self.store_int(17, _F_i64toa, "MOVLQSX")
}

func (self *_Assembler) _asm_OP_i64(_ *_Instr) {
Expand All @@ -848,7 +848,7 @@ func (self *_Assembler) _asm_OP_u16(_ *_Instr) {
}

func (self *_Assembler) _asm_OP_u32(_ *_Instr) {
self.store_int(10, _F_u64toa, "MOVLQZX")
self.store_int(16, _F_u64toa, "MOVLQZX")
}

func (self *_Assembler) _asm_OP_u64(_ *_Instr) {
Expand Down
42 changes: 31 additions & 11 deletions encoder/assembler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,40 @@
package encoder

import (
"encoding/hex"
"encoding/json"
"math"
"reflect"
"runtime"
"strings"
"testing"
"unsafe"
`encoding/hex`
`encoding/json`
`math`
`reflect`
`runtime`
`strings`
`testing`
`unsafe`

"github.com/bytedance/sonic/internal/rt"
"github.com/davecgh/go-spew/spew"
"github.com/stretchr/testify/assert"
`github.com/bytedance/sonic/internal/rt`
`github.com/davecgh/go-spew/spew`
`github.com/stretchr/testify/assert`
)

func TestEncoderMemoryCorruption(t *testing.T) {
println("TestEncoderMemoryCorruption")
var m = map[string]interface{}{
"1": map[string]interface{} {
`"`+strings.Repeat("a", _MaxBuffer - 38)+`"`: "b",
"1": map[string]int32{
"b": 1658219785,
},
},
}
out, err := Encode(m, SortMapKeys)
if err != nil {
t.Fatal(err)
}
println(len(out))
if err := json.Unmarshal(out, &m); err != nil {
t.Fatal(err)
}
}

func TestAssembler_CompileAndLoad(t *testing.T) {
p, err := newCompiler().compile(reflect.TypeOf((*bool)(nil)))
assert.Nil(t, err)
Expand Down
2 changes: 2 additions & 0 deletions encoder/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ func Encode(val interface{}, opts Options) ([]byte, error) {
// a new one.
func EncodeInto(buf *[]byte, val interface{}, opts Options) error {
stk := newStack()
println("buf:", uintptr(*(*unsafe.Pointer)(unsafe.Pointer(buf))))
println("stk:", uintptr(unsafe.Pointer(stk)))
efv := rt.UnpackEface(val)
err := encodeTypedPointer(buf, efv.Type, &efv.Value, stk, uint64(opts))

Expand Down

0 comments on commit 00942b0

Please sign in to comment.