This is an automated email from the ASF dual-hosted git repository.
iluo pushed a commit to branch 2.7.3-release
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/2.7.3-release by this push:
new 2e341d1 [Dubbo] Fix StringIndexOutOfBoundsException when len=0 #4402
(#4425)
2e341d1 is described below
commit 2e341d1d8a9da9612eaece85a3eaa8c93dba7096
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 263e7b9..c94f0ce 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 71b915c..90a3327 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