Skip to content

Commit 425d247

Browse files
nokute78edsiper
authored andcommitted
filter_record_modifier: dynamic allocate bool_map(#3968)
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]>
1 parent a5b565f commit 425d247

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

plugins/filter_record_modifier/filter_modifier.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ static int cb_modifier_filter(const void *data, size_t bytes,
262262
int i;
263263
int removed_map_num = 0;
264264
int map_num = 0;
265-
bool_map_t bool_map[128];
265+
bool_map_t *bool_map = NULL;
266266
(void) f_ins;
267267
(void) config;
268268
struct flb_time tm;
@@ -284,6 +284,11 @@ static int cb_modifier_filter(const void *data, size_t bytes,
284284
while (msgpack_unpack_next(&result, data, bytes, &off) == MSGPACK_UNPACK_SUCCESS) {
285285
map_num = 0;
286286
removed_map_num = 0;
287+
if (bool_map != NULL) {
288+
flb_free(bool_map);
289+
bool_map = NULL;
290+
}
291+
287292
if (result.data.type != MSGPACK_OBJECT_ARRAY) {
288293
continue;
289294
}
@@ -293,6 +298,12 @@ static int cb_modifier_filter(const void *data, size_t bytes,
293298
/* grep keys */
294299
if (obj->type == MSGPACK_OBJECT_MAP) {
295300
map_num = obj->via.map.size;
301+
/* allocate map_num + guard byte */
302+
bool_map = flb_calloc(map_num+1, sizeof(bool_map_t));
303+
if (bool_map == NULL) {
304+
flb_errno();
305+
return -1;
306+
}
296307
removed_map_num = make_bool_map(ctx, obj,
297308
bool_map, obj->via.map.size);
298309
}
@@ -320,6 +331,8 @@ static int cb_modifier_filter(const void *data, size_t bytes,
320331
msgpack_pack_object(&tmp_pck, (kv+i)->val);
321332
}
322333
}
334+
flb_free(bool_map);
335+
bool_map = NULL;
323336

324337
/* append record */
325338
if (ctx->records_num > 0) {
@@ -336,6 +349,9 @@ static int cb_modifier_filter(const void *data, size_t bytes,
336349
}
337350
}
338351
msgpack_unpacked_destroy(&result);
352+
if (bool_map != NULL) {
353+
flb_free(bool_map);
354+
}
339355

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

0 commit comments

Comments
 (0)