Skip to content

Commit

Permalink
[Dubbo] Fix StringIndexOutOfBoundsException when len=0 apache#4402 (a…
Browse files Browse the repository at this point in the history
…pache#4425)

* add guard clause for len=0

* add guard clause for len=0
  • Loading branch information
sunbufu authored and chickenlj committed Jul 19, 2019
1 parent 9b1e78b commit 894651a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,9 @@ public static byte[] base642bytes(final String str, final int off, final int len
if (len < 0) {
throw new IndexOutOfBoundsException("base642bytes: length < 0, length is " + len);
}
if (len == 0) {
return new byte[0];
}
if (off + len > str.length()) {
throw new IndexOutOfBoundsException("base642bytes: offset + length > string length.");
}
Expand Down Expand Up @@ -708,6 +711,9 @@ public static byte[] base642bytes(final String str, final int off, final int len
if (len < 0) {
throw new IndexOutOfBoundsException("base642bytes: length < 0, length is " + len);
}
if (len == 0) {
return new byte[0];
}
if (off + len > str.length()) {
throw new IndexOutOfBoundsException("base642bytes: offset + length > string length.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ public void testMain() throws Exception {

byte[] bytesWithC64 = Bytes.base642bytes(str, C64);
assertThat(bytesWithC64, is(bytes));

byte[] emptyBytes = Bytes.base642bytes("dubbo", 0, 0);
assertThat(emptyBytes, is("".getBytes()));

assertThat(Bytes.base642bytes("dubbo", 0, 0, ""), is("".getBytes()));
assertThat(Bytes.base642bytes("dubbo", 0, 0, new char[0]), is("".getBytes()));
}

@Test
Expand Down

0 comments on commit 894651a

Please sign in to comment.