-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Fixes a memory leak issue in the Avro C library under abnormal data scenarios. #3635
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -136,8 +136,10 @@ static int read_bytes(avro_reader_t reader, char **bytes, int64_t * len) | |
| avro_set_error("Cannot allocate buffer for bytes value"); | ||
| return ENOMEM; | ||
| } | ||
| AVRO_READ(reader, *bytes, *len); | ||
|
|
||
|
kwenzh marked this conversation as resolved.
|
||
| (*bytes)[*len] = '\0'; | ||
| AVRO_SAFE_READ(reader, *bytes, *len, *len+1); | ||
|
kwenzh marked this conversation as resolved.
Outdated
|
||
|
|
||
| return 0; | ||
| } | ||
|
|
||
|
|
@@ -180,21 +182,30 @@ size_bytes(avro_writer_t writer, const char *bytes, const int64_t len) | |
| static int read_string(avro_reader_t reader, char **s, int64_t *len) | ||
| { | ||
| int64_t str_len = 0; | ||
| int64_t max_available = -1; | ||
| int rval; | ||
| check_prefix(rval, read_long(reader, &str_len), | ||
| "Cannot read string length: "); | ||
| if (str_len < 0) { | ||
| avro_set_error("Invalid string length: %" PRId64, str_len); | ||
| return EINVAL; | ||
| } | ||
| // max := r.tail - r.head + 1; if max >= 0 && size > max | ||
|
kwenzh marked this conversation as resolved.
Outdated
|
||
| max_available = avro_max_read(reader); | ||
| if (max_available >= 0 && str_len > max_available) { | ||
| avro_set_error("mem io: String length %" PRId64 " is greater than available buffer size %" PRId64, | ||
|
kwenzh marked this conversation as resolved.
Outdated
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please make sure to use the same indentation as the rest of the file, i.e. tabs.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| str_len, max_available); | ||
| return ERANGE; | ||
| } | ||
|
|
||
| *len = str_len + 1; | ||
| *s = (char *) avro_malloc(*len); | ||
| if (!*s) { | ||
| avro_set_error("Cannot allocate buffer for string value"); | ||
| return ENOMEM; | ||
| } | ||
| (*s)[str_len] = '\0'; | ||
| AVRO_READ(reader, *s, str_len); | ||
| AVRO_SAFE_READ(reader, *s, str_len, *len); | ||
| return 0; | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -274,6 +274,20 @@ int avro_read(avro_reader_t reader, void *buf, int64_t len) | |
| return EINVAL; | ||
| } | ||
|
|
||
|
|
||
| int64_t avro_max_read(avro_reader_t reader) | ||
| { | ||
| if (is_memory_io(reader)) { | ||
| struct _avro_reader_memory_t *mem_reader = avro_reader_to_memory(reader); | ||
| return mem_reader->len - mem_reader->read; | ||
| } else if (is_file_io(reader)) { | ||
| struct _avro_reader_file_t *file_reader = avro_reader_to_file(reader); | ||
| return bytes_available(file_reader); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This returns only the buffered bytes, not all remaining bytes as the memory io branch above
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry, I haven't found a way to get the remaining buffer length for file I/O. My goal is to check the maximum readable length before malloc to avoid memory leaks caused by the length exceeding the limit during avro_read_memory checks. Are there any other good solutions?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
maybe use |
||
| } | ||
| return -1; | ||
| } | ||
|
|
||
|
|
||
| static int avro_skip_memory(struct _avro_reader_memory_t *reader, int64_t len) | ||
| { | ||
| if (len > 0) { | ||
|
|
||

Uh oh!
There was an error while loading. Please reload this page.