From: Florian Forster Date: Fri, 2 Feb 2024 09:52:21 +0000 (+0100) Subject: format_stackdriver: Add proper error handling to `sd_output_reset()`. X-Git-Tag: collectd-6.0.0.rc2~7^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fpull%2F4270%2Fhead;p=thirdparty%2Fcollectd.git format_stackdriver: Add proper error handling to `sd_output_reset()`. --- diff --git a/src/utils/format_stackdriver/format_stackdriver.c b/src/utils/format_stackdriver/format_stackdriver.c index eb0363b58..b008ce44b 100644 --- a/src/utils/format_stackdriver/format_stackdriver.c +++ b/src/utils/format_stackdriver/format_stackdriver.c @@ -601,15 +601,32 @@ int sd_output_register_metric(sd_output_t *out, metric_t const *m) { char *sd_output_reset(sd_output_t *out) /* {{{ */ { - sd_output_finalize(out); + int err = sd_output_finalize(out); + if (err) { + goto handle_err; + } unsigned char const *json_buffer = NULL; - yajl_gen_get_buf(out->gen, &json_buffer, &(size_t){0}); + size_t json_buffer_size = 0; + err = yajl_gen_get_buf(out->gen, &json_buffer, &json_buffer_size); + if (err) { + goto handle_err; + } + char *ret = strdup((void const *)json_buffer); + if (ret == NULL) { + err = errno; + goto handle_err; + } + /* success */ reset(out); - return ret; + +handle_err: + reset(out); + errno = err; + return NULL; } /* }}} char *sd_output_reset */ sd_resource_t *sd_resource_create(char const *type) /* {{{ */ diff --git a/src/utils/format_stackdriver/format_stackdriver.h b/src/utils/format_stackdriver/format_stackdriver.h index 1a5979f8b..557723c43 100644 --- a/src/utils/format_stackdriver/format_stackdriver.h +++ b/src/utils/format_stackdriver/format_stackdriver.h @@ -60,7 +60,7 @@ int sd_output_register_metric(sd_output_t *out, metric_t const *m); /* sd_output_reset resets the output and returns the previous content of the * buffer. It is the caller's responsibility to call free() with the returned - * pointer. */ + * pointer. On error errno is set and NULL is returned. */ char *sd_output_reset(sd_output_t *out); sd_resource_t *sd_resource_create(char const *type); diff --git a/src/write_stackdriver.c b/src/write_stackdriver.c index 70d69dadf..4d224f615 100644 --- a/src/write_stackdriver.c +++ b/src/write_stackdriver.c @@ -342,6 +342,11 @@ static int wg_flush_nolock(cdtime_t timeout, wg_callback_t *cb) /* {{{ */ } char *payload = sd_output_reset(cb->formatter); + if (payload == NULL) { + ERROR("write_stackdriver plugin: sd_output_reset failed: %s", STRERRNO); + return errno; + } + int status = wg_call_timeseries_write(cb, payload); wg_reset_buffer(cb); return status;