diff options
Diffstat (limited to 'parse.go')
-rw-r--r-- | parse.go | 15 |
1 files changed, 14 insertions, 1 deletions
@@ -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 |