From 3976560adbf1b01f8abd6952fdf7fa71661f20a0 Mon Sep 17 00:00:00 2001 From: "duanyi.aster" Date: Thu, 1 Feb 2024 19:29:59 +0800 Subject: [PATCH] fix: check Stackoverflow wrongly --- generic_test/benchmark_test.go | 23 +++++++++++------------ internal/encoder/vars/stack.go | 5 +++-- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/generic_test/benchmark_test.go b/generic_test/benchmark_test.go index 78ce56ebf..bd6842c57 100644 --- a/generic_test/benchmark_test.go +++ b/generic_test/benchmark_test.go @@ -24,7 +24,7 @@ import ( `reflect` `fmt` `github.com/bytedance/sonic` - dev `github.com/bytedance/sonic/dev` + // dev `github.com/bytedance/sonic/dev` `encoding/json` gojson `github.com/goccy/go-json` jsoniter `github.com/json-iterator/go` @@ -46,19 +46,18 @@ var jsonTotalLibs = []jsonLibEntry { {"Std", json.Marshal, json.Unmarshal}, {"StdV2", jsonv2.Marshal, jsonv2.Unmarshal}, {"Sonic", sonic.Marshal, sonic.Unmarshal}, - {"SonicDev", dev.Marshal, dev.Unmarshal}, + // {"SonicDev", dev.Marshal, dev.Unmarshal}, {"SonicStd", sonic.ConfigStd.Marshal, sonic.ConfigStd.Unmarshal}, {"GoJson", gojson.Marshal, gojson.Unmarshal}, {"JsonIter", jsoniter.Marshal, jsoniter.Unmarshal}, {"JsonIterStd", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal, jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal}, } - -var jsonLibs = []jsonLibEntry { - {"Std", json.Marshal, json.Unmarshal}, - {"Sonic", sonic.Marshal, sonic.Unmarshal}, - {"SonicDev", dev.Marshal, dev.Unmarshal}, -} +// var jsonTotalLibs = []jsonLibEntry { +// {"Std", json.Marshal, json.Unmarshal}, +// {"Sonic", sonic.Marshal, sonic.Unmarshal}, +// {"SonicDev", dev.Marshal, dev.Unmarshal}, +// } func BenchmarkUnmarshalConcrete(b *testing.B) { runUnmarshalC(b) @@ -78,7 +77,7 @@ func BenchmarkMarshalInterface(b *testing.B) { func runUnmarshalC(b *testing.B) { for _, tt := range jsonTestdata() { - for _, lib := range jsonLibs { + for _, lib := range jsonTotalLibs { var val any = tt.new() pretouch := func() { _ = lib.unmarshal(tt.data, val) @@ -123,7 +122,7 @@ func runUnmarshalC(b *testing.B) { func runUnmarshalI(b *testing.B) { for _, tt := range jsonTestdata() { - for _, lib := range jsonLibs { + for _, lib := range jsonTotalLibs { var val any = tt.newI() pretouch := func() { _ = lib.unmarshal(tt.data, val) @@ -168,7 +167,7 @@ func runUnmarshalI(b *testing.B) { func runMarshalC(b *testing.B) { for _, tt := range jsonTestdata() { - for _, lib := range jsonLibs { + for _, lib := range jsonTotalLibs { pretouch := func() { _, _ = lib.marshal(tt.val) } @@ -223,7 +222,7 @@ func runMarshalC(b *testing.B) { func runMarshalI(b *testing.B) { for _, tt := range jsonTestdata() { - for _, lib := range jsonLibs { + for _, lib := range jsonTotalLibs { pretouch := func() { _, _ = lib.marshal(tt.valI) } diff --git a/internal/encoder/vars/stack.go b/internal/encoder/vars/stack.go index e3b39bfe4..0bcebc08b 100644 --- a/internal/encoder/vars/stack.go +++ b/internal/encoder/vars/stack.go @@ -68,7 +68,6 @@ func ResetStack(p *Stack) { rt.MemclrNoHeapPointers(unsafe.Pointer(p), StackSize) } - func (s *Stack) Top() *State { return (*State)(unsafe.Add(unsafe.Pointer(&s.sb[0]), s.sp)) } @@ -77,8 +76,10 @@ func (s *Stack) Cur() *State { return (*State)(unsafe.Add(unsafe.Pointer(&s.sb[0]), s.sp - uintptr(StateSize))) } +const _MaxStackSP = uintptr(MaxStack * StateSize) + func (s *Stack) Push(v State) bool { - if uintptr(s.sp) > uintptr(unsafe.Pointer(&s.sb[MaxStack-1])) { + if uintptr(s.sp) >= _MaxStackSP { return false } st := s.Top()