Elffuss BraidHub

kikocis/java-driver public

Prevent ParseUtils.unquote() and ParseUtils.isQuoted() from throwing StringIndexOutOfBoundsException on null or empty inputs.
Changeset732cde7e1c80694b81829dd67e0226fa4e1fa8a7e3e18b901a049b339282618e
AuthorAlexandre Dutra <alexandre.dutra@datastax.com> · 2015-12-22 12:14 +01:00
CommitterAlexandre Dutra <alexandre.dutra@datastax.com> · 2015-12-22 12:14 +01:00
Parents461a090f4994

Browse the files at this changeset

2 files changed

modified driver-core/src/main/java/com/datastax/driver/core/ParseUtils.java +7 −4
@@ -232,12 +232,12 @@
232232 * by single quotes, and {@code false} otherwise.
233233 */
234234 public static boolean isQuoted(String value) {
235- return value.charAt(0) == '\'' && value.charAt(value.length() - 1) == '\'';
235+ return value != null && value.length() > 1 && value.charAt(0) == '\'' && value.charAt(value.length() - 1) == '\'';
236236 }
237237
238-
239238 /**
240239 * Quote the given string; single quotes are escaped.
240+ * If the given string is null, this method returns a quoted empty string ({@code ''}).
241241 *
242242 * @param value The value to quote.
243243 * @return The quoted string.
@@ -247,12 +247,15 @@
247247 }
248248
249249 /**
250- * Unquote the given string; single quotes are unescaped.
250+ * Unquote the given string if it is quoted; single quotes are unescaped.
251+ * If the given string is not quoted, it is returned without any modification.
251252 *
252253 * @param value The string to unquote.
253254 * @return The unquoted string.
254255 */
255256 public static String unquote(String value) {
257+ if (!isQuoted(value))
258+ return value;
256259 return value.substring(1, value.length() - 1).replace("''", "'");
257260 }
258261
@@ -445,7 +448,7 @@
445448 */
446449 private static String replaceChar(String text, char search, String replacement) {
447450 if (text == null || text.isEmpty())
448- return text;
451+ return "";
449452
450453 int nbMatch = 0;
451454 int start = -1;
added driver-core/src/test/java/com/datastax/driver/core/ParseUtilsTest.java +63 −0
@@ -0,0 +1,63 @@
1+/*
2+ * Copyright (C) 2012-2015 DataStax Inc.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+package com.datastax.driver.core;
17+
18+import org.testng.annotations.Test;
19+
20+import static com.datastax.driver.core.Assertions.assertThat;
21+
22+public class ParseUtilsTest {
23+
24+ @Test(groups = "unit")
25+ public void testQuote() {
26+ assertThat(ParseUtils.quote(null)).isEqualTo("''");
27+ assertThat(ParseUtils.quote("")).isEqualTo("''");
28+ assertThat(ParseUtils.quote(" ")).isEqualTo("' '");
29+ assertThat(ParseUtils.quote("foo")).isEqualTo("'foo'");
30+ assertThat(ParseUtils.quote(" 'foo' ")).isEqualTo("' ''foo'' '");
31+ }
32+
33+ @Test(groups = "unit")
34+ public void testUnquote() {
35+ assertThat(ParseUtils.unquote(null)).isNull();
36+ assertThat(ParseUtils.unquote("")).isEqualTo("");
37+ assertThat(ParseUtils.unquote(" ")).isEqualTo(" ");
38+ assertThat(ParseUtils.unquote("'")).isEqualTo("'"); // malformed string left untouched
39+ assertThat(ParseUtils.unquote("foo")).isEqualTo("foo");
40+ assertThat(ParseUtils.unquote("''")).isEqualTo("");
41+ assertThat(ParseUtils.unquote("' '")).isEqualTo(" ");
42+ assertThat(ParseUtils.unquote("'foo")).isEqualTo("'foo"); // malformed string left untouched
43+ assertThat(ParseUtils.unquote("'foo'")).isEqualTo("foo");
44+ assertThat(ParseUtils.unquote(" 'foo' ")).isEqualTo(" 'foo' "); // considered unquoted
45+ assertThat(ParseUtils.unquote("'''foo'''")).isEqualTo("'foo'");
46+ }
47+
48+ @Test(groups = "unit")
49+ public void testIsQuoted() {
50+ assertThat(ParseUtils.isQuoted(null)).isFalse();
51+ assertThat(ParseUtils.isQuoted("")).isFalse();
52+ assertThat(ParseUtils.isQuoted(" ")).isFalse();
53+ assertThat(ParseUtils.isQuoted("'")).isFalse(); // malformed string considered unquoted
54+ assertThat(ParseUtils.isQuoted("foo")).isFalse();
55+ assertThat(ParseUtils.isQuoted("''")).isTrue();
56+ assertThat(ParseUtils.isQuoted("' '")).isTrue();
57+ assertThat(ParseUtils.isQuoted("'foo")).isFalse(); // malformed string considered unquoted
58+ assertThat(ParseUtils.isQuoted("'foo'")).isTrue();
59+ assertThat(ParseUtils.isQuoted(" 'foo' ")).isFalse(); // considered unquoted
60+ assertThat(ParseUtils.isQuoted("'''foo'''")).isTrue();
61+ }
62+
63+}