Skip to content

Commit 7e6b33b

Browse files
committed
pipeline: outputs: es: avoid zero pointer dereferencing when HTTP client creation failed
Signed-off-by: Marat Abrarov <abrarov@gmail.com>
1 parent 0e40530 commit 7e6b33b

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

plugins/out_es/es.c

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -835,15 +835,15 @@ static void cb_es_flush(struct flb_event_chunk *event_chunk,
835835
{
836836
int ret;
837837
size_t pack_size;
838-
char *pack;
839-
void *out_buf;
838+
char *pack = NULL;
839+
void *out_buf = NULL;
840840
size_t out_size;
841841
size_t b_sent;
842842
struct flb_elasticsearch *ctx = out_context;
843843
struct flb_elasticsearch_config *ec;
844844
struct flb_connection *u_conn;
845845
struct flb_upstream_node *node;
846-
struct flb_http_client *c;
846+
struct flb_http_client *c = NULL;
847847
flb_sds_t signature = NULL;
848848
int compressed = FLB_FALSE;
849849
flb_sds_t header_line = NULL;
@@ -911,6 +911,11 @@ static void cb_es_flush(struct flb_event_chunk *event_chunk,
911911
/* Compose HTTP Client request */
912912
c = flb_http_client(u_conn, FLB_HTTP_POST, ec->uri,
913913
pack, pack_size, NULL, 0, NULL, 0);
914+
if (!c) {
915+
flb_plg_error(ctx->ins, "failed to create HTTP client");
916+
ret = FLB_ERROR;
917+
goto cleanup_and_return;
918+
}
914919

915920
flb_http_buffer_size(c, ec->buffer_size);
916921

@@ -1028,29 +1033,24 @@ static void cb_es_flush(struct flb_event_chunk *event_chunk,
10281033
}
10291034
}
10301035

1036+
ret = FLB_OK;
1037+
1038+
cleanup_and_return:
10311039
/* Cleanup */
1032-
flb_http_client_destroy(c);
1040+
if (c) {
1041+
flb_http_client_destroy(c);
1042+
}
10331043
flb_free(pack);
10341044
flb_upstream_conn_release(u_conn);
10351045
if (signature) {
10361046
flb_sds_destroy(signature);
10371047
}
1038-
FLB_OUTPUT_RETURN(FLB_OK);
1048+
FLB_OUTPUT_RETURN(ret);
10391049

10401050
/* Issue a retry */
10411051
retry:
1042-
flb_http_client_destroy(c);
1043-
flb_free(pack);
1044-
1045-
if (out_buf != pack) {
1046-
flb_free(out_buf);
1047-
}
1048-
1049-
flb_upstream_conn_release(u_conn);
1050-
if (signature) {
1051-
flb_sds_destroy(signature);
1052-
}
1053-
FLB_OUTPUT_RETURN(FLB_RETRY);
1052+
ret = FLB_RETRY;
1053+
goto cleanup_and_return;
10541054
}
10551055

10561056
static int elasticsearch_response_test(struct flb_config *config,
@@ -1079,6 +1079,10 @@ static int elasticsearch_response_test(struct flb_config *config,
10791079
/* Compose HTTP Client request (dummy client) */
10801080
c = flb_http_dummy_client(u_conn, FLB_HTTP_POST, ec->uri,
10811081
NULL, 0, NULL, 0, NULL, 0);
1082+
if (!c) {
1083+
flb_plg_error(ctx->ins, "failed to create dummy HTTP client");
1084+
return -2;
1085+
}
10821086

10831087
flb_http_buffer_size(c, ec->buffer_size);
10841088

0 commit comments

Comments
 (0)