feat: add OffsetDateTime, nesting limits and uniform Marshaler dispatch
Test / test (push) Successful in 2m18s
Test / test (push) Successful in 2m18s
Assisted-by: DeepSeek V4.1 Flash
This commit is contained in:
@@ -30,6 +30,12 @@ type parser struct {
|
||||
line int
|
||||
ctx context.Context
|
||||
|
||||
// maxDepth and depth bound the nesting the recursive descent may follow:
|
||||
// arrays and inline tables nest through parseValue, and without a limit a
|
||||
// hostile document would exhaust the stack.
|
||||
maxDepth int
|
||||
depth int
|
||||
|
||||
root map[string]any
|
||||
current map[string]any
|
||||
headers map[string]bool
|
||||
@@ -40,6 +46,23 @@ type parser struct {
|
||||
currentPath []string
|
||||
}
|
||||
|
||||
// maxNestingDepth bounds how deeply arrays and inline tables may nest when no
|
||||
// limit is set. It matches the default encoding/json uses for the same reason,
|
||||
// and sits far above any document a person writes.
|
||||
const maxNestingDepth = 10000
|
||||
|
||||
// enterNesting counts one level of array or inline-table nesting and reports a
|
||||
// document that nests deeper than the limit allows.
|
||||
func (p *parser) enterNesting() error {
|
||||
p.depth++
|
||||
if p.depth > p.maxDepth {
|
||||
return p.errf("nesting exceeds the limit of %d", p.maxDepth)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *parser) leaveNesting() { p.depth-- }
|
||||
|
||||
func (p *parser) parse() (map[string]any, error) {
|
||||
p.root = map[string]any{}
|
||||
p.current = p.root
|
||||
@@ -676,6 +699,10 @@ func (p *parser) readUnicode(n int) (rune, error) {
|
||||
// --- arrays and inline tables ---------------------------------------------
|
||||
|
||||
func (p *parser) parseArray() (any, error) {
|
||||
if err := p.enterNesting(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer p.leaveNesting()
|
||||
p.pos++ // '['
|
||||
arr := []any{}
|
||||
for {
|
||||
@@ -713,6 +740,10 @@ func (p *parser) parseArray() (any, error) {
|
||||
}
|
||||
|
||||
func (p *parser) parseInlineTable() (any, error) {
|
||||
if err := p.enterNesting(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer p.leaveNesting()
|
||||
p.pos++ // '{'
|
||||
tbl := map[string]any{}
|
||||
assigned := map[string]bool{}
|
||||
|
||||
Reference in New Issue
Block a user