Skip to content

Commit

Permalink
filter_record_modifier: dynamic allocate bool_map(fluent#3968)
Browse files Browse the repository at this point in the history
size of bool_map is fixed 128.
If map size of incoming record exceeds the array size,
it breaks stack.

This patch is to allocate bool_map dynamically.

Signed-off-by: Takahiro Yamashita <[email protected]>
  • Loading branch information
nokute78 committed Aug 21, 2021
1 parent 672c722 commit e0ba680
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion plugins/filter_record_modifier/filter_modifier.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ static int cb_modifier_filter(const void *data, size_t bytes,
int i;
int removed_map_num = 0;
int map_num = 0;
bool_map_t bool_map[128];
bool_map_t *bool_map = NULL;
(void) f_ins;
(void) config;
struct flb_time tm;
Expand All @@ -284,6 +284,11 @@ static int cb_modifier_filter(const void *data, size_t bytes,
while (msgpack_unpack_next(&result, data, bytes, &off) == MSGPACK_UNPACK_SUCCESS) {
map_num = 0;
removed_map_num = 0;
if (bool_map != NULL) {
flb_free(bool_map);
bool_map = NULL;
}

if (result.data.type != MSGPACK_OBJECT_ARRAY) {
continue;
}
Expand All @@ -293,6 +298,11 @@ static int cb_modifier_filter(const void *data, size_t bytes,
/* grep keys */
if (obj->type == MSGPACK_OBJECT_MAP) {
map_num = obj->via.map.size;
bool_map = flb_calloc(map_num, sizeof(bool_map_t));
if (bool_map == NULL) {
flb_errno();
return -1;
}
removed_map_num = make_bool_map(ctx, obj,
bool_map, obj->via.map.size);
}
Expand Down Expand Up @@ -320,6 +330,8 @@ static int cb_modifier_filter(const void *data, size_t bytes,
msgpack_pack_object(&tmp_pck, (kv+i)->val);
}
}
flb_free(bool_map);
bool_map = NULL;

/* append record */
if (ctx->records_num > 0) {
Expand All @@ -336,6 +348,9 @@ static int cb_modifier_filter(const void *data, size_t bytes,
}
}
msgpack_unpacked_destroy(&result);
if (bool_map != NULL) {
flb_free(bool_map);
}

if (is_modified != FLB_TRUE) {
/* Destroy the buffer to avoid more overhead */
Expand Down

0 comments on commit e0ba680

Please sign in to comment.