Skip to content

Commit 894651a

Browse files
sunbufuchickenlj
authored andcommitted
[Dubbo] Fix StringIndexOutOfBoundsException when len=0 #4402 (#4425)
* add guard clause for len=0 * add guard clause for len=0
1 parent 9b1e78b commit 894651a

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

dubbo-common/src/main/java/org/apache/dubbo/common/io/Bytes.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,9 @@ public static byte[] base642bytes(final String str, final int off, final int len
620620
if (len < 0) {
621621
throw new IndexOutOfBoundsException("base642bytes: length < 0, length is " + len);
622622
}
623+
if (len == 0) {
624+
return new byte[0];
625+
}
623626
if (off + len > str.length()) {
624627
throw new IndexOutOfBoundsException("base642bytes: offset + length > string length.");
625628
}
@@ -708,6 +711,9 @@ public static byte[] base642bytes(final String str, final int off, final int len
708711
if (len < 0) {
709712
throw new IndexOutOfBoundsException("base642bytes: length < 0, length is " + len);
710713
}
714+
if (len == 0) {
715+
return new byte[0];
716+
}
711717
if (off + len > str.length()) {
712718
throw new IndexOutOfBoundsException("base642bytes: offset + length > string length.");
713719
}

dubbo-common/src/test/java/org/apache/dubbo/common/io/BytesTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ public void testMain() throws Exception {
5757

5858
byte[] bytesWithC64 = Bytes.base642bytes(str, C64);
5959
assertThat(bytesWithC64, is(bytes));
60+
61+
byte[] emptyBytes = Bytes.base642bytes("dubbo", 0, 0);
62+
assertThat(emptyBytes, is("".getBytes()));
63+
64+
assertThat(Bytes.base642bytes("dubbo", 0, 0, ""), is("".getBytes()));
65+
assertThat(Bytes.base642bytes("dubbo", 0, 0, new char[0]), is("".getBytes()));
6066
}
6167

6268
@Test

0 commit comments

Comments
 (0)