-
Notifications
You must be signed in to change notification settings - Fork 1.8k
AVRO-4246: [c] Fix memory leak on failed decoding #3732
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
Open
fabiocfabini
wants to merge
3
commits into
apache:main
Choose a base branch
from
fabiocfabini:avro-4246-memory-leak-in-avroc-on-failed-decoding
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+228
−2
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,223 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to you under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
| * implied. See the License for the specific language governing | ||
| * permissions and limitations under the License. | ||
| */ | ||
|
|
||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
| #include <stdint.h> | ||
| #include <avro.h> | ||
|
|
||
| #define STRING_SCHEMA_LITERAL "\"string\"" | ||
| #define INT_SCHEMA_LITERAL "\"int\"" | ||
|
|
||
| typedef struct | ||
| { | ||
| char data[256]; | ||
| size_t size; | ||
| } encoded_buf_t; | ||
|
|
||
| static int encode_int( | ||
| avro_value_iface_t *iface, | ||
| int32_t number, | ||
| encoded_buf_t *out_buf) | ||
| { | ||
| int rc; | ||
| avro_value_t value; | ||
| avro_writer_t writer = NULL; | ||
|
|
||
| memset(out_buf, 0, sizeof(*out_buf)); | ||
|
|
||
| rc = avro_generic_value_new(iface, &value); | ||
| if (rc != 0) | ||
| { | ||
| fprintf(stderr, "encode_int: avro_generic_value_new failed: %s\n", | ||
| avro_strerror()); | ||
| return rc; | ||
| } | ||
|
|
||
| rc = avro_value_set_int(&value, number); | ||
| if (rc != 0) | ||
| { | ||
| fprintf(stderr, "encode_int: avro_value_set_int failed: %s\n", | ||
| avro_strerror()); | ||
| avro_value_decref(&value); | ||
| return rc; | ||
| } | ||
|
|
||
| writer = avro_writer_memory(out_buf->data, sizeof(out_buf->data)); | ||
| if (writer == NULL) | ||
| { | ||
| fprintf(stderr, "encode_int: avro_writer_memory failed\n"); | ||
| avro_value_decref(&value); | ||
| return -1; | ||
| } | ||
|
|
||
| rc = avro_value_write(writer, &value); | ||
| if (rc != 0) | ||
| { | ||
| fprintf(stderr, "encode_int: avro_value_write failed: %s\n", | ||
| avro_strerror()); | ||
| avro_writer_free(writer); | ||
| avro_value_decref(&value); | ||
| return rc; | ||
| } | ||
|
|
||
| out_buf->size = avro_writer_tell(writer); | ||
|
|
||
| avro_writer_free(writer); | ||
| avro_value_decref(&value); | ||
| return 0; | ||
| } | ||
|
|
||
| static int decode_as_string( | ||
| avro_value_iface_t *string_iface, | ||
| const void *buf, | ||
| size_t buf_size, | ||
| const char *label) | ||
| { | ||
| int rc; | ||
| avro_reader_t reader = NULL; | ||
| avro_value_t decoded; | ||
| const char *text = NULL; | ||
| size_t text_size = 0; | ||
|
|
||
| rc = avro_generic_value_new(string_iface, &decoded); | ||
| if (rc != 0) | ||
| { | ||
| fprintf(stderr, "%s: avro_generic_value_new failed: %s\n", | ||
| label, avro_strerror()); | ||
| return rc; | ||
| } | ||
|
|
||
| reader = avro_reader_memory((const char *)buf, buf_size); | ||
| if (reader == NULL) | ||
| { | ||
| fprintf(stderr, "%s: avro_reader_memory failed\n", label); | ||
| avro_value_decref(&decoded); | ||
| return -1; | ||
| } | ||
|
|
||
| rc = avro_value_read(reader, &decoded); | ||
| if (rc != 0) | ||
| { | ||
| fprintf(stderr, "%s: avro_value_read failed as expected? rc=%d err=%s\n", | ||
| label, rc, avro_strerror()); | ||
| avro_reader_free(reader); | ||
| avro_value_decref(&decoded); | ||
| return rc; | ||
| } | ||
|
|
||
| rc = avro_value_get_string(&decoded, &text, &text_size); | ||
| if (rc != 0) | ||
| { | ||
| fprintf(stderr, "%s: avro_value_get_string failed: %s\n", | ||
| label, avro_strerror()); | ||
| avro_reader_free(reader); | ||
| avro_value_decref(&decoded); | ||
| return rc; | ||
| } | ||
|
|
||
| printf("%s: decoded successfully: \"%s\" (%zu bytes)\n", | ||
| label, text, text_size); | ||
|
|
||
| avro_reader_free(reader); | ||
| avro_value_decref(&decoded); | ||
| return 0; | ||
| } | ||
|
|
||
| int main(void) | ||
| { | ||
| int rc; | ||
| int ret = EXIT_SUCCESS; | ||
|
|
||
| avro_schema_t string_schema = NULL; | ||
| avro_schema_t int_schema = NULL; | ||
|
|
||
| avro_value_iface_t *string_iface = NULL; | ||
| avro_value_iface_t *int_iface = NULL; | ||
|
|
||
| encoded_buf_t encoded_string; | ||
| encoded_buf_t encoded_int; | ||
|
|
||
| rc = avro_schema_from_json_literal(STRING_SCHEMA_LITERAL, &string_schema); | ||
| if (rc != 0) | ||
| { | ||
| fprintf(stderr, "Failed to parse string schema: %s\n", avro_strerror()); | ||
| return EXIT_FAILURE; | ||
| } | ||
|
|
||
| rc = avro_schema_from_json_literal(INT_SCHEMA_LITERAL, &int_schema); | ||
| if (rc != 0) | ||
| { | ||
| fprintf(stderr, "Failed to parse int schema: %s\n", avro_strerror()); | ||
| avro_schema_decref(string_schema); | ||
| return EXIT_FAILURE; | ||
| } | ||
|
|
||
| string_iface = avro_generic_class_from_schema(string_schema); | ||
| if (string_iface == NULL) | ||
| { | ||
| fprintf(stderr, "Failed to create string iface\n"); | ||
| avro_schema_decref(int_schema); | ||
| avro_schema_decref(string_schema); | ||
| return EXIT_FAILURE; | ||
| } | ||
|
|
||
| int_iface = avro_generic_class_from_schema(int_schema); | ||
| if (int_iface == NULL) | ||
| { | ||
| fprintf(stderr, "Failed to create int iface\n"); | ||
| avro_value_iface_decref(string_iface); | ||
| avro_schema_decref(int_schema); | ||
| avro_schema_decref(string_schema); | ||
| return EXIT_FAILURE; | ||
| } | ||
|
|
||
| rc = encode_int(int_iface, 12345, &encoded_int); | ||
| if (rc != 0) | ||
| { | ||
| fprintf(stderr, "Encoding int failed\n"); | ||
| avro_value_iface_decref(int_iface); | ||
| avro_value_iface_decref(string_iface); | ||
| avro_schema_decref(int_schema); | ||
| avro_schema_decref(string_schema); | ||
| return EXIT_FAILURE; | ||
| } | ||
|
|
||
| printf("Encoded int size: %zu bytes\n", encoded_int.size); | ||
|
|
||
| rc = decode_as_string(string_iface, | ||
| encoded_int.data, | ||
| encoded_int.size, | ||
| "int payload as string"); | ||
| if (rc == 0) | ||
| { | ||
| fprintf(stderr, "Unexpected success decoding int payload as string\n"); | ||
| ret = EXIT_FAILURE; | ||
| } | ||
| else | ||
| { | ||
| printf("int payload as string: failed cleanly, as expected\n"); | ||
| } | ||
|
|
||
| avro_value_iface_decref(int_iface); | ||
| avro_value_iface_decref(string_iface); | ||
| avro_schema_decref(int_schema); | ||
| avro_schema_decref(string_schema); | ||
|
|
||
| return ret; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this fix! I'm not a C expert, so I wanted to clarify the intended behaviour here. When
read_bytes()(orread_string()) returns an error, what should callers expect to find in the*bytesor*spointer? Will it beNULLbecause of macro expansion or not?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the feedback!
read_bytes()andread_string()are not part of the user API. They are called by theavro_value_read()which is part of the user API.The stack frame goes like this: user frame ->
avro_value_read()frame ->avro_read()frame.avro_readdispatches to the proper read procedure depending on the inner type of the value being constructed. In the dispatch branches that relate toavro_bytes()andavro_read()these calls are wrapped inside acheck_prefix(see read_bytes and read_string relevant source code lines) macro which short circuits in case of bad return code. On error, the pointers are not used.So to answer your questions:
what should callers expect to find in the
*bytesor*spointer?: Given that the pointers are not used, they don't need to be in any specific state. However, assigning the pointers toNULLis standard.Will it be NULL because of macro expansion or not?: Yes, in case of error the pointers are freed and set to
NULL.