@@ -709,11 +709,11 @@ static int flush_to_otel(struct opentelemetry_context *ctx,
709709 Opentelemetry__Proto__Logs__V1__ScopeLogs * scope_logs [1 ];
710710 Opentelemetry__Proto__Common__V1__KeyValue * * attributes_list ;
711711 Opentelemetry__Proto__Common__V1__KeyValue * attributes ;
712- size_t kv_size ;
712+ size_t kv_size = 0 ;
713713 size_t kv_index ;
714+ size_t index ;
714715 struct mk_list * kv_head ;
715716 struct flb_kv * kv ;
716- int index ;
717717
718718 void * body ;
719719 unsigned len ;
@@ -732,36 +732,48 @@ static int flush_to_otel(struct opentelemetry_context *ctx,
732732 resource_logs [0 ] = & resource_log ;
733733
734734 kv_size = mk_list_size (& (ctx -> kv_labels ));
735- attributes_list = flb_calloc (kv_size , sizeof (Opentelemetry__Proto__Common__V1__KeyValue * ));
736- if (attributes_list == NULL ) {
735+ if (kv_size > 0 ) {
736+ attributes_list = flb_calloc (kv_size , sizeof (Opentelemetry__Proto__Common__V1__KeyValue * ));
737+ if (attributes_list == NULL ) {
737738 flb_errno ();
738739 return -1 ;
739- }
740- attributes = flb_calloc (kv_size , sizeof (Opentelemetry__Proto__Common__V1__KeyValue ));
741- if (attributes == NULL ) {
740+ }
741+ attributes = flb_calloc (kv_size , sizeof (Opentelemetry__Proto__Common__V1__KeyValue ));
742+ if (attributes == NULL ) {
742743 flb_errno ();
744+ flb_free (attributes_list );
743745 return -1 ;
746+ }
747+ kv_index = 0 ;
748+ mk_list_foreach (kv_head , & ctx -> kv_labels ) {
749+ kv = mk_list_entry (kv_head , struct flb_kv , _head );
750+ opentelemetry__proto__common__v1__key_value__init (& attributes [kv_index ]);
751+ attributes [kv_index ].key = kv -> key ;
752+ attributes [kv_index ].value = otlp_any_value_initialize (MSGPACK_OBJECT_STR , 0 );
753+ if (attributes [kv_index ].value == NULL ) {
754+ flb_errno ();
755+ // cleanup previous allocated values
756+ for (index = 0 ; index < kv_index ; index ++ ) {
757+ flb_free (attributes [index ].value );
758+ }
759+ flb_free (attributes );
760+ flb_free (attributes_list );
761+ return -1 ;
762+ }
763+ attributes [kv_index ].value -> string_value = kv -> val ;
764+ attributes_list [kv_index ] = & attributes [kv_index ];
765+ kv_index ++ ;
766+ }
767+ resource_log .resource = flb_calloc (1 , sizeof (Opentelemetry__Proto__Resource__V1__Resource ));
768+ if (resource_log .resource == NULL ) {
769+ flb_errno ();
770+ res = -1 ;
771+ goto calloc_resource_fail ;
772+ }
773+ opentelemetry__proto__resource__v1__resource__init (resource_log .resource );
774+ resource_log .resource -> n_attributes = kv_size ;
775+ resource_log .resource -> attributes = attributes_list ;
744776 }
745- for (index = 0 ; index < kv_size ; index ++ ) {
746- attributes_list [index ] = & attributes [index ];
747- }
748-
749-
750- kv_index = 0 ;
751- mk_list_foreach (kv_head , & ctx -> kv_labels ) {
752- kv = mk_list_entry (kv_head , struct flb_kv , _head );
753- opentelemetry__proto__common__v1__key_value__init (& attributes [kv_index ]);
754- attributes [kv_index ].key = kv -> key ;
755- attributes [kv_index ].value = otlp_any_value_initialize (MSGPACK_OBJECT_STR , 0 );
756- attributes [kv_index ].value -> string_value = kv -> val ;
757- kv_index ++ ;
758- }
759-
760- resource_log .resource = flb_calloc (1 , sizeof (Opentelemetry__Proto__Resource__V1__Resource ));
761- opentelemetry__proto__resource__v1__resource__init (resource_log .resource );
762- resource_log .resource -> n_attributes = kv_size ;
763- resource_log .resource -> attributes = attributes_list ;
764-
765777 export_logs .resource_logs = resource_logs ;
766778 export_logs .n_resource_logs = 1 ;
767779
@@ -781,12 +793,15 @@ static int flush_to_otel(struct opentelemetry_context *ctx,
781793 ctx -> logs_uri );
782794
783795 flb_free (body );
784- flb_free ( resource_log . resource );
796+ calloc_resource_fail :
785797 for (index = 0 ; index < kv_size ; index ++ ) {
786798 flb_free (attributes [index ].value );
787799 }
788- flb_free (attributes );
789- flb_free (attributes_list );
800+ if (kv_size > 0 ) {
801+ flb_free (resource_log .resource );
802+ flb_free (attributes );
803+ flb_free (attributes_list );
804+ }
790805
791806 return res ;
792807}
0 commit comments