OpenHFT / Chronicle-Bytes

Chronicle Bytes has a similar purpose to Java NIO's ByteBuffer with many extensions
http://chronicle.software
Apache License 2.0
404 stars 84 forks source link

null byte in JSON representation #440

Closed ctrychta closed 2 years ago

ctrychta commented 2 years ago

There seems to be some cases where JSONWire adds NULL byte(s) to the JSON representation. So far I have only seen this when settinguse8bit to true. Here is a minimal example that shows that behavior:

    @Test
    void example() {
        final Bytes<byte[]> bytes = Bytes.allocateElasticOnHeap(1024);
        final JSONWire wire = new JSONWire(bytes, true);

        final Foo foo = new Foo();

        wire.getValueOut().marshallable(foo);

        System.out.println(bytes.toHexString());
    }

    private static class Foo extends SelfDescribingMarshallable {
        final Bar a = new Bar();

        private static class Bar extends SelfDescribingMarshallable {
            String b = "c";
        }
    }

The output is below and contains a NULL byte before the closing "}":

00000000 22 61 22 3a 7b 22 62 22  3a 22 63 22 00 7d       "a":{"b" :"c"·} 

This bit of code in ByteStringAppender looks suspicious:

    default B append8bit(@NotNull CharSequence cs, int start, int end)
            throws IllegalArgumentException, BufferOverflowException, BufferUnderflowException, IndexOutOfBoundsException, IllegalStateException {
        if (cs instanceof BytesStore) {
            return write((BytesStore) cs, (long) start, end);
        }
...

start and end are being passed to write which expects an offset and length: default S write(@NotNull BytesStore bytes, long offset, long length).

RobAustin commented 2 years ago

@jkolobok can you take look at this, and close it if its not an issue

hft-team-city commented 2 years ago

Released in Chronicle-Bytes-2.23ea29, BOM-2.23ea121