This is an automated email from the ASF dual-hosted git repository. liujun pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/dubbo.git
commit 894651a2fe28f0a33f23b28cd1090698197c9874 Author: 孙不服 <[email protected]> AuthorDate: Wed Jul 17 10:29:32 2019 +0800 [Dubbo] Fix StringIndexOutOfBoundsException when len=0 #4402 (#4425) * add guard clause for len=0 * add guard clause for len=0 --- dubbo-common/src/main/java/org/apache/dubbo/common/io/Bytes.java | 6 ++++++ .../src/test/java/org/apache/dubbo/common/io/BytesTest.java | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/io/Bytes.java b/dubbo-common/src/main/java/org/apache/dubbo/common/io/Bytes.java index 5112dcf..236c4ba 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/io/Bytes.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/io/Bytes.java @@ -620,6 +620,9 @@ public class Bytes { 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."); } @@ -708,6 +711,9 @@ public class Bytes { 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."); } diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/io/BytesTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/io/BytesTest.java index cae1a83..c9e6d8b 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/io/BytesTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/io/BytesTest.java @@ -57,6 +57,12 @@ public class BytesTest { 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
