Skip to content

Commit 5ef5bd1

Browse files
committed
CODEC-231 StringUtils.equals(CharSequence cs1, CharSequence cs2) can fail with String Index OBE
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1788777 13f79535-47bb-0310-9956-ffa450edef68
1 parent ca30fae commit 5ef5bd1

3 files changed

Lines changed: 31 additions & 1 deletion

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ The <action> type attribute can be add,update,fix,remove.
4545
<release version="1.11" date="2017-MM-DD" description="Feature and fix release.">
4646
<!-- The first attribute below should be the issue id; makes it easier to navigate in the IDE outline -->
4747

48+
<action issue="CODEC-231" dev="sebb" type="fix">StringUtils.equals(CharSequence cs1, CharSequence cs2) can fail with String Index OBE</action>
4849
<action issue="CODEC-230" dev="sebb" type="fix">URLCodec.WWW_FORM_URL should be private</action>
4950
<action issue="CODEC-229" dev="sebb" type="fix">StringUtils.newStringxxx(null) should return null, not NPE</action>
5051
<action issue="CODEC-220" dev="sebb" type="add">Fluent interface for DigestUtils</action>

src/main/java/org/apache/commons/codec/binary/StringUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public static boolean equals(final CharSequence cs1, final CharSequence cs2) {
7878
if (cs1 instanceof String && cs2 instanceof String) {
7979
return cs1.equals(cs2);
8080
}
81-
return CharSequenceUtils.regionMatches(cs1, false, 0, cs2, 0, Math.max(cs1.length(), cs2.length()));
81+
return cs1.length() == cs2.length() && CharSequenceUtils.regionMatches(cs1, false, 0, cs2, 0, cs1.length());
8282
}
8383

8484
/**

src/test/java/org/apache/commons/codec/binary/StringUtilsTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,33 @@ public void testNewStringUtf8() throws UnsupportedEncodingException {
208208
final String actual = StringUtils.newStringUtf8(BYTES_FIXTURE);
209209
Assert.assertEquals(expected, actual);
210210
}
211+
212+
@Test
213+
public void testEqualsString() {
214+
Assert.assertTrue(StringUtils.equals(null, null));
215+
Assert.assertFalse(StringUtils.equals("abc", null));
216+
Assert.assertFalse(StringUtils.equals(null, "abc"));
217+
Assert.assertTrue(StringUtils.equals("abc", "abc"));
218+
Assert.assertFalse(StringUtils.equals("abc", "abcd"));
219+
Assert.assertFalse(StringUtils.equals("abcd", "abc"));
220+
Assert.assertFalse(StringUtils.equals("abc", "ABC"));
221+
}
222+
223+
@Test
224+
public void testEqualsCS1() {
225+
Assert.assertFalse(StringUtils.equals(new StringBuilder("abc"), null));
226+
Assert.assertFalse(StringUtils.equals(null, new StringBuilder("abc")));
227+
Assert.assertTrue(StringUtils.equals(new StringBuilder("abc"), new StringBuilder("abc")));
228+
Assert.assertFalse(StringUtils.equals(new StringBuilder("abc"), new StringBuilder("abcd")));
229+
Assert.assertFalse(StringUtils.equals(new StringBuilder("abcd"), new StringBuilder("abc")));
230+
Assert.assertFalse(StringUtils.equals(new StringBuilder("abc"), new StringBuilder("ABC")));
231+
}
232+
233+
@Test
234+
public void testEqualsCS2() {
235+
Assert.assertTrue(StringUtils.equals("abc", new StringBuilder("abc")));
236+
Assert.assertFalse(StringUtils.equals(new StringBuilder("abc"), "abcd"));
237+
Assert.assertFalse(StringUtils.equals("abcd", new StringBuilder("abc")));
238+
Assert.assertFalse(StringUtils.equals(new StringBuilder("abc"), "ABC"));
239+
}
211240
}

0 commit comments

Comments
 (0)