(e *Env, _ ValidatorConfig, a *ast.AST, iss *Issues)
| 534 | } |
| 535 | |
| 536 | func (v regexProgramSizeLimitValidator) Validate(e *Env, _ ValidatorConfig, a *ast.AST, iss *Issues) { |
| 537 | if v.limit <= 0 { |
| 538 | return |
| 539 | } |
| 540 | root := ast.NavigateAST(a) |
| 541 | callExprs := ast.MatchDescendants(root, ast.KindMatcher(ast.CallKind)) |
| 542 | for _, call := range callExprs { |
| 543 | c := call.AsCall() |
| 544 | fn := c.FunctionName() |
| 545 | if !isRegexFunctionName(fn) { |
| 546 | continue |
| 547 | } |
| 548 | args := c.Args() |
| 549 | var regexArgIndex int |
| 550 | if (fn == overloads.Matches || fn == "matches") && c.Target() != nil { |
| 551 | regexArgIndex = 0 |
| 552 | } else { |
| 553 | regexArgIndex = 1 |
| 554 | } |
| 555 | if len(args) <= regexArgIndex { |
| 556 | continue |
| 557 | } |
| 558 | arg := args[regexArgIndex] |
| 559 | if arg.Kind() != ast.LiteralKind { |
| 560 | continue |
| 561 | } |
| 562 | pattern, ok := arg.AsLiteral().Value().(string) |
| 563 | if !ok { |
| 564 | continue |
| 565 | } |
| 566 | sz, err := types.RegexProgramSize(pattern) |
| 567 | if err != nil { |
| 568 | // Invalid regex literals are handled in a different validator. |
| 569 | continue |
| 570 | } |
| 571 | if sz > v.limit { |
| 572 | iss.ReportErrorAtID(arg.ID(), "regex program size %d exceeds limit of %d", sz, v.limit) |
| 573 | } |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | func isEmptyRangeComprehension(e ast.NavigableExpr) bool { |
| 578 | if e.Kind() != ast.ComprehensionKind { |
nothing calls this directly
no test coverage detected