You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by li...@apache.org on 2021/12/16 06:39:29 UTC
[dubbo] branch 3.0 updated: Fix The logic of setBytes does not meet expectations (#9174)
This is an automated email from the ASF dual-hosted git repository.
liujun pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/3.0 by this push:
new dee5181 Fix The logic of setBytes does not meet expectations (#9174)
dee5181 is described below
commit dee5181645036f3bbaca9cc9492e9ccd3c2b700e
Author: huazhongming <cr...@gmail.com>
AuthorDate: Thu Dec 16 14:39:14 2021 +0800
Fix The logic of setBytes does not meet expectations (#9174)
fixes #6207
---
.../transport/netty/NettyBackedChannelBuffer.java | 7 ++-
.../netty/NettyBackedChannelBufferTest.java | 63 +++++++++++++++++++++
.../transport/netty4/NettyBackedChannelBuffer.java | 8 ++-
.../netty4/NettyBackedChannelBufferTest.java | 65 ++++++++++++++++++++++
4 files changed, 138 insertions(+), 5 deletions(-)
diff --git a/dubbo-remoting/dubbo-remoting-netty/src/main/java/org/apache/dubbo/remoting/transport/netty/NettyBackedChannelBuffer.java b/dubbo-remoting/dubbo-remoting-netty/src/main/java/org/apache/dubbo/remoting/transport/netty/NettyBackedChannelBuffer.java
index 51c697c..54b8f28 100644
--- a/dubbo-remoting/dubbo-remoting-netty/src/main/java/org/apache/dubbo/remoting/transport/netty/NettyBackedChannelBuffer.java
+++ b/dubbo-remoting/dubbo-remoting-netty/src/main/java/org/apache/dubbo/remoting/transport/netty/NettyBackedChannelBuffer.java
@@ -28,7 +28,7 @@ import java.nio.ByteBuffer;
public class NettyBackedChannelBuffer implements ChannelBuffer {
- private org.jboss.netty.buffer.ChannelBuffer buffer;
+ private final org.jboss.netty.buffer.ChannelBuffer buffer;
public NettyBackedChannelBuffer(org.jboss.netty.buffer.ChannelBuffer buffer) {
Assert.notNull(buffer, "buffer == null");
@@ -116,9 +116,12 @@ public class NettyBackedChannelBuffer implements ChannelBuffer {
@Override
public void setBytes(int index, ChannelBuffer src, int srcIndex, int length) {
+ if (length > src.readableBytes()) {
+ throw new IndexOutOfBoundsException();
+ }
// careful
byte[] data = new byte[length];
- buffer.getBytes(srcIndex, data, 0, length);
+ src.getBytes(srcIndex, data, 0, length);
setBytes(index, data, 0, length);
}
diff --git a/dubbo-remoting/dubbo-remoting-netty/src/test/java/org/apache/dubbo/remoting/transport/netty/NettyBackedChannelBufferTest.java b/dubbo-remoting/dubbo-remoting-netty/src/test/java/org/apache/dubbo/remoting/transport/netty/NettyBackedChannelBufferTest.java
new file mode 100644
index 0000000..046774c
--- /dev/null
+++ b/dubbo-remoting/dubbo-remoting-netty/src/test/java/org/apache/dubbo/remoting/transport/netty/NettyBackedChannelBufferTest.java
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.dubbo.remoting.transport.netty;
+
+
+import org.apache.dubbo.remoting.buffer.ChannelBuffer;
+import org.jboss.netty.buffer.ChannelBuffers;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class NettyBackedChannelBufferTest {
+
+ private static final int CAPACITY = 4096;
+
+ private ChannelBuffer buffer;
+
+ @BeforeEach
+ public void init() {
+ buffer = new NettyBackedChannelBuffer(ChannelBuffers.dynamicBuffer(CAPACITY));
+ }
+
+ @AfterEach
+ public void dispose() {
+ buffer = null;
+ }
+
+ @Test
+ public void testBufferTransfer() {
+ byte[] tmp1 = {1, 2};
+ byte[] tmp2 = {3, 4};
+ ChannelBuffer source = new NettyBackedChannelBuffer(ChannelBuffers.dynamicBuffer(2));
+ source.writeBytes(tmp1);
+ buffer.writeBytes(tmp2);
+
+ assertEquals(2, buffer.readableBytes());
+ source.setBytes(0, tmp1, 0, 2);
+
+ buffer.setBytes(0, source, 0, 2);
+ assertEquals(2, buffer.readableBytes());
+
+ byte[] actual = new byte[2];
+ buffer.getBytes(0, actual);
+ assertEquals(1, actual[0]);
+ assertEquals(2, actual[1]);
+ }
+}
diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyBackedChannelBuffer.java b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyBackedChannelBuffer.java
index 2c00e94..a64a646 100644
--- a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyBackedChannelBuffer.java
+++ b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyBackedChannelBuffer.java
@@ -30,7 +30,7 @@ import java.nio.ByteBuffer;
public class NettyBackedChannelBuffer implements ChannelBuffer {
- private ByteBuf buffer;
+ private final ByteBuf buffer;
public NettyBackedChannelBuffer(ByteBuf buffer) {
Assert.notNull(buffer, "buffer == null");
@@ -115,9 +115,12 @@ public class NettyBackedChannelBuffer implements ChannelBuffer {
@Override
public void setBytes(int index, ChannelBuffer src, int srcIndex, int length) {
+ if (length > src.readableBytes()) {
+ throw new IndexOutOfBoundsException();
+ }
// careful
byte[] data = new byte[length];
- buffer.getBytes(srcIndex, data, 0, length);
+ src.getBytes(srcIndex, data, 0, length);
setBytes(index, data, 0, length);
}
@@ -155,7 +158,6 @@ public class NettyBackedChannelBuffer implements ChannelBuffer {
// AbstractChannelBuffer
-
@Override
public void clear() {
buffer.clear();
diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/NettyBackedChannelBufferTest.java b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/NettyBackedChannelBufferTest.java
new file mode 100644
index 0000000..5b2309e
--- /dev/null
+++ b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/NettyBackedChannelBufferTest.java
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.dubbo.remoting.transport.netty4;
+
+
+import io.netty.buffer.Unpooled;
+import org.apache.dubbo.remoting.buffer.ChannelBuffer;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class NettyBackedChannelBufferTest {
+
+ private static final int CAPACITY = 4096;
+
+ private ChannelBuffer buffer;
+
+ @BeforeEach
+ public void init() {
+ buffer = new NettyBackedChannelBuffer(Unpooled.buffer(CAPACITY, CAPACITY * 2));
+ }
+
+ @AfterEach
+ public void dispose() {
+ buffer = null;
+ }
+
+ @Test
+ public void testBufferTransfer() {
+ byte[] tmp1 = {1, 2};
+ byte[] tmp2 = {3, 4};
+ ChannelBuffer source = new NettyBackedChannelBuffer(Unpooled.buffer(2, 4));
+ source.writeBytes(tmp1);
+ buffer.writeBytes(tmp2);
+
+ assertEquals(2, buffer.readableBytes());
+ source.setBytes(0, tmp1, 0, 2);
+
+ buffer.setBytes(0, source, 0, 2);
+ assertEquals(2, buffer.readableBytes());
+
+ byte[] actual = new byte[2];
+ buffer.getBytes(0, actual);
+ assertEquals(1, actual[0]);
+ assertEquals(2, actual[1]);
+ }
+
+}