summaryrefslogtreecommitdiff
path: root/parse.go
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@datawire.io>2022-08-15 19:49:46 -0600
committerLuke Shumaker <lukeshu@datawire.io>2022-08-16 00:05:35 -0600
commit6476b9ae7019bedd9324786ff47bc25693e01b60 (patch)
tree3d793bbd40327d20ba7021459862c895cddb5fb2 /parse.go
parent54bbd1e59317a6e9658eb8098657078cc8e81979 (diff)
Push nesting-depth checks down in to the parser [ci-skip]
Diffstat (limited to 'parse.go')
-rw-r--r--parse.go15
1 files changed, 14 insertions, 1 deletions
diff --git a/parse.go b/parse.go
index 23df5bc..fb72280 100644
--- a/parse.go
+++ b/parse.go
@@ -204,6 +204,11 @@ func (t RuneType) IsNumber() bool {
}
type Parser struct {
+ // Setting MaxError to a value greater than 0 causes
+ // HandleRune to return ErrParserExceededMaxDepth if
+ // objects/arrays become nested more deeply than this.
+ MaxDepth int
+
initialized bool
err error
@@ -286,7 +291,9 @@ func (par *Parser) stackString() string {
// Reset all Parser state.
func (par *Parser) Reset() {
- *par = Parser{}
+ *par = Parser{
+ MaxDepth: par.MaxDepth,
+ }
}
// HandleEOF feeds EOF to the Parser. The returned RuneType is either
@@ -370,8 +377,14 @@ func (par *Parser) HandleRune(c rune) (RuneType, error) {
case 0x0020, 0x000A, 0x000D, 0x0009:
return RuneTypeSpace, nil
case '{':
+ if par.MaxDepth > 0 && len(par.stack) > par.MaxDepth {
+ return RuneTypeError, ErrParserExceededMaxDepth
+ }
return par.replaceState(RuneTypeObjectBeg), nil
case '[':
+ if par.MaxDepth > 0 && len(par.stack) > par.MaxDepth {
+ return RuneTypeError, ErrParserExceededMaxDepth
+ }
return par.replaceState(RuneTypeArrayBeg), nil
case '"':
return par.replaceState(RuneTypeStringBeg), nil