Uploaded image for project: 'SonarJava'
  1. SonarJava
  2. SONARJAVA-3550

Rule S5994: Regex patterns following a possessive quantifier should not always fail

    Details

    • Type: New Feature
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 6.10
    • Component/s: Rules
    • Labels:

      Description

      Given an expression 'r' containing a possessive qualifier, for example: "[ab]*+b"
      Given 'r₁' the repeated sub-expression with a possessive qualifier, in the above example "[ab]"
      Given 'r₂' the continuation after the repetition, in the above example "b"

      Then the regular expression will always fail if every possible match of 'r₂' has a prefix that can be matched by 'r₁'.

      Test cases:

      "x*+x" // Noncompliant
      "x*+xy" // Noncompliant
      "(xy)*+xy" // Noncompliant
      "(xy)*+xyz" // Noncompliant
      "(xy)*+x" // Compliant
      "(xy|ab)*+xy" // Noncompliant
      "(xy)*+(xy|ab)" // Compliant, but the xy is dead code
      "(xy|ab)*+(xy|ab)" // Noncompliant
      "(xy|a)*+(xy|ab)" // Noncompliant
      "[aeoiu]*+[a-z]" // Compliant possible - the [a-z] can never match vowels, but that’s likely intended
      "[a-z]*+[aeiou]" // Noncompliant
      

      Verification algorithm:

        boolean doesRepetitionContinuationAlwaysFail(RepetitionTree repetitionTree) {
          return repetitionTree.getQuantifier().getModifier() == Modifier.POSSESSIVE &&
            isSupersetOfPrefix(repetitionTree, repetitionTree.continuation(), repetitionTree.continuation(), new HashSet<>());
        }
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                alban.auzeill Alban Auzeill
                Reporter:
                alban.auzeill Alban Auzeill
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Due:
                  Created:
                  Updated:
                  Resolved: