You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by qi...@apache.org on 2021/11/26 07:02:54 UTC
[iotdb] branch master updated: [IOTDB-2064] fix the NPE caused by map serde (#4473)
This is an automated email from the ASF dual-hosted git repository.
qiaojialin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git
The following commit(s) were added to refs/heads/master by this push:
new a8f467c [IOTDB-2064] fix the NPE caused by map serde (#4473)
a8f467c is described below
commit a8f467cdae105cf85472a0e070101ebb666c6f8d
Author: Zhong Wang <wa...@alibaba-inc.com>
AuthorDate: Fri Nov 26 15:02:21 2021 +0800
[IOTDB-2064] fix the NPE caused by map serde (#4473)
---
.../iotdb/tsfile/utils/ReadWriteIOUtils.java | 11 ++++++++
.../iotdb/tsfile/utils/ReadWriteIOUtilsTest.java | 30 ++++++++++++++++++++--
2 files changed, 39 insertions(+), 2 deletions(-)
diff --git a/tsfile/src/main/java/org/apache/iotdb/tsfile/utils/ReadWriteIOUtils.java b/tsfile/src/main/java/org/apache/iotdb/tsfile/utils/ReadWriteIOUtils.java
index a54ad6e..6744c24 100644
--- a/tsfile/src/main/java/org/apache/iotdb/tsfile/utils/ReadWriteIOUtils.java
+++ b/tsfile/src/main/java/org/apache/iotdb/tsfile/utils/ReadWriteIOUtils.java
@@ -111,6 +111,10 @@ public class ReadWriteIOUtils {
}
public static int write(Map<String, String> map, DataOutputStream stream) throws IOException {
+ if (map == null) {
+ return write(-1, stream);
+ }
+
int length = 0;
stream.writeInt(map.size());
length += 4;
@@ -129,6 +133,10 @@ public class ReadWriteIOUtils {
}
public static int write(Map<String, String> map, ByteBuffer buffer) {
+ if (map == null) {
+ return write(-1, buffer);
+ }
+
int length = 0;
byte[] bytes;
buffer.putInt(map.size());
@@ -649,6 +657,9 @@ public class ReadWriteIOUtils {
public static Map<String, String> readMap(ByteBuffer buffer) {
int length = readInt(buffer);
+ if (length == -1) {
+ return null;
+ }
Map<String, String> map = new HashMap<>(length);
for (int i = 0; i < length; i++) {
// key
diff --git a/tsfile/src/test/java/org/apache/iotdb/tsfile/utils/ReadWriteIOUtilsTest.java b/tsfile/src/test/java/org/apache/iotdb/tsfile/utils/ReadWriteIOUtilsTest.java
index 48a89c0..7662508 100644
--- a/tsfile/src/test/java/org/apache/iotdb/tsfile/utils/ReadWriteIOUtilsTest.java
+++ b/tsfile/src/test/java/org/apache/iotdb/tsfile/utils/ReadWriteIOUtilsTest.java
@@ -25,6 +25,7 @@ import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
+import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@@ -35,7 +36,7 @@ public class ReadWriteIOUtilsTest {
protected static final int DEFAULT_BUFFER_SIZE = 4096;
@Test
- public void readStringBufferTest() {
+ public void stringSerdeTest() {
// 1. not null value
String str = "string";
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(DEFAULT_BUFFER_SIZE);
@@ -67,7 +68,7 @@ public class ReadWriteIOUtilsTest {
}
@Test
- public void readMapTest() {
+ public void mapSerdeTest() {
// 1. key: not null; value: not null
String key = "string";
String value = "string";
@@ -136,5 +137,30 @@ public class ReadWriteIOUtilsTest {
result = ReadWriteIOUtils.readMap(ByteBuffer.wrap(byteArrayOutputStream.toByteArray()));
Assert.assertNotNull(result);
Assert.assertEquals(map, result);
+
+ // 5. empty map
+ map = Collections.emptyMap();
+ byteArrayOutputStream = new ByteArrayOutputStream(DEFAULT_BUFFER_SIZE);
+ stream = new DataOutputStream(byteArrayOutputStream);
+ try {
+ ReadWriteIOUtils.write(map, stream);
+ } catch (IOException e) {
+ fail(e.toString());
+ }
+ result = ReadWriteIOUtils.readMap(ByteBuffer.wrap(byteArrayOutputStream.toByteArray()));
+ Assert.assertNotNull(result);
+ Assert.assertTrue(result.isEmpty());
+
+ // 6. null
+ map = null;
+ byteArrayOutputStream = new ByteArrayOutputStream(DEFAULT_BUFFER_SIZE);
+ stream = new DataOutputStream(byteArrayOutputStream);
+ try {
+ ReadWriteIOUtils.write(map, stream);
+ } catch (IOException e) {
+ fail(e.toString());
+ }
+ result = ReadWriteIOUtils.readMap(ByteBuffer.wrap(byteArrayOutputStream.toByteArray()));
+ Assert.assertNull(result);
}
}