feat: add OffsetDateTime, nesting limits and uniform Marshaler dispatch
Test / test (push) Successful in 2m18s

Assisted-by: DeepSeek V4.1 Flash
This commit is contained in:
2026-09-19 19:36:24 +02:00
parent b695b69768
commit a6e3e3fe31
12 changed files with 631 additions and 117 deletions
+31
View File
@@ -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{}