Skip to content

Commit f176701

Browse files
committed
log_event_decoder: add context extraction helpers
This patch adds helper functions to extract context variables from decoded log events. Signed-off-by: Eduardo Silva <[email protected]>
1 parent d843a0c commit f176701

File tree

1 file changed

+32
-17
lines changed

1 file changed

+32
-17
lines changed

src/flb_log_event_decoder.c

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,6 @@ void flb_log_event_decoder_destroy(struct flb_log_event_decoder *context)
150150

151151
if (context != NULL) {
152152
if (context->initialized) {
153-
if (context->unpacked_group_record.zone ==
154-
context->unpacked_event.zone) {
155-
msgpack_unpacked_init(&context->unpacked_event);
156-
}
157-
158153
msgpack_unpacked_destroy(&context->unpacked_group_record);
159154
msgpack_unpacked_destroy(&context->unpacked_empty_map);
160155
msgpack_unpacked_destroy(&context->unpacked_event);
@@ -331,11 +326,6 @@ int flb_log_event_decoder_next(struct flb_log_event_decoder *context,
331326
return context->last_result;
332327
}
333328

334-
if (context->unpacked_group_record.zone ==
335-
context->unpacked_event.zone) {
336-
msgpack_unpacked_init(&context->unpacked_event);
337-
}
338-
339329
previous_offset = context->offset;
340330
result = msgpack_unpack_next(&context->unpacked_event,
341331
context->buffer,
@@ -378,12 +368,38 @@ int flb_log_event_decoder_next(struct flb_log_event_decoder *context,
378368
msgpack_unpacked_destroy(&context->unpacked_group_record);
379369

380370
if (record_type == FLB_LOG_EVENT_GROUP_START) {
381-
memcpy(&context->unpacked_group_record,
382-
&context->unpacked_event,
383-
sizeof(msgpack_unpacked));
384-
385-
context->current_group_metadata = event->metadata;
386-
context->current_group_attributes = event->body;
371+
/*
372+
* Transfer zone ownership from unpacked_event to unpacked_group_record
373+
* instead of using memcpy. This prevents double-free issues when
374+
* both structures would otherwise reference the same zone.
375+
*/
376+
context->unpacked_group_record.zone = msgpack_unpacked_release_zone(&context->unpacked_event);
377+
context->unpacked_group_record.data = context->unpacked_event.data;
378+
379+
/*
380+
* Extract pointers from the transferred data structure to ensure they
381+
* remain valid. The pointers must come from unpacked_group_record.data
382+
* since that's where the zone (and the data) now reside.
383+
*/
384+
if (context->unpacked_group_record.data.type == MSGPACK_OBJECT_ARRAY &&
385+
context->unpacked_group_record.data.via.array.size == 2) {
386+
msgpack_object *header = &context->unpacked_group_record.data.via.array.ptr[0];
387+
msgpack_object *root_body = &context->unpacked_group_record.data.via.array.ptr[1];
388+
389+
if (header->type == MSGPACK_OBJECT_ARRAY &&
390+
header->via.array.size == 2) {
391+
context->current_group_metadata = &header->via.array.ptr[1];
392+
}
393+
else {
394+
context->current_group_metadata = context->empty_map;
395+
}
396+
context->current_group_attributes = root_body;
397+
}
398+
else {
399+
/* Fallback to using event pointers if structure is unexpected */
400+
context->current_group_metadata = event->metadata;
401+
context->current_group_attributes = event->body;
402+
}
387403
}
388404
else {
389405
context->current_group_metadata = NULL;
@@ -392,7 +408,6 @@ int flb_log_event_decoder_next(struct flb_log_event_decoder *context,
392408

393409
if (context->read_groups != FLB_TRUE) {
394410
memset(event, 0, sizeof(struct flb_log_event));
395-
396411
return flb_log_event_decoder_next(context, event);
397412
}
398413
}

0 commit comments

Comments
 (0)