|
1 |
| package net.sourceforge.pmd.rules; |
|
2 |
| |
|
3 |
| import net.sourceforge.pmd.AbstractRule; |
|
4 |
| import net.sourceforge.pmd.ast.ASTLiteral; |
|
5 |
| |
|
6 |
| public class SuspiciousOctalEscape extends AbstractRule { |
|
7 |
| |
|
8 |
4
| public Object visit(ASTLiteral node, Object data) {
|
|
9 |
4
| String image = node.getImage();
|
|
10 |
4
| if (image != null && image.startsWith("\""))
|
|
11 |
| { |
|
12 |
| |
|
13 |
3
| String s = image.substring(1, image.length() - 1);
|
|
14 |
| |
|
15 |
| |
|
16 |
3
| int offset = 0;
|
|
17 |
3
| for (int slash = s.indexOf('\\', offset);
|
|
18 |
6
| slash != -1 && slash < s.length() - 1;
|
|
19 |
| slash = s.indexOf('\\', offset)) { |
|
20 |
3
| String escapeSequence = s.substring(slash + 1);
|
|
21 |
3
| char first = escapeSequence.charAt(0);
|
|
22 |
3
| if (isOctal(first)) {
|
|
23 |
3
| if (escapeSequence.length() > 1) {
|
|
24 |
3
| char second = escapeSequence.charAt(1);
|
|
25 |
3
| if (isOctal(second)) {
|
|
26 |
3
| if (escapeSequence.length() > 2) {
|
|
27 |
3
| char third = escapeSequence.charAt(2);
|
|
28 |
3
| if (isOctal(third)) {
|
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
2
| if (first != '0' && first != '1' && first != '2' && first != '3') {
|
|
34 |
| |
|
35 |
| |
|
36 |
1
| addViolation(data, node);
|
|
37 |
| } else { |
|
38 |
| |
|
39 |
| |
|
40 |
1
| if (escapeSequence.length() > 3) {
|
|
41 |
1
| char fourth = escapeSequence.charAt(3);
|
|
42 |
1
| if (isDecimal(fourth)) {
|
|
43 |
1
| addViolation(data, node);
|
|
44 |
| } |
|
45 |
| } |
|
46 |
| } |
|
47 |
| |
|
48 |
1
| } else if (isDecimal(third)) {
|
|
49 |
| |
|
50 |
| |
|
51 |
1
| addViolation(data, node);
|
|
52 |
| } |
|
53 |
| } |
|
54 |
0
| } else if (isDecimal(second)) {
|
|
55 |
| |
|
56 |
| |
|
57 |
0
| addViolation(data, node);
|
|
58 |
| } |
|
59 |
| } |
|
60 |
| } |
|
61 |
| |
|
62 |
3
| offset = slash + 1;
|
|
63 |
| } |
|
64 |
| } |
|
65 |
| |
|
66 |
4
| return super.visit(node, data);
|
|
67 |
| } |
|
68 |
| |
|
69 |
9
| private boolean isOctal(char c) {
|
|
70 |
9
| switch (c) {
|
|
71 |
5
| case '0':
|
|
72 |
1
| case '1':
|
|
73 |
1
| case '2':
|
|
74 |
0
| case '3':
|
|
75 |
1
| case '4':
|
|
76 |
0
| case '5':
|
|
77 |
0
| case '6':
|
|
78 |
0
| case '7':
|
|
79 |
8
| return true;
|
|
80 |
1
| default:
|
|
81 |
1
| return false;
|
|
82 |
| } |
|
83 |
| } |
|
84 |
| |
|
85 |
2
| private boolean isDecimal(char c) {
|
|
86 |
2
| switch (c) {
|
|
87 |
0
| case '0':
|
|
88 |
0
| case '1':
|
|
89 |
0
| case '2':
|
|
90 |
0
| case '3':
|
|
91 |
0
| case '4':
|
|
92 |
0
| case '5':
|
|
93 |
0
| case '6':
|
|
94 |
0
| case '7':
|
|
95 |
2
| case '8':
|
|
96 |
0
| case '9':
|
|
97 |
2
| return true;
|
|
98 |
0
| default:
|
|
99 |
0
| return false;
|
|
100 |
| } |
|
101 |
| } |
|
102 |
| } |