From f599aa485268b89ef68ae8fdbb08c3d3ea23ff08 Mon Sep 17 00:00:00 2001 From: CalDescent Date: Sat, 17 Jul 2021 14:43:02 +0100 Subject: [PATCH] Modified serializeSizedString() and deserializeSizedString() to cope with null strings. This affects various other parts of the system, not just arbitrary transactions. --- .../java/org/qortal/utils/Serialization.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/qortal/utils/Serialization.java b/src/main/java/org/qortal/utils/Serialization.java index e9bf6e0e..8c3c43ed 100644 --- a/src/main/java/org/qortal/utils/Serialization.java +++ b/src/main/java/org/qortal/utils/Serialization.java @@ -101,9 +101,17 @@ public class Serialization { } public static void serializeSizedString(ByteArrayOutputStream bytes, String string) throws UnsupportedEncodingException, IOException { - byte[] stringBytes = string.getBytes(StandardCharsets.UTF_8); - bytes.write(Ints.toByteArray(stringBytes.length)); - bytes.write(stringBytes); + byte[] stringBytes = null; + int stringBytesLength = 0; + + if (string != null) { + stringBytes = string.getBytes(StandardCharsets.UTF_8); + stringBytesLength = stringBytes.length; + } + bytes.write(Ints.toByteArray(stringBytesLength)); + if (stringBytesLength > 0) { + bytes.write(stringBytes); + } } public static String deserializeSizedString(ByteBuffer byteBuffer, int maxSize) throws TransformationException { @@ -114,6 +122,9 @@ public class Serialization { if (size > byteBuffer.remaining()) throw new TransformationException("Byte data too short for serialized string"); + if (size == 0) + return null; + byte[] bytes = new byte[size]; byteBuffer.get(bytes);