Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scan: coverity fixes 20241012 #9488

Merged
merged 8 commits into from
Oct 14, 2024
2 changes: 1 addition & 1 deletion lib/cmetrics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# CMetrics Version
set(CMT_VERSION_MAJOR 0)
set(CMT_VERSION_MINOR 9)
set(CMT_VERSION_PATCH 7)
set(CMT_VERSION_PATCH 8)
set(CMT_VERSION_STR "${CMT_VERSION_MAJOR}.${CMT_VERSION_MINOR}.${CMT_VERSION_PATCH}")

# Include helpers
Expand Down
1 change: 1 addition & 0 deletions lib/cmetrics/include/cmetrics/cmt_decode_prometheus.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#define CMT_DECODE_PROMETHEUS_CMT_CREATE_ERROR 50
#define CMT_DECODE_PROMETHEUS_PARSE_VALUE_FAILED 60
#define CMT_DECODE_PROMETHEUS_PARSE_TIMESTAMP_FAILED 70
#define CMT_DECODE_PROMETHEUS_SAMPLE_VALUE_TOO_LONG 80

#define CMT_DECODE_PROMETHEUS_MAX_LABEL_COUNT 128

Expand Down
4 changes: 2 additions & 2 deletions lib/cmetrics/src/cmt_cat.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ int cmt_cat_copy_label_keys(struct cmt_map *map, char **out)

/* labels array */
s = map->label_count;
if (s == 0) {
if (s <= 0) {
*out = NULL;
return 0;
}
Expand Down Expand Up @@ -242,7 +242,7 @@ static inline int cmt_opts_compare(struct cmt_opts *a, struct cmt_opts *b)
{
int ret;

ret = strcmp(a->ns, a->ns);
ret = strcmp(a->ns, b->ns);
if (ret != 0) {
return ret;
}
Expand Down
10 changes: 5 additions & 5 deletions lib/cmetrics/src/cmt_decode_msgpack.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,15 @@ static int unpack_opts(mpack_reader_t *reader, struct cmt_opts *opts)
}

if (cfl_sds_len(opts->ns) > 0) {
cfl_sds_cat(opts->fqname, opts->ns, cfl_sds_len(opts->ns));
cfl_sds_cat(opts->fqname, "_", 1);
cfl_sds_cat_safe(&opts->fqname, opts->ns, cfl_sds_len(opts->ns));
cfl_sds_cat_safe(&opts->fqname, "_", 1);
}

if (cfl_sds_len(opts->subsystem) > 0) {
cfl_sds_cat(opts->fqname, opts->subsystem, cfl_sds_len(opts->subsystem));
cfl_sds_cat(opts->fqname, "_", 1);
cfl_sds_cat_safe(&opts->fqname, opts->subsystem, cfl_sds_len(opts->subsystem));
cfl_sds_cat_safe(&opts->fqname, "_", 1);
}
cfl_sds_cat(opts->fqname, opts->name, cfl_sds_len(opts->name));
cfl_sds_cat_safe(&opts->fqname, opts->name, cfl_sds_len(opts->name));
}

return result;
Expand Down
58 changes: 47 additions & 11 deletions lib/cmetrics/src/cmt_decode_prometheus.c
Original file line number Diff line number Diff line change
Expand Up @@ -456,14 +456,22 @@ static int add_metric_histogram(struct cmt_decode_prometheus_context *context)
cfl_sds_t *labels_without_le = NULL;
cfl_sds_t *values_without_le = NULL;
int label_i;
uint64_t timestamp;
uint64_t timestamp = 0;

if (cfl_list_size(&context->metric.samples) < 3) {
return report_error(context,
CMT_DECODE_PROMETHEUS_SYNTAX_ERROR,
"not enough samples for histogram");
}

/* bucket_count = sample count - 3:
* - "Inf" bucket
* - sum
* - count */
bucket_count = cfl_list_size(&context->metric.samples) - 3;
timestamp = context->opts.override_timestamp;
if (context->opts.override_timestamp) {
timestamp = context->opts.override_timestamp;
}

bucket_defaults = calloc(bucket_count + 1, sizeof(*bucket_defaults));
if (!bucket_defaults) {
Expand Down Expand Up @@ -627,6 +635,7 @@ static int add_metric_histogram(struct cmt_decode_prometheus_context *context)
context->current.histogram = h;
}


if (cmt_histogram_set_default(h, timestamp, bucket_defaults, sum, count,
label_i,
label_i ? values_without_le : NULL)) {
Expand Down Expand Up @@ -661,7 +670,7 @@ static int add_metric_summary(struct cmt_decode_prometheus_context *context)
size_t quantile_index;
double *quantiles = NULL;
double *quantile_defaults = NULL;
double sum;
double sum = 0.0;
double count_dbl;
size_t label_count;
uint64_t count = 0;
Expand All @@ -673,13 +682,21 @@ static int add_metric_summary(struct cmt_decode_prometheus_context *context)
cfl_sds_t *labels_without_quantile = NULL;
cfl_sds_t *values_without_quantile = NULL;
int label_i;
uint64_t timestamp;
uint64_t timestamp = 0;

if (cfl_list_size(&context->metric.samples) < 2) {
return report_error(context,
CMT_DECODE_PROMETHEUS_SYNTAX_ERROR,
"not enough samples for summary");
}

/* quantile_count = sample count - 2:
* - sum
* - count */
quantile_count = cfl_list_size(&context->metric.samples) - 2;
timestamp = context->opts.override_timestamp;
if (context->opts.override_timestamp) {
timestamp = context->opts.override_timestamp;
}

quantile_defaults = calloc(quantile_count, sizeof(*quantile_defaults));
if (!quantile_defaults) {
Expand Down Expand Up @@ -1143,17 +1160,36 @@ static int sample_start(struct cmt_decode_prometheus_context *context)
return 0;
}

static int parse_sample(
struct cmt_decode_prometheus_context *context,
const char *value1,
const char *value2)
static int parse_sample(struct cmt_decode_prometheus_context *context,
const char *value1,
const char *value2)
{
int len;
struct cmt_decode_prometheus_context_sample *sample;
sample = cfl_list_entry_last(&context->metric.samples,
struct cmt_decode_prometheus_context_sample, _head);

strcpy(sample->value1, value1);
strcpy(sample->value2, value2);
/* value1 */
len = strlen(value1);
if (len >= sizeof(sample->value1) - 1) {
return report_error(context,
CMT_DECODE_PROMETHEUS_SAMPLE_VALUE_TOO_LONG,
"sample value is too long (max %zu characters)", sizeof(sample->value1) - 1);
}

strncpy(sample->value1, value1, len);
sample->value1[len] = 0;

/* value2 */
len = strlen(value2);
if (len >= sizeof(sample->value2) - 1) {
return report_error(context,
CMT_DECODE_PROMETHEUS_SAMPLE_VALUE_TOO_LONG,
"sample value is too long (max %zu characters)", sizeof(sample->value2) - 1);
}
strncpy(sample->value2, value2, len);
sample->value2[len] = 0;

return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/cmetrics/src/cmt_encode_prometheus.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ static void append_metric_value(cfl_sds_t *buf,
struct prom_fmt *fmt, int add_timestamp)
{
int len;
double val;
double val = 0.0;
uint64_t ts;
char tmp[128];

Expand Down
16 changes: 11 additions & 5 deletions lib/cmetrics/src/cmt_encode_splunk_hec.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,15 @@ static void append_metric_value(cfl_sds_t *buf, struct cmt_map *map,
static void format_context_common(struct cmt_splunk_hec_context *context, cfl_sds_t *buf, struct cmt_map *map,
struct cmt_metric *metric)
{
int len, tlen;
int len;
int tlen;
int result = CMT_ENCODE_SPLUNK_HEC_ALLOCATION_ERROR;
uint64_t ts;
char hostname[256], timestamp[128];
char *index = NULL, *source = NULL, *source_type = NULL;
char *index = NULL;
char *source = NULL;
char *source_type = NULL;
struct timespec tms;
uint64_t ts;
int result = CMT_ENCODE_SPLUNK_HEC_ALLOCATION_ERROR;

/* Open parenthesis */
cfl_sds_cat_safe(buf, "{", 1);
Expand Down Expand Up @@ -159,6 +162,7 @@ static void format_context_common(struct cmt_splunk_hec_context *context, cfl_sd
len = snprintf(index, tlen, "\"index\":\"%s\",", context->index);
cfl_sds_cat_safe(buf, index, len);
free(index);
index = NULL;
}

/* source */
Expand All @@ -174,13 +178,14 @@ static void format_context_common(struct cmt_splunk_hec_context *context, cfl_sd
len = snprintf(source, tlen, "\"source\":\"%s\",", context->source);
cfl_sds_cat_safe(buf, source, len);
free(source);
source = NULL;
}

/* sourcetype */
if (context->source_type != NULL) {
tlen = strlen(context->source_type) + 18; /* adding snprintf template character length */
source_type = malloc(tlen);
if (source == NULL) {
if (source_type == NULL) {
cmt_errno();
result = CMT_ENCODE_SPLUNK_HEC_ALLOCATION_ERROR;

Expand All @@ -189,6 +194,7 @@ static void format_context_common(struct cmt_splunk_hec_context *context, cfl_sd
len = snprintf(source_type, tlen, "\"sourcetype\":\"%s\",", context->source_type);
cfl_sds_cat_safe(buf, source_type, len);
free(source_type);
source_type = NULL;
}

return;
Expand Down
2 changes: 1 addition & 1 deletion lib/ctraces/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ endif()
# CTraces Version
set(CTR_VERSION_MAJOR 0)
set(CTR_VERSION_MINOR 5)
set(CTR_VERSION_PATCH 6)
set(CTR_VERSION_PATCH 7)
set(CTR_VERSION_STR "${CTR_VERSION_MAJOR}.${CTR_VERSION_MINOR}.${CTR_VERSION_PATCH}")

# Define __FILENAME__ consistently across Operating Systems
Expand Down
14 changes: 8 additions & 6 deletions lib/ctraces/src/ctr_encode_opentelemetry.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ static inline void otlp_array_destroy(Opentelemetry__Proto__Common__V1__ArrayVal

static inline void otlp_kvpair_list_destroy(Opentelemetry__Proto__Common__V1__KeyValue **pair_list, size_t entry_count);

static void destroy_spans(Opentelemetry__Proto__Trace__V1__Span **spans, size_t count);

static inline void otlp_kvpair_destroy(Opentelemetry__Proto__Common__V1__KeyValue *kvpair)
{
if (kvpair != NULL) {
Expand Down Expand Up @@ -425,8 +427,7 @@ static inline Opentelemetry__Proto__Common__V1__AnyValue *ctr_variant_string_to_

if (result->string_value == NULL) {
otlp_any_value_destroy(result);

result = NULL;
return NULL;
}
}

Expand Down Expand Up @@ -484,8 +485,8 @@ static inline Opentelemetry__Proto__Common__V1__AnyValue *ctr_variant_binary_to_

if (result->bytes_value.data == NULL) {
otlp_any_value_destroy(result);

result = NULL;

}

memcpy(result->bytes_value.data, value->data.as_bytes, result->bytes_value.len);
Expand Down Expand Up @@ -884,6 +885,9 @@ static Opentelemetry__Proto__Trace__V1__Span **set_spans(struct ctrace_scope_spa

otel_span = initialize_span();
if (!otel_span) {
if (span_index > 0) {
destroy_spans(spans, span_index);
}
return NULL;
}

Expand Down Expand Up @@ -992,9 +996,7 @@ static Opentelemetry__Proto__Trace__V1__ScopeSpans **set_scope_spans(struct ctra
if (scope_span_index > 0) {
destroy_scope_spans(scope_spans, scope_span_index - 1);
}

free(scope_spans);

/* note: scope_spans is freed inside destroy_scope_spans() */
return NULL;
}

Expand Down
47 changes: 35 additions & 12 deletions plugins/in_forward/fw_prot.c
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@ static int check_ping(struct flb_input_instance *ins,
if (o.type != MSGPACK_OBJECT_STR) {
flb_plg_error(ins, "Invalid shared_key_salt type message");
flb_free(serverside);
flb_free(hostname);
msgpack_unpacked_destroy(&result);
return -1;
}
Expand All @@ -609,7 +610,9 @@ static int check_ping(struct flb_input_instance *ins,
if (o.type != MSGPACK_OBJECT_STR) {
flb_plg_error(ins, "Invalid shared_key_digest type message");
flb_free(serverside);
flb_free(hostname);
msgpack_unpacked_destroy(&result);

return -1;
}
shared_key_digest = flb_sds_create_len(o.via.str.ptr, o.via.str.size);
Expand All @@ -621,6 +624,7 @@ static int check_ping(struct flb_input_instance *ins,
flb_plg_error(ins, "Invalid username type message");
flb_free(serverside);
flb_free(hostname);
flb_free(shared_key_salt);
msgpack_unpacked_destroy(&result);
return -1;
}
Expand All @@ -634,6 +638,7 @@ static int check_ping(struct flb_input_instance *ins,
flb_free(hostname);
flb_free(shared_key_salt);
flb_free(shared_key_digest);
flb_free(username);
msgpack_unpacked_destroy(&result);
return -1;
}
Expand All @@ -648,7 +653,10 @@ static int check_ping(struct flb_input_instance *ins,
flb_free(serverside);
flb_free(username);
flb_free(password_digest);
flb_plg_error(ctx->ins, "failed to hash shard_key");
flb_free(shared_key_salt);
flb_free(shared_key_digest);
flb_free(hostname);
flb_plg_error(ctx->ins, "failed to hash shared_key");
return -1;
}

Expand Down Expand Up @@ -1112,34 +1120,49 @@ static int append_log(struct flb_input_instance *ins, struct fw_conn *conn,
struct ctrace *ctr;

if (event_type == FLB_EVENT_TYPE_LOGS) {
flb_input_log_append(conn->in,
out_tag, flb_sds_len(out_tag),
data, len);
ret = flb_input_log_append(conn->in,
out_tag, flb_sds_len(out_tag),
data, len);
if (ret != 0) {
flb_plg_error(ins, "could not append logs. ret=%d", ret);
return -1;
}

return 0;
}
else if (event_type == FLB_EVENT_TYPE_METRICS) {
ret = cmt_decode_msgpack_create(&cmt, (char *) data, len, &off);
if (ret != CMT_DECODE_MSGPACK_SUCCESS) {
flb_error("cmt_decode_msgpack_create failed. ret=%d", ret);
flb_plg_error(ins, "cmt_decode_msgpack_create failed. ret=%d", ret);
return -1;
}

ret = flb_input_metrics_append(conn->in,
out_tag, flb_sds_len(out_tag),
cmt);
if (ret != 0) {
flb_plg_error(ins, "could not append metrics. ret=%d", ret);
cmt_decode_msgpack_destroy(cmt);
return -1;
}
flb_input_metrics_append(conn->in,
out_tag, flb_sds_len(out_tag),
cmt);
cmt_decode_msgpack_destroy(cmt);
}
else if (event_type == FLB_EVENT_TYPE_TRACES) {
off = 0;
ret = ctr_decode_msgpack_create(&ctr, (char *) data, len, &off);
if (ret == -1) {
flb_error("could not decode trace message. ret=%d", ret);
return -1;
}

flb_input_trace_append(ins,
out_tag, flb_sds_len(out_tag),
ctr);

ret = flb_input_trace_append(ins,
out_tag, flb_sds_len(out_tag),
ctr);
if (ret != 0) {
flb_plg_error(ins, "could not append traces. ret=%d", ret);
ctr_decode_msgpack_destroy(ctr);
return -1;
}
ctr_decode_msgpack_destroy(ctr);
}

Expand Down
Loading
Loading