Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AVRO-3221: Verify NUL termination on set_string_len and make buffer more compact #1352

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions lang/c/src/generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -1780,6 +1780,8 @@ avro_generic_string_set_length(const avro_value_iface_t *iface,
{
AVRO_UNUSED(iface);
check_param(EINVAL, val != NULL, "string contents");
check_param(EINVAL, size > 0, "string size");
check_param(EINVAL, val[size-1] == '\0', "string terminator");
avro_raw_string_t *self = (avro_raw_string_t *) vself;
avro_raw_string_set_length(self, val, size);
return 0;
Expand Down
5 changes: 3 additions & 2 deletions lang/c/src/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,10 @@ void
avro_raw_string_set_length(avro_raw_string_t *str,
const void *src, size_t length)
{
avro_raw_string_ensure_buf(str, length+1);
size_t length_including_nul = length + (((char *) src)[length - 1] != '\0');
avro_raw_string_ensure_buf(str, length_including_nul);
memcpy((void *) str->wrapped.buf, src, length);
((char *) str->wrapped.buf)[length] = '\0';
((char *) str->wrapped.buf)[length_including_nul-1] = '\0';
str->wrapped.size = length;
}

Expand Down
4 changes: 3 additions & 1 deletion lang/c/tests/test_avro_values.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,12 @@ _check_invalid_methods(const char *name, avro_value_t *val)
if (type != AVRO_STRING) {
const char *cstr = NULL;
char *str = NULL;
char *str2 = "length without null terminator";
size_t size = 0;
check_bad(get_string, val, &cstr, &size);
check_bad(set_string, val, str);
check_bad(set_string_len, val, str, size);
check_bad(set_string_len, val, str2, strlen(str2));
}

if (type != AVRO_ENUM) {
Expand Down Expand Up @@ -749,7 +751,7 @@ test_string(void)
AVRO_STRING, avro_schema_string()));
try(avro_value_reset(&val),
"Cannot reset string");
try(avro_value_set_string_len(&val, "", 0),
try(avro_value_set_string_len(&val, "", 1),
"Cannot set_len dummy string");

/* First try a round-trip using set_string */
Expand Down