|
| 1 | +## Overview |
| 2 | + |
| 3 | +This query detects non-explicit control and whitespace characters in Java literals. |
| 4 | +Such characters are often introduced accidentally and can be invisible or hard to recognize, leading to bugs when the actual contents of the string contain control characters. |
| 5 | + |
| 6 | +## Recommendation |
| 7 | + |
| 8 | +To avoid issues, use the encoded versions of control characters (e.g. ASCII `\n`, `\t`, or Unicode `U+000D`, `U+0009`). |
| 9 | +This makes the literals (e.g. string literals) more readable, and also helps to make the surrounding code less error-prone and more maintainable. |
| 10 | + |
| 11 | +## Example |
| 12 | + |
| 13 | +The following examples illustrate good and bad code: |
| 14 | + |
| 15 | +Bad: |
| 16 | + |
| 17 | +```java |
| 18 | +char tabulationChar = ' '; // Non compliant |
| 19 | +String tabulationCharInsideString = "A B"; // Non compliant |
| 20 | +String fooZeroWidthSpacebar = "foobar"; // Non compliant |
| 21 | +``` |
| 22 | + |
| 23 | +Good: |
| 24 | + |
| 25 | +```java |
| 26 | +char escapedTabulationChar = '\t'; |
| 27 | +String escapedTabulationCharInsideString = "A\tB"; // Compliant |
| 28 | +String fooUnicodeSpacebar = "foo\u0020bar"; // Compliant |
| 29 | +String foo2Spacebar = "foo bar"; // Compliant |
| 30 | +String foo3Spacebar = "foo bar"; // Compliant |
| 31 | +``` |
| 32 | + |
| 33 | +## Implementation notes |
| 34 | + |
| 35 | +This query detects Java literals that contain reserved control characters and/or non-printable whitespace characters, such as: |
| 36 | + |
| 37 | +- Decimal and hexidecimal representations of ASCII control characters (code points 0-8, 11, 14-31, and 127). |
| 38 | +- Invisible characters (e.g. zero-width space, zero-width joiner). |
| 39 | +- Unicode C0 control codes, plus the delete character (U+007F), such as: |
| 40 | + |
| 41 | + | Escaped Unicode | ASCII Decimal | Description | |
| 42 | + | --------------- | ------------- | ------------------------- | |
| 43 | + | `\u0000` | 0 | null character | |
| 44 | + | `\u0001` | 1 | start of heading | |
| 45 | + | `\u0002` | 2 | start of text | |
| 46 | + | `\u0003` | 3 | end of text | |
| 47 | + | `\u0004` | 4 | end of transmission | |
| 48 | + | `\u0005` | 5 | enquiry | |
| 49 | + | `\u0006` | 6 | acknowledge | |
| 50 | + | `\u0007` | 7 | bell | |
| 51 | + | `\u0008` | 8 | backspace | |
| 52 | + | `\u000B` | 11 | vertical tab | |
| 53 | + | `\u000E` | 14 | shift out | |
| 54 | + | `\u000F` | 15 | shift in | |
| 55 | + | `\u0010` | 16 | data link escape | |
| 56 | + | `\u0011` | 17 | device control 1 | |
| 57 | + | `\u0012` | 18 | device control 2 | |
| 58 | + | `\u0013` | 19 | device control 3 | |
| 59 | + | `\u0014` | 20 | device control 4 | |
| 60 | + | `\u0015` | 21 | negative acknowledge | |
| 61 | + | `\u0016` | 22 | synchronous idle | |
| 62 | + | `\u0017` | 23 | end of transmission block | |
| 63 | + | `\u0018` | 24 | cancel | |
| 64 | + | `\u0019` | 25 | end of medium | |
| 65 | + | `\u001A` | 26 | substitute | |
| 66 | + | `\u001B` | 27 | escape | |
| 67 | + | `\u001C` | 28 | file separator | |
| 68 | + | `\u001D` | 29 | group separator | |
| 69 | + | `\u001E` | 30 | record separator | |
| 70 | + | `\u001F` | 31 | unit separator | |
| 71 | + | `\u007F` | 127 | delete | |
| 72 | + |
| 73 | +- Zero-width Unicode characters (e.g. zero-width space, zero-width joiner), such as: |
| 74 | + |
| 75 | + | Escaped Unicode | Description | |
| 76 | + | --------------- | ------------------------- | |
| 77 | + | `\u200B` | zero-width space | |
| 78 | + | `\u200C` | zero-width non-joiner | |
| 79 | + | `\u200D` | zero-width joiner | |
| 80 | + | `\u2028` | line separator | |
| 81 | + | `\u2029` | paragraph separator | |
| 82 | + | `\u2060` | word joiner | |
| 83 | + | `\uFEFF` | zero-width no-break space | |
| 84 | + |
| 85 | +The following list outlines the _**explicit exclusions from query scope**_: |
| 86 | + |
| 87 | +- any number of simple space characters (`U+0020`, ASCII 32). |
| 88 | +- an escape character sequence (e.g. `\t`), or the Unicode equivalent (e.g. `\u0009`), for printable whitespace characters: |
| 89 | + |
| 90 | + | Character Sequence | Escaped Unicode | ASCII Decimal | Description | |
| 91 | + | ------------------ | --------------- | ------------- | --------------- | |
| 92 | + | `\t` | \u0009 | 9 | horizontal tab | |
| 93 | + | `\n` | \u000A | 10 | line feed | |
| 94 | + | `\f` | \u000C | 12 | form feed | |
| 95 | + | `\r` | \u000D | 13 | carriage return | |
| 96 | + | | \u0020 | 32 | space | |
| 97 | + |
| 98 | +- character literals (i.e. single quotes) containing control characters. |
| 99 | +- literals defined within "likely" test methods, such as: |
| 100 | + - JUnit test methods |
| 101 | + - methods annotated with `@Test` |
| 102 | + - methods of a class annotated with `@Test` |
| 103 | + - methods with names containing "test" |
| 104 | + |
| 105 | +## References |
| 106 | + |
| 107 | +- Unicode: [Unicode Control Characters](https://www.unicode.org/charts/PDF/U0000.pdf). |
| 108 | +- Wikipedia: [Unicode C0 control codes](https://en.wikipedia.org/wiki/C0_and_C1_control_codes). |
| 109 | +- Wikipedia: [Unicode characters with property "WSpace=yes" or "White_Space=yes"](https://en.wikipedia.org/wiki/Unicode_character_property#Whitespace). |
| 110 | +- Java API Specification: [Java String Literals](https://docs.oracle.com/javase/tutorial/java/data/characters.html). |
| 111 | +- Java API Specification: [Java Class Charset](https://docs.oracle.com/javase/8/docs/api///?java/nio/charset/Charset.html). |
0 commit comments