Skip to content

Commit 246e5ca

Browse files
authored
Merge pull request #906 from terencehonles/fix-multiple-subst-regexp-calls
fix: fix symbol checking and creation for ``subst`` with a regexp
2 parents 2d4365f + c7ba9c4 commit 246e5ca

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

internal/runtime/compiler/checker/checker.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type checker struct {
3535
tooDeep bool
3636
maxRecursionDepth int
3737
maxRegexLength int
38+
noRegexSymbols bool
3839
}
3940

4041
// Check performs a semantic check of the astNode, and returns a potentially
@@ -83,6 +84,12 @@ func (c *checker) VisitBefore(node ast.Node) (ast.Visitor, ast.Node) {
8384
glog.V(2).Infof("Created new scope %v in condstmt", n.Scope)
8485
return c, n
8586

87+
case *ast.BuiltinExpr:
88+
if n.Name == "subst" {
89+
c.noRegexSymbols = true
90+
}
91+
return c, n
92+
8693
case *ast.CaprefTerm:
8794
if n.Symbol == nil {
8895
sym := c.scope.Lookup(n.Name, symbol.CaprefSymbol)
@@ -821,6 +828,10 @@ func (c *checker) VisitAfter(node ast.Node) ast.Node {
821828
return n
822829
}
823830

831+
case "subst":
832+
c.noRegexSymbols = false
833+
return n
834+
824835
case "tolower":
825836
if !types.Equals(gotType.Args[0], types.String) {
826837
c.errors.Add(n.Args.(*ast.ExprList).Children[0].Pos(), fmt.Sprintf("Expecting a String for argument 1 of tolower(), not %v.", gotType.Args[0]))
@@ -875,6 +886,10 @@ func (c *checker) checkRegex(pattern string, n ast.Node) {
875886
return
876887
}
877888
if reAst, err := types.ParseRegexp(pattern); err == nil {
889+
if c.noRegexSymbols {
890+
return
891+
}
892+
878893
// We reserve the names of the capturing groups as declarations
879894
// of those symbols, so that future CAPREF tokens parsed can
880895
// retrieve their value. By recording them in the symbol table, we

internal/runtime/compiler/checker/checker_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ var checkerInvalidPrograms = []struct {
6767
[]string{"invalid regex 3:1:1-24: error parsing regexp: invalid character class range: `[:alph:]`"},
6868
},
6969

70+
{
71+
"subst invalid regex",
72+
`subst(/foo(/, "", "")
73+
`,
74+
[]string{"subst invalid regex:1:7-12: error parsing regexp: missing closing ): `foo(`"},
75+
},
76+
7077
{
7178
"duplicate declaration",
7279
"counter foo\ncounter foo\n",
@@ -275,6 +282,13 @@ m`,
275282
[]string{"regexp too long:1:1-1027: Exceeded maximum regular expression pattern length of 1024 bytes with 1025.", "\tExcessively long patterns are likely to cause compilation and runtime performance problems."},
276283
},
277284

285+
{
286+
"subst regexp too long",
287+
"subst(/" + strings.Repeat("c", 1025) + `/, "", "")
288+
`,
289+
[]string{"subst regexp too long:1:7-1033: Exceeded maximum regular expression pattern length of 1024 bytes with 1025.", "\tExcessively long patterns are likely to cause compilation and runtime performance problems."},
290+
},
291+
278292
{
279293
"strptime invalid args",
280294
`strptime("",8)
@@ -576,6 +590,11 @@ foo = subst(",", "", $1)
576590
}`},
577591
{"regexp subst", `
578592
subst(/\d+/, "d", "1234")
593+
`},
594+
{"regexp subst twice", `
595+
text value
596+
value = subst(/[a-zA-Z]+/, "a", "1234abcd")
597+
value = subst(/\d+/, "d", value)
579598
`},
580599
}
581600

0 commit comments

Comments
 (0)