Skip to content

Commit 6a7cab6

Browse files
committed
AVRO-4136 [c] json encoding of byte[] containing 0x00
Prevent the fixed and bytes type to be preliminary cut off when they are encoded into the json encoding.
1 parent a3123ec commit 6a7cab6

2 files changed

Lines changed: 8 additions & 10 deletions

File tree

lang/c/src/value-json.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include "jansson.h"
3030

3131
/*
32-
* Converts a binary buffer into a NUL-terminated JSON UTF-8 string.
32+
* Converts a binary buffer into a JSON UTF-8 string which is not NULL terminated!
3333
* Avro bytes and fixed values are encoded in JSON as a string, and JSON
3434
* strings must be in UTF-8. For these Avro types, the JSON string is
3535
* restricted to the characters U+0000..U+00FF, which corresponds to the
@@ -51,7 +51,7 @@ encode_utf8_bytes(const void *src, size_t src_len,
5151
// the range 0x80..0xff will take up two.
5252
const uint8_t *src8 = (const uint8_t *) src;
5353

54-
size_t utf8_len = src_len + 1; // +1 for NUL terminator
54+
size_t utf8_len = src_len;
5555
size_t i;
5656
for (i = 0; i < src_len; i++) {
5757
if (src8[i] & 0x80) {
@@ -76,8 +76,6 @@ encode_utf8_bytes(const void *src, size_t src_len,
7676
}
7777
}
7878

79-
*curr = '\0';
80-
8179
// And we're good.
8280
*dest = dest8;
8381
*dest_len = utf8_len;
@@ -127,7 +125,7 @@ avro_value_to_json_t(const avro_value_t *value)
127125
return NULL;
128126
}
129127

130-
json_t *result = json_string_nocheck((const char *) encoded);
128+
json_t *result = json_stringn_nocheck((const char *) encoded, encoded_size);
131129
avro_free(encoded, encoded_size);
132130
if (result == NULL) {
133131
avro_set_error("Cannot allocate JSON bytes");
@@ -242,7 +240,7 @@ avro_value_to_json_t(const avro_value_t *value)
242240
return NULL;
243241
}
244242

245-
json_t *result = json_string_nocheck((const char *) encoded);
243+
json_t *result = json_stringn_nocheck((const char *) encoded, encoded_size);
246244
avro_free(encoded, encoded_size);
247245
if (result == NULL) {
248246
avro_set_error("Cannot allocate JSON fixed");

lang/c/tests/test_avro_data.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,14 @@ static int test_string(void)
181181

182182
static int test_bytes(void)
183183
{
184-
char bytes[] = { 0xDE, 0xAD, 0xBE, 0xEF };
184+
char bytes[] = { 0xDE, 0xAD, 0x00, 0xBE, 0xEF };
185185
avro_schema_t writer_schema = avro_schema_bytes();
186186
avro_datum_t datum;
187187
avro_datum_t expected_datum;
188188

189189
datum = avro_givebytes(bytes, sizeof(bytes), NULL);
190190
write_read_check(writer_schema, datum, NULL, NULL, "bytes");
191-
test_json(datum, "\"\\u00de\\u00ad\\u00be\\u00ef\"");
191+
test_json(datum, "\"\\u00de\\u00ad\\u0000\\u00be\\u00ef\"");
192192
avro_datum_decref(datum);
193193
avro_schema_decref(writer_schema);
194194

@@ -613,14 +613,14 @@ static int test_union(void)
613613

614614
static int test_fixed(void)
615615
{
616-
char bytes[] = { 0xD, 0xA, 0xD, 0xA, 0xB, 0xA, 0xB, 0xA };
616+
char bytes[] = { 0xD, 0xA, 0xD, 0xA, 0xB, 0x0, 0xB, 0xA };
617617
avro_schema_t schema = avro_schema_fixed("msg", sizeof(bytes));
618618
avro_datum_t datum;
619619
avro_datum_t expected_datum;
620620

621621
datum = avro_givefixed(schema, bytes, sizeof(bytes), NULL);
622622
write_read_check(schema, datum, NULL, NULL, "fixed");
623-
test_json(datum, "\"\\r\\n\\r\\n\\u000b\\n\\u000b\\n\"");
623+
test_json(datum, "\"\\r\\n\\r\\n\\u000b\\u0000\\u000b\\n\"");
624624
avro_datum_decref(datum);
625625

626626
datum = avro_givefixed(schema, NULL, sizeof(bytes), NULL);

0 commit comments

Comments
 (0)