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

S1120 should not crash on code containing line breaking control characters

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 7.3
    • Component/s: None
    • Labels:

      Description

      When checking the consistency of indentation in a file, S1120 uses the previous line to determine the level of indentation. If the file contains line breaking control characters then the line count can no longer be relied upon to determine what the current line and its predecessor are.

      public class IndentationCheckWithControlCharacters {
        String a = "'…' next line";            // U+0085
        String b = "'
' line separator";      // U+2028
      
        public void expectACrashJustBelowThisLine() {
          return;
        }
      }
      

      In the example above, if the strings a and b both contain a line breaking Unicode character, that offsets the count by 2.
      When trying to read the first line of expectACrashJustBelowThisLine, the check will actually load the empty line between string b and the method.
      As the check relies on the position of the first character of the method, it assumes the line above is at least 4 characters long and thus produces the following exception

      java.lang.StringIndexOutOfBoundsException: String index out of range: 0
      

      This bug stems from the way SonarComponents returns lines. Indeed, the Scanner used in the getLines method interprets the control characters as deliberate line breaks and thus introduces splits in the end result.

        Attachments

          Activity

            People

            Assignee:
            dorian.burihabwa Dorian Burihabwa
            Reporter:
            dorian.burihabwa Dorian Burihabwa
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: