Skip to content

Commit

Permalink
filter_modify: fix error handling after flb_log_event_decoder_next
Browse files Browse the repository at this point in the history
Signed-off-by: Takahiro Yamashita <[email protected]>
  • Loading branch information
nokute78 committed Dec 15, 2023
1 parent 2612a1a commit f781a20
Showing 1 changed file with 44 additions and 39 deletions.
83 changes: 44 additions & 39 deletions plugins/filter_modify/modify.c
Original file line number Diff line number Diff line change
Expand Up @@ -1492,75 +1492,80 @@ static int cb_modify_filter(const void *data, size_t bytes,
struct filter_modify_ctx *ctx = context;
int modifications = 0;
int total_modifications = 0;
int ret;
int ret = FLB_FILTER_NOTOUCH;
int dec_ret;
int enc_ret;

(void) f_ins;
(void) i_ins;
(void) config;

ret = flb_log_event_decoder_init(&log_decoder, (char *) data, bytes);

if (ret != FLB_EVENT_DECODER_SUCCESS) {
dec_ret = flb_log_event_decoder_init(&log_decoder, (char *) data, bytes);
if (dec_ret != FLB_EVENT_DECODER_SUCCESS) {
flb_plg_error(ctx->ins,
"Log event decoder initialization error : %d", ret);

"Log event decoder initialization error : %s",
flb_log_event_decoder_get_error_description(dec_ret));
return FLB_FILTER_NOTOUCH;
}

ret = flb_log_event_encoder_init(&log_encoder,
FLB_LOG_EVENT_FORMAT_DEFAULT);

if (ret != FLB_EVENT_ENCODER_SUCCESS) {
enc_ret = flb_log_event_encoder_init(&log_encoder,
FLB_LOG_EVENT_FORMAT_DEFAULT);
if (enc_ret != FLB_EVENT_ENCODER_SUCCESS) {
flb_plg_error(ctx->ins,
"Log event encoder initialization error : %d", ret);

"Log event encoder initialization error : %s",
flb_log_event_encoder_get_error_description(enc_ret));
flb_log_event_decoder_destroy(&log_decoder);

return FLB_FILTER_NOTOUCH;
}

while ((ret = flb_log_event_decoder_next(
&log_decoder,
&log_event)) == FLB_EVENT_DECODER_SUCCESS) {
while ((dec_ret = flb_log_event_decoder_next(
&log_decoder,
&log_event)) == FLB_EVENT_DECODER_SUCCESS) {
modifications =
apply_modifying_rules(&log_encoder, &log_event, ctx);

if (modifications == 0) {
/* not matched, so copy original event. */
ret = flb_log_event_encoder_emit_raw_record(
&log_encoder,
log_decoder.record_base,
log_decoder.record_length);
enc_ret = flb_log_event_encoder_emit_raw_record(
&log_encoder,
log_decoder.record_base,
log_decoder.record_length);
}

total_modifications += modifications;
}

if(total_modifications > 0) {
if (ret == FLB_EVENT_DECODER_ERROR_INSUFFICIENT_DATA &&
log_decoder.offset == bytes) {
ret = FLB_EVENT_ENCODER_SUCCESS;
}

if (ret == FLB_EVENT_ENCODER_SUCCESS) {
*out_buf = log_encoder.output_buffer;
*out_size = log_encoder.output_length;
if (total_modifications <= 0) {
ret = FLB_FILTER_NOTOUCH;
goto cb_modify_filter_end;
}

ret = FLB_FILTER_MODIFIED;
dec_ret = flb_log_event_decoder_get_last_result(&log_decoder);
if (dec_ret != FLB_EVENT_DECODER_SUCCESS) {
flb_plg_error(ctx->ins,
"Log event decoder error : %s",
flb_log_event_decoder_get_error_description(dec_ret));
ret = FLB_FILTER_NOTOUCH;
goto cb_modify_filter_end;
}

flb_log_event_encoder_claim_internal_buffer_ownership(&log_encoder);
}
else {
flb_plg_error(ctx->ins,
"Log event encoder error : %d", ret);
if (enc_ret != FLB_EVENT_ENCODER_SUCCESS) {
flb_plg_error(ctx->ins,
"Log event encoder error : %s",
flb_log_event_encoder_get_error_description(enc_ret));

ret = FLB_FILTER_NOTOUCH;
}
}
else {
ret = FLB_FILTER_NOTOUCH;
goto cb_modify_filter_end;
}

*out_buf = log_encoder.output_buffer;
*out_size = log_encoder.output_length;

ret = FLB_FILTER_MODIFIED;

flb_log_event_encoder_claim_internal_buffer_ownership(&log_encoder);

cb_modify_filter_end:
flb_log_event_decoder_destroy(&log_decoder);
flb_log_event_encoder_destroy(&log_encoder);

Expand Down

0 comments on commit f781a20

Please sign in to comment.