MCPcopy Create free account
hub / github.com/spotbugs/spotbugs / BadRegEx

Class BadRegEx

spotbugsTestCases/src/java/BadRegEx.java:6–63  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4import edu.umd.cs.findbugs.annotations.NoWarning;
5
6class BadRegEx {
7
8 @ExpectWarning("RE")
9 boolean f(String s) {
10 return s.matches("][");
11 }
12
13 @ExpectWarning("RE")
14 String g(String s) {
15 return s.replaceAll("][", "xx");
16 }
17
18 @ExpectWarning("RE")
19 String h(String s) {
20 return s.replaceFirst("][", "xx");
21 }
22
23 @ExpectWarning("RE")
24 void x(String s) throws Exception {
25 Pattern.matches("][", s);
26 }
27
28 @ExpectWarning("RE")
29 Pattern y(String s) throws Exception {
30 return Pattern.compile("][", Pattern.CASE_INSENSITIVE);
31 }
32
33 @ExpectWarning("RE")
34 Pattern z(String s) {
35 return Pattern.compile("][");
36 }
37
38 @NoWarning("RE")
39 Pattern literalOne(String s) throws Exception {
40 return Pattern.compile("][", Pattern.LITERAL); // not a bug
41 }
42
43 @NoWarning("RE")
44 Pattern literalTwo(String s) throws Exception {
45 return Pattern.compile("][", Pattern.CASE_INSENSITIVE | Pattern.LITERAL); // not
46 // a
47 // bug
48 }
49
50 // this is OK; we shouldn't report a warning here
51 @NoWarning("RE")
52 String passwordMasking(String s) {
53 return s.replaceAll(".", "x");
54 }
55
56 private StringBuilder allStatesPost;
57
58 Pattern test() {
59 String statesPost = allStatesPost.append(").*$").toString();
60
61 return Pattern.compile(statesPost, Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
62 }
63}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…