Skip to content

Commit abcb520

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 80cebcf commit abcb520

File tree

3 files changed

+11
-12
lines changed

3 files changed

+11
-12
lines changed

lang/c/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,13 @@ set(CODEC_LIBRARIES ${ZLIB_LIBRARIES} ${LZMA_LIBRARIES} ${SNAPPY_LIBRARIES})
179179
set(CODEC_PKG "${ZLIB_PKG} ${LZMA_PKG} ${SNAPPY_PKG}")
180180

181181
# Jansson JSON library
182-
pkg_check_modules(JANSSON jansson>=2.3)
182+
pkg_check_modules(JANSSON jansson>=2.7)
183183
if (JANSSON_FOUND)
184184
set(JANSSON_PKG libjansson)
185185
include_directories(${JANSSON_INCLUDE_DIRS})
186186
link_directories(${JANSSON_LIBRARY_DIRS})
187187
else (JANSSON_FOUND)
188-
message(FATAL_ERROR "libjansson >=2.3 not found")
188+
message(FATAL_ERROR "libjansson >=2.7 not found")
189189
endif (JANSSON_FOUND)
190190

191191

lang/c/src/value-json.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
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
33+
* terminated with a null byte ('\0').
3334
* Avro bytes and fixed values are encoded in JSON as a string, and JSON
3435
* strings must be in UTF-8. For these Avro types, the JSON string is
3536
* restricted to the characters U+0000..U+00FF, which corresponds to the
@@ -51,7 +52,7 @@ encode_utf8_bytes(const void *src, size_t src_len,
5152
// the range 0x80..0xff will take up two.
5253
const uint8_t *src8 = (const uint8_t *) src;
5354

54-
size_t utf8_len = src_len + 1; // +1 for NUL terminator
55+
size_t utf8_len = src_len;
5556
size_t i;
5657
for (i = 0; i < src_len; i++) {
5758
if (src8[i] & 0x80) {
@@ -76,8 +77,6 @@ encode_utf8_bytes(const void *src, size_t src_len,
7677
}
7778
}
7879

79-
*curr = '\0';
80-
8180
// And we're good.
8281
*dest = dest8;
8382
*dest_len = utf8_len;
@@ -127,7 +126,7 @@ avro_value_to_json_t(const avro_value_t *value)
127126
return NULL;
128127
}
129128

130-
json_t *result = json_string_nocheck((const char *) encoded);
129+
json_t *result = json_stringn_nocheck((const char *) encoded, encoded_size);
131130
avro_free(encoded, encoded_size);
132131
if (result == NULL) {
133132
avro_set_error("Cannot allocate JSON bytes");
@@ -242,7 +241,7 @@ avro_value_to_json_t(const avro_value_t *value)
242241
return NULL;
243242
}
244243

245-
json_t *result = json_string_nocheck((const char *) encoded);
244+
json_t *result = json_stringn_nocheck((const char *) encoded, encoded_size);
246245
avro_free(encoded, encoded_size);
247246
if (result == NULL) {
248247
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)