Skip to content

Commit b00322e

Browse files
authored
Guard Values against null/empty values (#1965)
* Guard Values against null/empty values The classes modified by this commit are `DoubleValue`, `LongValue`, and `TimeValue`. Both `null` and empty strings provided to their constructors fail, but they provide very different error messages (NullPointerException and StringIndexOutOfBoundsException), which is neither sensible nor helpful in debugging. This commit adds a guard to throw `IllegalArgumentException` for both cases in order to improve coherency and usefulness of the error messages. * fix checkstyle issues
1 parent 67e2204 commit b00322e

File tree

6 files changed

+87
-1
lines changed

6 files changed

+87
-1
lines changed

src/main/java/net/sf/jsqlparser/expression/DoubleValue.java

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ public DoubleValue() {
2424
}
2525

2626
public DoubleValue(final String value) {
27+
if (value == null || value.length() == 0) {
28+
throw new IllegalArgumentException("value can neither be null nor empty.");
29+
}
2730
String val = value;
2831
if (val.charAt(0) == '+') {
2932
val = val.substring(1);

src/main/java/net/sf/jsqlparser/expression/LongValue.java

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ public LongValue() {
2626
}
2727

2828
public LongValue(final String value) {
29+
if (value == null || value.length() == 0) {
30+
throw new IllegalArgumentException("value can neither be null nor empty.");
31+
}
2932
String val = value;
3033
if (val.charAt(0) == '+') {
3134
val = val.substring(1);

src/main/java/net/sf/jsqlparser/expression/TimeValue.java

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ public TimeValue() {
2525
}
2626

2727
public TimeValue(String value) {
28+
if (value == null || value.length() == 0) {
29+
throw new IllegalArgumentException("value can neither be null nor empty.");
30+
}
2831
this.value = Time.valueOf(value.substring(1, value.length() - 1));
2932
}
3033

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2019 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
10+
package net.sf.jsqlparser.expression;
11+
12+
import org.junit.jupiter.api.Test;
13+
14+
import static org.junit.jupiter.api.Assertions.assertThrows;
15+
16+
public class DoubleValueTest {
17+
18+
@Test
19+
public void testNullValue() {
20+
assertThrows(IllegalArgumentException.class, () -> {
21+
new DoubleValue(null);
22+
});
23+
}
24+
25+
@Test
26+
public void testEmptyValue() {
27+
assertThrows(IllegalArgumentException.class, () -> {
28+
new DoubleValue("");
29+
});
30+
}
31+
}

src/test/java/net/sf/jsqlparser/expression/LongValueTest.java

+16-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import java.math.BigInteger;
1313
import static org.junit.jupiter.api.Assertions.assertEquals;
14+
import static org.junit.jupiter.api.Assertions.assertThrows;
1415
import static org.junit.jupiter.api.Assertions.fail;
1516
import org.junit.jupiter.api.Test;
1617

@@ -39,8 +40,22 @@ public void testLargeNumber() {
3940
value.getValue();
4041
fail("should not work");
4142
} catch (Exception e) {
42-
//expected to fail
43+
// expected to fail
4344
}
4445
assertEquals(new BigInteger(largeNumber), value.getBigIntegerValue());
4546
}
47+
48+
@Test
49+
public void testNullStringValue() {
50+
assertThrows(IllegalArgumentException.class, () -> {
51+
new LongValue((String) null);
52+
});
53+
}
54+
55+
@Test
56+
public void testEmptyStringValue() {
57+
assertThrows(IllegalArgumentException.class, () -> {
58+
new LongValue("");
59+
});
60+
}
4661
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2019 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
10+
package net.sf.jsqlparser.expression;
11+
12+
import org.junit.jupiter.api.Test;
13+
14+
import static org.junit.jupiter.api.Assertions.assertThrows;
15+
16+
public class TimeValueTest {
17+
18+
@Test
19+
public void testNullValue() {
20+
assertThrows(IllegalArgumentException.class, () -> {
21+
new TimeValue(null);
22+
});
23+
}
24+
25+
@Test
26+
public void testEmptyValue() {
27+
assertThrows(IllegalArgumentException.class, () -> {
28+
new TimeValue("");
29+
});
30+
}
31+
}

0 commit comments

Comments
 (0)