Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-5996

Regex boundaries should not be used in a way that can never be matched

    XMLWordPrintable

    Details

    • Type: Bug Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Remove or replace this boundary that will never match because it appears after/before mandatory input.
    • Highlighting:
      Hide

      The boundary or boundaries that can never match and/or the optional parts of the regex that can never match because of a boundary.

      Show
      The boundary or boundaries that can never match and/or the optional parts of the regex that can never match because of a boundary.
    • Default Severity:
      Critical
    • Impact:
      High
    • Likelihood:
      Low
    • Default Quality Profiles:
      Sonar way
    • Targeted languages:
      PHP
    • Covered Languages:
      Java
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      10min
    • Analysis Scope:
      Main Sources

      Description

      In regular expressions the boundaries ^ and \A can only match at the beginning of the input (or, in case of ^ in combination with the MULTILINE flag, the beginning of the line) and $, \Z and \z only at the end.

      These patterns can be misused, by accidentally switching ^ and $ for example, to create a pattern that can never match.

      Noncompliant Code Example

      // This can never match because $ and ^ have been switched around
      Pattern.compile("$[a-z]+^"); // Noncompliant
      

      Compliant Solution

      Pattern.compile("^[a-z]+$");
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              sebastian.hungerecker Sebastian Hungerecker
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated: