Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guard Values against null/empty values #1965

Merged
merged 2 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/main/java/net/sf/jsqlparser/expression/DoubleValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public DoubleValue() {
}

public DoubleValue(final String value) {
if (value == null || value.length() == 0) {
throw new IllegalArgumentException("value can neither be null nor empty.");
}
String val = value;
if (val.charAt(0) == '+') {
val = val.substring(1);
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/net/sf/jsqlparser/expression/LongValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public LongValue() {
}

public LongValue(final String value) {
if (value == null || value.length() == 0) {
throw new IllegalArgumentException("value can neither be null nor empty.");
}
String val = value;
if (val.charAt(0) == '+') {
val = val.substring(1);
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/net/sf/jsqlparser/expression/TimeValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public TimeValue() {
}

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

Expand Down
31 changes: 31 additions & 0 deletions src/test/java/net/sf/jsqlparser/expression/DoubleValueTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2019 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.expression;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertThrows;

public class DoubleValueTest {

@Test
public void testNullValue() {
assertThrows(IllegalArgumentException.class, () -> {
new DoubleValue(null);
});
}

@Test
public void testEmptyValue() {
assertThrows(IllegalArgumentException.class, () -> {
new DoubleValue("");
});
}
}
17 changes: 16 additions & 1 deletion src/test/java/net/sf/jsqlparser/expression/LongValueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import java.math.BigInteger;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -39,8 +40,22 @@ public void testLargeNumber() {
value.getValue();
fail("should not work");
} catch (Exception e) {
//expected to fail
// expected to fail
}
assertEquals(new BigInteger(largeNumber), value.getBigIntegerValue());
}

@Test
public void testNullStringValue() {
assertThrows(IllegalArgumentException.class, () -> {
new LongValue((String) null);
});
}

@Test
public void testEmptyStringValue() {
assertThrows(IllegalArgumentException.class, () -> {
new LongValue("");
});
}
}
31 changes: 31 additions & 0 deletions src/test/java/net/sf/jsqlparser/expression/TimeValueTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2019 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.expression;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertThrows;

public class TimeValueTest {

@Test
public void testNullValue() {
assertThrows(IllegalArgumentException.class, () -> {
new TimeValue(null);
});
}

@Test
public void testEmptyValue() {
assertThrows(IllegalArgumentException.class, () -> {
new TimeValue("");
});
}
}
Loading