Skip to content

Commit

Permalink
aws_util: memory fixes
Browse files Browse the repository at this point in the history
aws_util: always use flb_calloc, never flb_malloc

Signed-off-by: Wesley Pettit <[email protected]>

out_s3: always use flb_calloc, never flb_malloc

Signed-off-by: Wesley Pettit <[email protected]>

aws_util: fix tmp leak and fix formatting

Signed-off-by: David Korczynski <[email protected]>

config: fix possible null-dereference

flb_env_create can fail which causes config->env to be NULL, and this
can cause NULL-dereferences further in the process. Bail if environment
creation failed. Consequently, we need to add a safeguard for cleaning
up multiline parsers as these have not been initialized yet.

Signed-off-by: David Korczynski <[email protected]>
  • Loading branch information
PettitWesley authored and FireLens Team committed Aug 20, 2024
1 parent 7b35f05 commit be7b387
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
10 changes: 5 additions & 5 deletions plugins/out_s3/s3.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ static char *mock_error_response(char *error_env_var)

err_val = getenv(error_env_var);
if (err_val != NULL && strlen(err_val) > 0) {
error = flb_malloc(strlen(err_val) + sizeof(char));
error = flb_calloc(strlen(err_val) + 1, sizeof(char));
if (error == NULL) {
flb_errno();
return NULL;
Expand Down Expand Up @@ -156,7 +156,7 @@ int create_headers(struct flb_s3 *ctx, char *body_md5,
return 0;
}

s3_headers = flb_malloc(sizeof(struct flb_aws_header) * headers_len);
s3_headers = flb_calloc(headers_len, sizeof(struct flb_aws_header));
if (s3_headers == NULL) {
flb_errno();
return -1;
Expand Down Expand Up @@ -241,7 +241,7 @@ struct flb_http_client *mock_s3_call(char *error_env_var, char *api)
"Server: AmazonS3";
/* since etag is in the headers, this code uses resp.data */
len = strlen(resp);
c->resp.data = flb_malloc(len + 1);
c->resp.data = flb_calloc(len + 1, sizeof(char));
if (!c->resp.data) {
flb_errno();
return NULL;
Expand Down Expand Up @@ -1558,7 +1558,7 @@ static int add_to_queue(struct flb_s3 *ctx, struct s3_file *upload_file,
flb_sds_t tag_cpy;

/* Create upload contents object and add to upload queue */
upload_contents = flb_malloc(sizeof(struct upload_queue));
upload_contents = flb_calloc(1, sizeof(struct upload_queue));
if (upload_contents == NULL) {
flb_plg_error(ctx->ins, "Error allocating memory for upload_queue entry");
flb_errno();
Expand Down Expand Up @@ -1888,7 +1888,7 @@ static flb_sds_t flb_pack_msgpack_extract_log_key(void *out_context, const char
}

/* Allocate buffer to store log_key contents */
val_buf = flb_malloc(msgpack_size);
val_buf = flb_calloc(1, msgpack_size);
if (val_buf == NULL) {
flb_plg_error(ctx->ins, "Could not allocate enough "
"memory to read record");
Expand Down
11 changes: 7 additions & 4 deletions src/aws/flb_aws_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ char *flb_aws_endpoint(char* service, char* region)
len += strlen(region);
len++; /* null byte */

endpoint = flb_malloc(len);
endpoint = flb_calloc(len, sizeof(char));
if (!endpoint) {
flb_errno();
return NULL;
Expand Down Expand Up @@ -136,7 +136,7 @@ int flb_read_file(const char *path, char **out_buf, size_t *out_size)
return -1;
}

buf = flb_malloc(st.st_size + sizeof(char));
buf = flb_calloc(st.st_size + 1, sizeof(char));
if (!buf) {
flb_errno();
close(fd);
Expand Down Expand Up @@ -851,15 +851,18 @@ flb_sds_t flb_get_s3_key(const char *format, time_t time, const char *tag,
/* Find all occurences of $INDEX and replace with the appropriate index. */
if (strstr((char *) format, INDEX_STRING)) {
seq_index_len = snprintf(NULL, 0, "%"PRIu64, seq_index);
seq_index_str = flb_malloc(seq_index_len + 1);
seq_index_str = flb_calloc(seq_index_len + 1, sizeof(char));
if (seq_index_str == NULL) {
goto error;
}

sprintf(seq_index_str, "%"PRIu64, seq_index);
seq_index_str[seq_index_len] = '\0';
tmp_key = replace_uri_tokens(s3_key, INDEX_STRING, seq_index_str);

if (tmp_key == NULL) {
flb_free(seq_index_str);
goto error;
}
if (strlen(tmp_key) > S3_KEY_SIZE) {
flb_warn("[s3_key] Object key length is longer than the 1024 character limit.");
}
Expand Down
5 changes: 5 additions & 0 deletions src/flb_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,11 @@ struct flb_config *flb_config_init()

/* Environment */
config->env = flb_env_create();
if (!config->env) {
flb_error("[config] environment creation failed");
flb_config_exit(config);
return NULL;
}

/* Multiline core */
mk_list_init(&config->multiline_parsers);
Expand Down
5 changes: 5 additions & 0 deletions src/multiline/flb_ml_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,11 @@ void flb_ml_parser_destroy_all(struct mk_list *list)
struct mk_list *head;
struct flb_ml_parser *parser;

/* Ensure list is initialized */
if (list->next == NULL) {
return;
}

mk_list_foreach_safe(head, tmp, list) {
parser = mk_list_entry(head, struct flb_ml_parser, _head);
flb_ml_parser_destroy(parser);
Expand Down

0 comments on commit be7b387

Please sign in to comment.