Skip to content

Commit 0ddb7f0

Browse files
committed
always clean up ctx object when error occurs
Signed-off-by: Hannes Schulz <Hannes.Schulz@microsoft.com>
1 parent 73c62d3 commit 0ddb7f0

1 file changed

Lines changed: 28 additions & 28 deletions

File tree

plugins/out_azure_blob/azure_blob_conf.c

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -563,29 +563,25 @@ struct flb_azure_blob *flb_azure_blob_conf_create(struct flb_output_instance *in
563563
/* Load config map */
564564
ret = flb_output_config_map_set(ins, (void *) ctx);
565565
if (ret == -1) {
566-
flb_free(ctx);
567-
568-
return NULL;
566+
goto error;
569567
}
570568

571569
if (ctx->account_name == NULL) {
572570
flb_plg_error(ctx->ins, "'account_name' has not been set");
573-
return NULL;
571+
goto error;
574572
}
575573

576574
if (ctx->configuration_endpoint_url != NULL) {
577575
ret = flb_azure_blob_apply_remote_configuration(ctx);
578576

579577
if (ret != 0) {
580-
flb_free(ctx);
581-
582-
return NULL;
578+
goto error;
583579
}
584580
}
585581

586582
if (!ctx->container_name) {
587583
flb_plg_error(ctx->ins, "'container_name' has not been set");
588-
return NULL;
584+
goto error;
589585
}
590586

591587
/* Set Auth type */
@@ -605,7 +601,7 @@ struct flb_azure_blob *flb_azure_blob_conf_create(struct flb_output_instance *in
605601
flb_plg_error(ctx->ins,
606602
"managed_identity auth requires 'client_id' "
607603
"(set to 'system' for system-assigned)");
608-
return NULL;
604+
goto error;
609605
}
610606
if (strcasecmp(ctx->client_id, "system") == 0) {
611607
ctx->atype = AZURE_BLOB_AUTH_MI_SYSTEM;
@@ -620,24 +616,24 @@ struct flb_azure_blob *flb_azure_blob_conf_create(struct flb_output_instance *in
620616
flb_plg_error(ctx->ins,
621617
"workload_identity auth requires "
622618
"'tenant_id' and 'client_id'");
623-
return NULL;
619+
goto error;
624620
}
625621
}
626622
else {
627623
flb_plg_error(ctx->ins, "invalid auth_type value '%s'", tmp);
628-
return NULL;
624+
goto error;
629625
}
630626
}
631627
if (ctx->atype == AZURE_BLOB_AUTH_KEY &&
632628
ctx->shared_key == NULL) {
633629
flb_plg_error(ctx->ins, "'shared_key' has not been set");
634-
return NULL;
630+
goto error;
635631
}
636632

637633
if (ctx->atype == AZURE_BLOB_AUTH_SAS) {
638634
if (ctx->sas_token == NULL) {
639635
flb_plg_error(ctx->ins, "'sas_token' has not been set");
640-
return NULL;
636+
goto error;
641637
}
642638
if (ctx->sas_token[0] == '?') {
643639
ctx->sas_token++;
@@ -649,7 +645,7 @@ struct flb_azure_blob *flb_azure_blob_conf_create(struct flb_output_instance *in
649645
ctx->shared_key != NULL) {
650646
ret = set_shared_key(ctx);
651647
if (ret == -1) {
652-
return NULL;
648+
goto error;
653649
}
654650
}
655651

@@ -667,7 +663,7 @@ struct flb_azure_blob *flb_azure_blob_conf_create(struct flb_output_instance *in
667663
}
668664
else {
669665
flb_plg_error(ctx->ins, "invalid blob_type value '%s'", tmp);
670-
return NULL;
666+
goto error;
671667
}
672668
}
673669

@@ -676,7 +672,7 @@ struct flb_azure_blob *flb_azure_blob_conf_create(struct flb_output_instance *in
676672
flb_plg_error(ctx->ins,
677673
"buffering is not supported with 'appendblob' blob_type. "
678674
"Please use 'blockblob' blob_type or disable buffering.");
679-
return NULL;
675+
goto error;
680676
}
681677

682678
/* Compress (gzip) */
@@ -693,7 +689,7 @@ struct flb_azure_blob *flb_azure_blob_conf_create(struct flb_output_instance *in
693689
flb_plg_error(ctx->ins,
694690
"the option 'compress_blob' is not compatible with 'appendblob' "
695691
"blob_type");
696-
return NULL;
692+
goto error;
697693
}
698694

699695
/*
@@ -714,15 +710,15 @@ struct flb_azure_blob *flb_azure_blob_conf_create(struct flb_output_instance *in
714710
io_flags, ins->tls);
715711
if (!ctx->u) {
716712
flb_plg_error(ctx->ins, "invalid endpoint '%s'", ctx->endpoint);
717-
return NULL;
713+
goto error;
718714
}
719715
ctx->real_endpoint = flb_sds_create(ctx->endpoint);
720716
}
721717
else {
722718
ctx->real_endpoint = flb_sds_create_size(256);
723719
if (!ctx->real_endpoint) {
724720
flb_plg_error(ctx->ins, "cannot create endpoint");
725-
return NULL;
721+
goto error;
726722
}
727723
flb_sds_printf(&ctx->real_endpoint, "%s%s",
728724
ctx->account_name,
@@ -749,7 +745,7 @@ struct flb_azure_blob *flb_azure_blob_conf_create(struct flb_output_instance *in
749745
if (!ctx->u) {
750746
flb_plg_error(ctx->ins, "cannot create upstream for endpoint '%s'",
751747
ctx->real_endpoint);
752-
return NULL;
748+
goto error;
753749
}
754750
}
755751
flb_output_upstream_set(ctx->u, ins);
@@ -759,7 +755,7 @@ struct flb_azure_blob *flb_azure_blob_conf_create(struct flb_output_instance *in
759755
if (!ctx->base_uri) {
760756
flb_plg_error(ctx->ins, "cannot create base_uri for endpoint '%s'",
761757
ctx->real_endpoint);
762-
return NULL;
758+
goto error;
763759
}
764760

765761
if (ctx->emulator_mode == FLB_TRUE) {
@@ -774,7 +770,7 @@ struct flb_azure_blob *flb_azure_blob_conf_create(struct flb_output_instance *in
774770
ctx->shared_key_prefix = flb_sds_create_size(256);
775771
if (!ctx->shared_key_prefix) {
776772
flb_plg_error(ctx->ins, "cannot create shared key prefix");
777-
return NULL;
773+
goto error;
778774
}
779775
flb_sds_printf(&ctx->shared_key_prefix, "SharedKey %s:", ctx->account_name);
780776
}
@@ -787,7 +783,7 @@ struct flb_azure_blob *flb_azure_blob_conf_create(struct flb_output_instance *in
787783
ctx->oauth_url = flb_sds_create_size(
788784
sizeof(FLB_AZURE_BLOB_MSIAUTH_URL_TEMPLATE) + 1);
789785
if (!ctx->oauth_url) {
790-
return NULL;
786+
goto error;
791787
}
792788
flb_sds_snprintf(&ctx->oauth_url, flb_sds_alloc(ctx->oauth_url),
793789
FLB_AZURE_BLOB_MSIAUTH_URL_TEMPLATE, "", "");
@@ -797,7 +793,7 @@ struct flb_azure_blob *flb_azure_blob_conf_create(struct flb_output_instance *in
797793
sizeof(FLB_AZURE_BLOB_MSIAUTH_URL_TEMPLATE) +
798794
sizeof("&client_id=") + flb_sds_len(ctx->client_id));
799795
if (!ctx->oauth_url) {
800-
return NULL;
796+
goto error;
801797
}
802798
flb_sds_snprintf(&ctx->oauth_url, flb_sds_alloc(ctx->oauth_url),
803799
FLB_AZURE_BLOB_MSIAUTH_URL_TEMPLATE,
@@ -807,7 +803,7 @@ struct flb_azure_blob *flb_azure_blob_conf_create(struct flb_output_instance *in
807803
ctx->o = flb_oauth2_create(config, ctx->oauth_url, 3000);
808804
if (!ctx->o) {
809805
flb_plg_error(ctx->ins, "cannot create OAuth2 context for IMDS");
810-
return NULL;
806+
goto error;
811807
}
812808
flb_stream_disable_async_mode(&ctx->o->u->base);
813809
pthread_mutex_init(&ctx->token_mutex, NULL);
@@ -818,15 +814,15 @@ struct flb_azure_blob *flb_azure_blob_conf_create(struct flb_output_instance *in
818814
sizeof(FLB_AZURE_BLOB_MSAL_AUTH_URL_TEMPLATE) +
819815
flb_sds_len(ctx->tenant_id));
820816
if (!ctx->oauth_url) {
821-
return NULL;
817+
goto error;
822818
}
823819
flb_sds_snprintf(&ctx->oauth_url, flb_sds_alloc(ctx->oauth_url),
824820
FLB_AZURE_BLOB_MSAL_AUTH_URL_TEMPLATE, ctx->tenant_id);
825821

826822
ctx->o = flb_oauth2_create(config, ctx->oauth_url, 3000);
827823
if (!ctx->o) {
828824
flb_plg_error(ctx->ins, "cannot create OAuth2 context for workload identity");
829-
return NULL;
825+
goto error;
830826
}
831827
flb_stream_disable_async_mode(&ctx->o->u->base);
832828
pthread_mutex_init(&ctx->token_mutex, NULL);
@@ -843,7 +839,7 @@ struct flb_azure_blob *flb_azure_blob_conf_create(struct flb_output_instance *in
843839
if (ctx->database_file) {
844840
ctx->db = azb_db_open(ctx, ctx->database_file);
845841
if (!ctx->db) {
846-
return NULL;
842+
goto error;
847843
}
848844
}
849845

@@ -861,6 +857,10 @@ struct flb_azure_blob *flb_azure_blob_conf_create(struct flb_output_instance *in
861857
ctx->atype == AZURE_BLOB_AUTH_MI_USER ? "managed_identity (user)" :
862858
"workload_identity");
863859
return ctx;
860+
861+
error:
862+
flb_azure_blob_conf_destroy(ctx);
863+
return NULL;
864864
}
865865

866866
void flb_azure_blob_conf_destroy(struct flb_azure_blob *ctx)

0 commit comments

Comments
 (0)