From: Ruben Kerkhof Date: Tue, 15 May 2018 20:03:00 +0000 (+0200) Subject: table plugin: fix truncation warnings X-Git-Url: https://git.octo.it/?a=commitdiff_plain;h=a97658980166d9b19b6dda048a7969e163daf58b;hp=-c;p=collectd.git table plugin: fix truncation warnings CC src/table.lo src/table.c: In function ‘tbl_read_table’: src/table.c:396:66: warning: ‘snprintf’ output may be truncated before the last format character [-Wformat-truncation=] snprintf(vl.type_instance, sizeof(vl.type_instance), "%s-%s", ^ src/table.c:396:7: note: ‘snprintf’ output 2 or more bytes (assuming 129) into a destination of size 128 snprintf(vl.type_instance, sizeof(vl.type_instance), "%s-%s", ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ res->instance_prefix, instances_str); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --- a97658980166d9b19b6dda048a7969e163daf58b diff --git a/src/table.c b/src/table.c index 59471bd1..ae26c789 100644 --- a/src/table.c +++ b/src/table.c @@ -389,17 +389,15 @@ static int tbl_result_dispatch(tbl_t *tbl, tbl_result_t *res, char **fields, STATIC_ARRAY_SIZE(instances), "-"); instances_str[sizeof(instances_str) - 1] = '\0'; - vl.type_instance[sizeof(vl.type_instance) - 1] = '\0'; + int r; if (res->instance_prefix == NULL) - strncpy(vl.type_instance, instances_str, sizeof(vl.type_instance)); + r = snprintf(vl.type_instance, sizeof(vl.type_instance), "%s", + instances_str); else - snprintf(vl.type_instance, sizeof(vl.type_instance), "%s-%s", - res->instance_prefix, instances_str); - - if (vl.type_instance[sizeof(vl.type_instance) - 1] != '\0') { - vl.type_instance[sizeof(vl.type_instance) - 1] = '\0'; + r = snprintf(vl.type_instance, sizeof(vl.type_instance), "%s-%s", + res->instance_prefix, instances_str); + if (r >= sizeof(vl.type_instance)) log_warn("Truncated type instance: %s.", vl.type_instance); - } } plugin_dispatch_values(&vl); @@ -429,7 +427,7 @@ static int tbl_parse_line(tbl_t *tbl, char *line, size_t len) { for (i = 0; i < tbl->results_num; ++i) if (tbl_result_dispatch(tbl, tbl->results + i, fields, - STATIC_ARRAY_SIZE(fields)) != 0) { + STATIC_ARRAY_SIZE(fields)) != 0) { log_err("Failed to dispatch result."); continue; }