@@ -7,7 +7,6 @@ package parse
7
7
import (
8
8
"flag"
9
9
"fmt"
10
- "runtime"
11
10
"strings"
12
11
"testing"
13
12
)
@@ -87,6 +86,11 @@ var numberTests = []numberTest{
87
86
{"0xef" , true , true , true , false , 0xef , 0xef , 0xef , 0 },
88
87
}
89
88
89
+ func init () {
90
+ // Use a small depth limit for testing to avoid creating huge expressions.
91
+ maxExpressionParenDepth = 3
92
+ }
93
+
90
94
func TestNumberParse (t * testing.T ) {
91
95
for _ , test := range numberTests {
92
96
// If fmt.Sscan thinks it's complex, it's complex. We can't trust the output
@@ -330,13 +334,13 @@ var parseTests = []parseTest{
330
334
{"block definition" , `{{block "foo"}}hello{{end}}` , hasError , "" },
331
335
332
336
// Parenthesis nesting depth tests
333
- {"paren nesting normal" , "{{( ( ( ( (1) ) ) ) ) }}" , noError , "{{(((((1))) ))}}" },
334
- {"paren nesting at limit" , "{{" + buildNestedParenExpression ( getMaxParenDepth (), "1" ) + " }}" , noError , "{{" + buildNestedParenExpression ( getMaxParenDepth (), "1" ) + " }}" },
335
- {"paren nesting exceeds limit" , "{{" + buildNestedParenExpression ( getMaxParenDepth () + 1 , "1" ) + " }}" , hasError , "template: test:1: max expression depth exceeded" },
336
- {"paren nesting in pipeline" , "{{ ( ( ( ( (1) ) ) ) ) | printf }}" , noError , "{{(((((1)) ))) | printf}}" },
337
- {"paren nesting in pipeline exceeds limit" , "{{ " + buildNestedParenExpression ( getMaxParenDepth () + 1 , "1" ) + " | printf }}" , hasError , "template: test:1: max expression depth exceeded" },
338
- {"paren nesting with other constructs" , "{{if " + buildNestedParenExpression ( 5 , " true" ) + " }}YES{{end}}" , noError , "{{if " + buildNestedParenExpression ( 5 , " true" ) + " }}\" YES\" {{end}}" },
339
- {"paren nesting with other constructs exceeds limit" , "{{if " + buildNestedParenExpression ( getMaxParenDepth () + 1 , " true" ) + " }}YES{{end}}" , hasError , "template: test:1: max expression depth exceeded" },
337
+ {"paren nesting normal" , "{{ (( 1 )) }}" , noError , "{{((1 ))}}" },
338
+ {"paren nesting at limit" , "{{ ((( 1 ))) }}" , noError , "{{(((1))) }}" },
339
+ {"paren nesting exceeds limit" , "{{ (((( 1 )))) }}" , hasError , "template: test:1: max expression depth exceeded" },
340
+ {"paren nesting in pipeline" , "{{ ((( 1 ))) | printf }}" , noError , "{{(((1 ))) | printf}}" },
341
+ {"paren nesting in pipeline exceeds limit" , "{{ (((( 1 )))) | printf }}" , hasError , "template: test:1: max expression depth exceeded" },
342
+ {"paren nesting with other constructs" , "{{ if ((( true ))) }}YES{{ end }}" , noError , "{{if ((( true))) }}\" YES\" {{end}}" },
343
+ {"paren nesting with other constructs exceeds limit" , "{{ if (((( true )))) }}YES{{ end }}" , hasError , "template: test:1: max expression depth exceeded" },
340
344
}
341
345
342
346
var builtins = map [string ]any {
@@ -726,17 +730,3 @@ func BenchmarkListString(b *testing.B) {
726
730
b .Fatal ("Benchmark was not run" )
727
731
}
728
732
}
729
-
730
- // buildNestedParenExpression is a helper for testing parenthesis depth.
731
- func buildNestedParenExpression (depth int , content string ) string {
732
- return strings .Repeat ("(" , depth ) + content + strings .Repeat (")" , depth )
733
- }
734
-
735
- // getMaxParenDepth returns the appropriate parenthesis nesting depth limit
736
- // based on the current architecture.
737
- func getMaxParenDepth () int {
738
- if runtime .GOARCH == "wasm" {
739
- return maxExpressionParenDepthWasm
740
- }
741
- return maxExpressionParenDepth
742
- }
0 commit comments