Skip to content

Revert "Merge pull request #418 from bbrks/configurable_maxDepth" #425

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

Merged
merged 1 commit into from
Dec 21, 2019
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
42 changes: 1 addition & 41 deletions api_tests/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package test

import (
"encoding/json"
"fmt"
"strings"
"testing"

jsoniter "github.com/json-iterator/go"
"github.com/json-iterator/go"
"github.com/stretchr/testify/require"
)

Expand All @@ -26,44 +24,6 @@ func Test_customize_float_marshal(t *testing.T) {
should.Equal("1.234568", str)
}

func Test_max_depth(t *testing.T) {
deepJSON := func(depth int) []byte {
return []byte(strings.Repeat(`[`, depth) + strings.Repeat(`]`, depth))
}

tests := []struct {
jsonDepth int
cfgMaxDepth int
expectedErr string
}{
// Test the default depth
{jsonDepth: 10000, cfgMaxDepth: 0},
{jsonDepth: 10001, cfgMaxDepth: 0, expectedErr: "max depth"},
// Test max depth logic
{jsonDepth: 5, cfgMaxDepth: 6},
{jsonDepth: 5, cfgMaxDepth: 5},
{jsonDepth: 5, cfgMaxDepth: 4, expectedErr: "max depth"},
// Try a large depth without a limit
{jsonDepth: 128000, cfgMaxDepth: -1},
}

for _, test := range tests {
t.Run(fmt.Sprintf("jsonDepth:%v_cfgMaxDepth:%v", test.jsonDepth, test.cfgMaxDepth), func(t *testing.T) {
should := require.New(t)
cfg := jsoniter.Config{MaxDepth: test.cfgMaxDepth}.Froze()

var val interface{}
err := cfg.Unmarshal(deepJSON(test.jsonDepth), &val)
if test.expectedErr != "" {
should.Error(err)
should.Contains(err.Error(), test.expectedErr)
} else {
should.NoError(err)
}
})
}
}

func Test_customize_tag_key(t *testing.T) {

type TestObject struct {
Expand Down
10 changes: 0 additions & 10 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ import (
"github.com/modern-go/reflect2"
)

// limit maximum depth of nesting, as allowed by https://tools.ietf.org/html/rfc7159#section-9
const defaultMaxDepth = 10000

// Config customize how the API should behave.
// The API is created from Config by Froze.
type Config struct {
Expand All @@ -28,7 +25,6 @@ type Config struct {
ValidateJsonRawMessage bool
ObjectFieldMustBeSimpleString bool
CaseSensitive bool
MaxDepth int
}

// API the public interface of this package.
Expand Down Expand Up @@ -60,7 +56,6 @@ var ConfigCompatibleWithStandardLibrary = Config{
EscapeHTML: true,
SortMapKeys: true,
ValidateJsonRawMessage: true,
MaxDepth: -1, // encoding/json has no max depth (stack overflow at 2581101)
}.Froze()

// ConfigFastest marshals float with only 6 digits precision
Expand All @@ -85,7 +80,6 @@ type frozenConfig struct {
streamPool *sync.Pool
iteratorPool *sync.Pool
caseSensitive bool
maxDepth int
}

func (cfg *frozenConfig) initCache() {
Expand Down Expand Up @@ -133,17 +127,13 @@ func addFrozenConfigToCache(cfg Config, frozenConfig *frozenConfig) {

// Froze forge API from config
func (cfg Config) Froze() API {
if cfg.MaxDepth == 0 {
cfg.MaxDepth = defaultMaxDepth
}
api := &frozenConfig{
sortMapKeys: cfg.SortMapKeys,
indentionStep: cfg.IndentionStep,
objectFieldMustBeSimpleString: cfg.ObjectFieldMustBeSimpleString,
onlyTaggedField: cfg.OnlyTaggedField,
disallowUnknownFields: cfg.DisallowUnknownFields,
caseSensitive: cfg.CaseSensitive,
maxDepth: cfg.MaxDepth,
}
api.streamPool = &sync.Pool{
New: func() interface{} {
Expand Down
5 changes: 4 additions & 1 deletion iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,12 @@ func (iter *Iterator) Read() interface{} {
}
}

// limit maximum depth of nesting, as allowed by https://tools.ietf.org/html/rfc7159#section-9
const maxDepth = 10000

func (iter *Iterator) incrementDepth() (success bool) {
iter.depth++
if iter.depth <= iter.cfg.maxDepth || iter.cfg.maxDepth < 0 {
if iter.depth <= maxDepth {
return true
}
iter.ReportError("incrementDepth", "exceeded max depth")
Expand Down