2222#include <fluent-bit/flb_regex.h>
2323
2424#include "cm.h"
25+ #include "cm_config.h"
2526
2627static int set_action (struct content_modifier_ctx * ctx )
2728{
@@ -146,6 +147,11 @@ static int set_context(struct content_modifier_ctx *ctx)
146147 /* check that 'name' is the key set */
147148 if (!ctx -> key ) {
148149 ctx -> key = flb_sds_create ("name" );
150+ if (!ctx -> key ) {
151+ flb_errno ();
152+ return -1 ;
153+ }
154+ ctx -> key_is_autogenerated = FLB_TRUE ;
149155 }
150156 else if (strcasecmp (ctx -> key , "name" ) != 0 ) {
151157 flb_plg_error (ctx -> ins , "context '%s' requires the name of the key to be 'name', no '%s'" ,
@@ -182,6 +188,11 @@ static int set_context(struct content_modifier_ctx *ctx)
182188 /* check that 'version' is the key set */
183189 if (!ctx -> key ) {
184190 ctx -> key = flb_sds_create ("version" );
191+ if (!ctx -> key ) {
192+ flb_errno ();
193+ return -1 ;
194+ }
195+ ctx -> key_is_autogenerated = FLB_TRUE ;
185196 }
186197 else if (strcasecmp (ctx -> key , "version" ) != 0 ) {
187198 flb_plg_error (ctx -> ins , "context '%s' requires the name of the key to be 'version', no '%s'" ,
@@ -256,6 +267,11 @@ static int set_context(struct content_modifier_ctx *ctx)
256267 /* check that 'name' is the key set */
257268 if (!ctx -> key ) {
258269 ctx -> key = flb_sds_create ("name" );
270+ if (!ctx -> key ) {
271+ flb_errno ();
272+ return -1 ;
273+ }
274+ ctx -> key_is_autogenerated = FLB_TRUE ;
259275 }
260276 else if (strcasecmp (ctx -> key , "name" ) != 0 ) {
261277 flb_plg_error (ctx -> ins , "context '%s' requires the name of the key to be 'name', no '%s'" ,
@@ -356,27 +372,27 @@ struct content_modifier_ctx *cm_config_create(struct flb_processor_instance *ins
356372 /* Initialize the config map */
357373 ret = flb_processor_instance_config_map_set (ins , ctx );
358374 if (ret == -1 ) {
359- flb_free (ctx );
375+ cm_config_destroy (ctx );
360376 return NULL ;
361377 }
362378
363379 if (!ctx -> action_str ) {
364380 flb_plg_error (ctx -> ins , "no 'action' defined" );
365- flb_free (ctx );
381+ cm_config_destroy (ctx );
366382 return NULL ;
367383 }
368384
369385 /* process the 'action' configuration */
370386 ret = set_action (ctx );
371387 if (ret == -1 ) {
372- flb_free (ctx );
388+ cm_config_destroy (ctx );
373389 return NULL ;
374390 }
375391
376392 /* process the 'context' where the action will be applied */
377393 ret = set_context (ctx );
378394 if (ret == -1 ) {
379- flb_free (ctx );
395+ cm_config_destroy (ctx );
380396 return NULL ;
381397 }
382398
@@ -385,25 +401,33 @@ struct content_modifier_ctx *cm_config_create(struct flb_processor_instance *ins
385401 ctx -> regex = flb_regex_create (ctx -> pattern );
386402 if (!ctx -> regex ) {
387403 flb_plg_error (ctx -> ins , "invalid regex pattern '%s'" , ctx -> pattern );
388- flb_free (ctx );
404+ cm_config_destroy (ctx );
389405 return NULL ;
390406 }
391407 }
392408
393409 /* Certain actions needs extra configuration, e.g: insert -> requires a key and a value */
394410 ret = check_action_requirements (ctx );
395411 if (ret == -1 ) {
396- flb_free (ctx );
412+ cm_config_destroy (ctx );
397413 return NULL ;
398414 }
399415 return ctx ;
400416}
401417
402418void cm_config_destroy (struct content_modifier_ctx * ctx )
403419{
420+ if (ctx == NULL ) {
421+ return ;
422+ }
423+
404424 if (ctx -> regex ) {
405425 flb_regex_destroy (ctx -> regex );
406426 }
407427
428+ if (ctx -> key_is_autogenerated == FLB_TRUE && ctx -> key != NULL ) {
429+ flb_sds_destroy (ctx -> key );
430+ }
431+
408432 flb_free (ctx );
409433}
0 commit comments