Skip to content

Commit 2fdb1e9

Browse files
arthurscchangotson
andauthored
fix: possible StringIndexOutOfBoundsException in ExtendedCommand
Closes: #1141 Co-authored-by: Gauthier Roebroeck <gauthier.roebroeck@gmail.com>
1 parent da8596c commit 2fdb1e9

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/main/java/org/sqlite/ExtendedCommand.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static String removeQuotation(String s) {
5454
if (s == null) return s;
5555

5656
if ((s.startsWith("\"") && s.endsWith("\"")) || (s.startsWith("'") && s.endsWith("'")))
57-
return s.substring(1, s.length() - 1);
57+
return (s.length() >= 2) ? s.substring(1, s.length() - 1) : s;
5858
else return s;
5959
}
6060

src/test/java/org/sqlite/ExtendedCommandTest.java

+22
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
import static org.assertj.core.api.Assertions.assertThat;
1313

1414
import java.sql.SQLException;
15+
import java.util.stream.Stream;
1516
import org.junit.jupiter.api.Test;
17+
import org.junit.jupiter.params.ParameterizedTest;
18+
import org.junit.jupiter.params.provider.Arguments;
19+
import org.junit.jupiter.params.provider.MethodSource;
1620
import org.sqlite.ExtendedCommand.BackupCommand;
1721
import org.sqlite.ExtendedCommand.RestoreCommand;
1822
import org.sqlite.ExtendedCommand.SQLExtension;
@@ -69,4 +73,22 @@ public void parseRestoreCmd() throws SQLException {
6973
assertThat(b.targetDB).isEqualTo("main");
7074
assertThat(b.srcFile).isEqualTo("target/sample.db");
7175
}
76+
77+
@ParameterizedTest
78+
@MethodSource
79+
public void removeQuotation(String input, String expected) throws SQLException {
80+
assertThat(ExtendedCommand.removeQuotation(input)).isEqualTo(expected);
81+
}
82+
83+
private static Stream<Arguments> removeQuotation() {
84+
return Stream.of(
85+
Arguments.of(null, null), // Null String
86+
Arguments.of("'", "'"), // String with one single quotation only
87+
Arguments.of("\"", "\""), // String with one double quotation only
88+
Arguments.of("'Test\"", "'Test\""), // String with two mismatch quotations
89+
Arguments.of("'Test'", "Test"), // String with two matching single quotations
90+
Arguments.of("\"Test\"", "Test"), // String with two matching double quotations
91+
Arguments.of("'Te's\"t'", "Te's\"t") // String with more than two quotations
92+
);
93+
}
7294
}

0 commit comments

Comments
 (0)