Skip to content

Commit d1af763

Browse files
authored
Merge pull request #425 from liggitt/default-max-depth
Revert "Merge pull request #418 from bbrks/configurable_maxDepth"
2 parents 44a7e73 + 7c9f8c2 commit d1af763

File tree

3 files changed

+5
-52
lines changed

3 files changed

+5
-52
lines changed

api_tests/config_test.go

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ package test
22

33
import (
44
"encoding/json"
5-
"fmt"
6-
"strings"
75
"testing"
86

9-
jsoniter "github.com/json-iterator/go"
7+
"github.com/json-iterator/go"
108
"github.com/stretchr/testify/require"
119
)
1210

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

29-
func Test_max_depth(t *testing.T) {
30-
deepJSON := func(depth int) []byte {
31-
return []byte(strings.Repeat(`[`, depth) + strings.Repeat(`]`, depth))
32-
}
33-
34-
tests := []struct {
35-
jsonDepth int
36-
cfgMaxDepth int
37-
expectedErr string
38-
}{
39-
// Test the default depth
40-
{jsonDepth: 10000, cfgMaxDepth: 0},
41-
{jsonDepth: 10001, cfgMaxDepth: 0, expectedErr: "max depth"},
42-
// Test max depth logic
43-
{jsonDepth: 5, cfgMaxDepth: 6},
44-
{jsonDepth: 5, cfgMaxDepth: 5},
45-
{jsonDepth: 5, cfgMaxDepth: 4, expectedErr: "max depth"},
46-
// Try a large depth without a limit
47-
{jsonDepth: 128000, cfgMaxDepth: -1},
48-
}
49-
50-
for _, test := range tests {
51-
t.Run(fmt.Sprintf("jsonDepth:%v_cfgMaxDepth:%v", test.jsonDepth, test.cfgMaxDepth), func(t *testing.T) {
52-
should := require.New(t)
53-
cfg := jsoniter.Config{MaxDepth: test.cfgMaxDepth}.Froze()
54-
55-
var val interface{}
56-
err := cfg.Unmarshal(deepJSON(test.jsonDepth), &val)
57-
if test.expectedErr != "" {
58-
should.Error(err)
59-
should.Contains(err.Error(), test.expectedErr)
60-
} else {
61-
should.NoError(err)
62-
}
63-
})
64-
}
65-
}
66-
6727
func Test_customize_tag_key(t *testing.T) {
6828

6929
type TestObject struct {

config.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ import (
1111
"github.com/modern-go/reflect2"
1212
)
1313

14-
// limit maximum depth of nesting, as allowed by https://tools.ietf.org/html/rfc7159#section-9
15-
const defaultMaxDepth = 10000
16-
1714
// Config customize how the API should behave.
1815
// The API is created from Config by Froze.
1916
type Config struct {
@@ -28,7 +25,6 @@ type Config struct {
2825
ValidateJsonRawMessage bool
2926
ObjectFieldMustBeSimpleString bool
3027
CaseSensitive bool
31-
MaxDepth int
3228
}
3329

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

6661
// ConfigFastest marshals float with only 6 digits precision
@@ -85,7 +80,6 @@ type frozenConfig struct {
8580
streamPool *sync.Pool
8681
iteratorPool *sync.Pool
8782
caseSensitive bool
88-
maxDepth int
8983
}
9084

9185
func (cfg *frozenConfig) initCache() {
@@ -133,17 +127,13 @@ func addFrozenConfigToCache(cfg Config, frozenConfig *frozenConfig) {
133127

134128
// Froze forge API from config
135129
func (cfg Config) Froze() API {
136-
if cfg.MaxDepth == 0 {
137-
cfg.MaxDepth = defaultMaxDepth
138-
}
139130
api := &frozenConfig{
140131
sortMapKeys: cfg.SortMapKeys,
141132
indentionStep: cfg.IndentionStep,
142133
objectFieldMustBeSimpleString: cfg.ObjectFieldMustBeSimpleString,
143134
onlyTaggedField: cfg.OnlyTaggedField,
144135
disallowUnknownFields: cfg.DisallowUnknownFields,
145136
caseSensitive: cfg.CaseSensitive,
146-
maxDepth: cfg.MaxDepth,
147137
}
148138
api.streamPool = &sync.Pool{
149139
New: func() interface{} {

iter.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,12 @@ func (iter *Iterator) Read() interface{} {
327327
}
328328
}
329329

330+
// limit maximum depth of nesting, as allowed by https://tools.ietf.org/html/rfc7159#section-9
331+
const maxDepth = 10000
332+
330333
func (iter *Iterator) incrementDepth() (success bool) {
331334
iter.depth++
332-
if iter.depth <= iter.cfg.maxDepth || iter.cfg.maxDepth < 0 {
335+
if iter.depth <= maxDepth {
333336
return true
334337
}
335338
iter.ReportError("incrementDepth", "exceeded max depth")

0 commit comments

Comments
 (0)