Skip to content

Commit f4ebd17

Browse files
committed
text/template: rename depth variables
Renames parenDepth to stackDepth and maxExpressionParenDepth to maxStackDepth to better reflect their purpose in preventing stack overflow. Also simplifies initialization logic and improves comment clarity per PR feedback. Signed-off-by: Ville Vesilehto <ville@vesilehto.fi>
1 parent bd1f53b commit f4ebd17

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

src/text/template/parse/parse.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type Tree struct {
3232
treeSet map[string]*Tree
3333
actionLine int // line of left delim starting action
3434
rangeDepth int
35-
parenDepth int // depth of nested parenthesized expressions
35+
stackDepth int // depth of nested parenthesized expressions
3636
}
3737

3838
// A mode value is a set of flags (or 0). Modes control parser behavior.
@@ -43,17 +43,14 @@ const (
4343
SkipFuncCheck // do not check that functions are defined
4444
)
4545

46-
// maxExpressionParenDepth is the maximum depth permitted for nested
46+
// maxStackDepth is the maximum depth permitted for nested
4747
// parenthesized expressions.
48-
var maxExpressionParenDepth int
48+
var maxStackDepth = 10000
4949

50-
// init sets up the maximum expression parenthesis depth based on the architecture.
51-
// WebAssembly has a smaller stack size and is more prone to stack overflow.
50+
// init reduces maxStackDepth for WebAssembly due to its smaller stack size.
5251
func init() {
5352
if runtime.GOARCH == "wasm" {
54-
maxExpressionParenDepth = 1000
55-
} else {
56-
maxExpressionParenDepth = 10000
53+
maxStackDepth = 1000
5754
}
5855
}
5956

@@ -238,7 +235,7 @@ func (t *Tree) startParse(funcs []map[string]any, lex *lexer, treeSet map[string
238235
t.vars = []string{"$"}
239236
t.funcs = funcs
240237
t.treeSet = treeSet
241-
t.parenDepth = 0
238+
t.stackDepth = 0
242239
lex.options = lexOptions{
243240
emitComment: t.Mode&ParseComments != 0,
244241
breakOK: !t.hasFunction("break"),
@@ -803,11 +800,11 @@ func (t *Tree) term() Node {
803800
}
804801
return number
805802
case itemLeftParen:
806-
if t.parenDepth >= maxExpressionParenDepth {
803+
if t.stackDepth >= maxStackDepth {
807804
t.errorf("max expression depth exceeded")
808805
}
809-
t.parenDepth++
810-
defer func() { t.parenDepth-- }()
806+
t.stackDepth++
807+
defer func() { t.stackDepth-- }()
811808
return t.pipeline("parenthesized pipeline", itemRightParen)
812809
case itemString, itemRawString:
813810
s, err := strconv.Unquote(token.val)

src/text/template/parse/parse_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ var numberTests = []numberTest{
8787
}
8888

8989
func init() {
90-
// Use a small depth limit for testing to avoid creating huge expressions.
91-
maxExpressionParenDepth = 3
90+
// Use a small stack limit for testing to avoid creating huge expressions.
91+
maxStackDepth = 3
9292
}
9393

9494
func TestNumberParse(t *testing.T) {
@@ -333,7 +333,7 @@ var parseTests = []parseTest{
333333
// Missing pipeline in block
334334
{"block definition", `{{block "foo"}}hello{{end}}`, hasError, ""},
335335

336-
// Parenthesis nesting depth tests
336+
// Expression nested depth tests.
337337
{"paren nesting normal", "{{ (( 1 )) }}", noError, "{{((1))}}"},
338338
{"paren nesting at limit", "{{ ((( 1 ))) }}", noError, "{{(((1)))}}"},
339339
{"paren nesting exceeds limit", "{{ (((( 1 )))) }}", hasError, "template: test:1: max expression depth exceeded"},

0 commit comments

Comments
 (0)