Skip to content

Commit

Permalink
Add snprintf wrapper for GCC 8.2/3
Browse files Browse the repository at this point in the history
  • Loading branch information
zebity committed May 13, 2019
1 parent 47a8c3f commit abaa1c8
Show file tree
Hide file tree
Showing 67 changed files with 302 additions and 266 deletions.
6 changes: 3 additions & 3 deletions src/aggregation.c
Expand Up @@ -198,13 +198,13 @@ static int agg_instance_create_name(agg_instance_t *inst, /* {{{ */
sstrncpy(inst->ident.plugin_instance, AGG_FUNC_PLACEHOLDER,
sizeof(inst->ident.plugin_instance));
else if (strcmp("", tmp_plugin) != 0)
snprintf(inst->ident.plugin_instance, sizeof(inst->ident.plugin_instance),
ssnprintf(inst->ident.plugin_instance, sizeof(inst->ident.plugin_instance),
"%s-%s", tmp_plugin, AGG_FUNC_PLACEHOLDER);
else if (strcmp("", tmp_plugin_instance) != 0)
snprintf(inst->ident.plugin_instance, sizeof(inst->ident.plugin_instance),
ssnprintf(inst->ident.plugin_instance, sizeof(inst->ident.plugin_instance),
"%s-%s", tmp_plugin_instance, AGG_FUNC_PLACEHOLDER);
else
snprintf(inst->ident.plugin_instance, sizeof(inst->ident.plugin_instance),
ssnprintf(inst->ident.plugin_instance, sizeof(inst->ident.plugin_instance),
"%s-%s-%s", tmp_plugin, tmp_plugin_instance,
AGG_FUNC_PLACEHOLDER);
}
Expand Down
12 changes: 6 additions & 6 deletions src/amqp.c
Expand Up @@ -217,23 +217,23 @@ static char *camqp_strerror(camqp_config_t *conf, /* {{{ */
if (r.reply.id == AMQP_CONNECTION_CLOSE_METHOD) {
amqp_connection_close_t *m = r.reply.decoded;
char *tmp = camqp_bytes_cstring(&m->reply_text);
snprintf(buffer, buffer_size, "Server connection error %d: %s",
ssnprintf(buffer, buffer_size, "Server connection error %d: %s",
m->reply_code, tmp);
sfree(tmp);
} else if (r.reply.id == AMQP_CHANNEL_CLOSE_METHOD) {
amqp_channel_close_t *m = r.reply.decoded;
char *tmp = camqp_bytes_cstring(&m->reply_text);
snprintf(buffer, buffer_size, "Server channel error %d: %s",
ssnprintf(buffer, buffer_size, "Server channel error %d: %s",
m->reply_code, tmp);
sfree(tmp);
} else {
snprintf(buffer, buffer_size, "Server error method %#" PRIx32,
ssnprintf(buffer, buffer_size, "Server error method %#" PRIx32,
r.reply.id);
}
break;

default:
snprintf(buffer, buffer_size, "Unknown reply type %i", (int)r.reply_type);
ssnprintf(buffer, buffer_size, "Unknown reply type %i", (int)r.reply_type);
}

return buffer;
Expand Down Expand Up @@ -748,7 +748,7 @@ static int camqp_write(const data_set_t *ds, const value_list_t *vl, /* {{{ */
if (conf->routing_key != NULL) {
sstrncpy(routing_key, conf->routing_key, sizeof(routing_key));
} else {
snprintf(routing_key, sizeof(routing_key), "collectd/%s/%s/%s/%s/%s",
ssnprintf(routing_key, sizeof(routing_key), "collectd/%s/%s/%s/%s/%s",
vl->host, vl->plugin, vl->plugin_instance, vl->type,
vl->type_instance);

Expand Down Expand Up @@ -970,7 +970,7 @@ static int camqp_config_connection(oconfig_item_t *ci, /* {{{ */

if (publish) {
char cbname[128];
snprintf(cbname, sizeof(cbname), "amqp/%s", conf->name);
ssnprintf(cbname, sizeof(cbname), "amqp/%s", conf->name);

status =
plugin_register_write(cbname, camqp_write,
Expand Down
4 changes: 2 additions & 2 deletions src/amqp1.c
Expand Up @@ -624,12 +624,12 @@ static int amqp1_config_instance(oconfig_item_t *ci) /* {{{ */
return status;
} else {
char tpname[DATA_MAX_NAME_LEN];
status = snprintf(tpname, sizeof(tpname), "amqp1/%s", instance->name);
status = ssnprintf(tpname, sizeof(tpname), "amqp1/%s", instance->name);
if ((status < 0) || (size_t)status >= sizeof(tpname)) {
ERROR("amqp1 plugin: Instance name would have been truncated.");
return -1;
}
status = snprintf(instance->send_to, sizeof(instance->send_to), "/%s/%s",
status = ssnprintf(instance->send_to, sizeof(instance->send_to), "/%s/%s",
transport->address, instance->name);
if ((status < 0) || (size_t)status >= sizeof(instance->send_to)) {
ERROR("amqp1 plugin: send_to address would have been truncated.");
Expand Down
6 changes: 3 additions & 3 deletions src/aquaero.c
Expand Up @@ -81,7 +81,7 @@ static void aquaero_submit_array(const char *type,
if (value_array[i] == AQ5_FLOAT_UNDEF)
continue;

snprintf(type_instance, sizeof(type_instance), "%s%d", type_instance_prefix,
ssnprintf(type_instance, sizeof(type_instance), "%s%d", type_instance_prefix,
i + 1);
aquaero_submit(type, type_instance, value_array[i]);
}
Expand Down Expand Up @@ -130,7 +130,7 @@ static int aquaero_read(void) {
(aq_data.fan_vrm_temp[i] != AQ5_FLOAT_UNDEF))
continue;

snprintf(type_instance, sizeof(type_instance), "fan%d", i + 1);
ssnprintf(type_instance, sizeof(type_instance), "fan%d", i + 1);

aquaero_submit("fanspeed", type_instance, aq_data.fan_rpm[i]);
aquaero_submit("percent", type_instance, aq_data.fan_duty[i]);
Expand All @@ -139,7 +139,7 @@ static int aquaero_read(void) {

/* Report the voltage reglator module (VRM) temperature with a
* different type instance. */
snprintf(type_instance, sizeof(type_instance), "fan%d-vrm", i + 1);
ssnprintf(type_instance, sizeof(type_instance), "fan%d-vrm", i + 1);
aquaero_submit("temperature", type_instance, aq_data.fan_vrm_temp[i]);
}

Expand Down
2 changes: 1 addition & 1 deletion src/ascent.c
Expand Up @@ -499,7 +499,7 @@ static int ascent_init(void) /* {{{ */
static char credentials[1024];
int status;

status = snprintf(credentials, sizeof(credentials), "%s:%s", user,
status = ssnprintf(credentials, sizeof(credentials), "%s:%s", user,
(pass == NULL) ? "" : pass);
if ((status < 0) || ((size_t)status >= sizeof(credentials))) {
ERROR("ascent plugin: ascent_init: Returning an error because the "
Expand Down
4 changes: 2 additions & 2 deletions src/ceph.c
Expand Up @@ -990,7 +990,7 @@ static int cconn_connect(struct cconn *io) {
return err;
}
address.sun_family = AF_UNIX;
snprintf(address.sun_path, sizeof(address.sun_path), "%s", io->d->asok_path);
ssnprintf(address.sun_path, sizeof(address.sun_path), "%s", io->d->asok_path);
RETRY_ON_EINTR(err, connect(fd, (struct sockaddr *)&address,
sizeof(struct sockaddr_un)));
if (err < 0) {
Expand Down Expand Up @@ -1153,7 +1153,7 @@ static ssize_t cconn_handle_event(struct cconn *io) {
return -EDOM;
case CSTATE_WRITE_REQUEST: {
char cmd[32];
snprintf(cmd, sizeof(cmd), "%s%d%s", "{ \"prefix\": \"", io->request_type,
ssnprintf(cmd, sizeof(cmd), "%s%d%s", "{ \"prefix\": \"", io->request_type,
"\" }\n");
size_t cmd_len = strlen(cmd);
RETRY_ON_EINTR(
Expand Down
21 changes: 19 additions & 2 deletions src/collectdctl.c
Expand Up @@ -27,6 +27,7 @@

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
Expand Down Expand Up @@ -80,6 +81,22 @@
extern char *optarg;
extern int optind;

/* ssnprintf returns zero on success, one if truncation occurred
and a negative integer onerror. */
static int _ssnprintf(char *str, size_t sz, const char *format, ...) {
va_list ap;
va_start(ap, format);

int ret = vsnprintf(str, sz, format, ap);

va_end(ap);

if (ret < 0) {
return ret;
}
return (size_t)ret >= sz;
} /* int _ssnprintf */

__attribute__((noreturn)) static void exit_usage(const char *name, int status) {
fprintf(
(status == 0) ? stdout : stderr,
Expand Down Expand Up @@ -166,7 +183,7 @@ static int parse_identifier(lcc_connection_t *c, const char *value,
}
hostname[sizeof(hostname) - 1] = '\0';

snprintf(ident_str, sizeof(ident_str), "%s/%s", hostname, value);
_ssnprintf(ident_str, sizeof(ident_str), "%s/%s", hostname, value);
ident_str[sizeof(ident_str) - 1] = '\0';
} else {
strncpy(ident_str, value, sizeof(ident_str));
Expand Down Expand Up @@ -543,7 +560,7 @@ int main(int argc, char **argv) {

switch (opt) {
case 's':
snprintf(address, sizeof(address), "unix:%s", optarg);
_ssnprintf(address, sizeof(address), "unix:%s", optarg);
address[sizeof(address) - 1] = '\0';
break;
case 'h':
Expand Down
12 changes: 6 additions & 6 deletions src/daemon/configfile.c
Expand Up @@ -200,7 +200,7 @@ static int dispatch_global_option(const oconfig_item_t *ci) {
return global_option_set(ci->key, ci->values[0].value.string, 0);
else if (ci->values[0].type == OCONFIG_TYPE_NUMBER) {
char tmp[128];
snprintf(tmp, sizeof(tmp), "%lf", ci->values[0].value.number);
ssnprintf(tmp, sizeof(tmp), "%lf", ci->values[0].value.number);
return global_option_set(ci->key, tmp, 0);
} else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN) {
if (ci->values[0].value.boolean)
Expand Down Expand Up @@ -311,12 +311,12 @@ static int dispatch_value_plugin(const char *plugin, oconfig_item_t *ci) {

if (ci->values[i].type == OCONFIG_TYPE_STRING)
status =
snprintf(buffer_ptr, buffer_free, " %s", ci->values[i].value.string);
ssnprintf(buffer_ptr, buffer_free, " %s", ci->values[i].value.string);
else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
status =
snprintf(buffer_ptr, buffer_free, " %lf", ci->values[i].value.number);
ssnprintf(buffer_ptr, buffer_free, " %lf", ci->values[i].value.number);
else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
status = snprintf(buffer_ptr, buffer_free, " %s",
status = ssnprintf(buffer_ptr, buffer_free, " %s",
ci->values[i].value.boolean ? "true" : "false");

if ((status < 0) || (status >= buffer_free))
Expand Down Expand Up @@ -666,7 +666,7 @@ static oconfig_item_t *cf_read_dir(const char *dir, const char *pattern,
if ((de->d_name[0] == '.') || (de->d_name[0] == 0))
continue;

status = snprintf(name, sizeof(name), "%s/%s", dir, de->d_name);
status = ssnprintf(name, sizeof(name), "%s/%s", dir, de->d_name);
if ((status < 0) || ((size_t)status >= sizeof(name))) {
ERROR("configfile: Not including `%s/%s' because its"
" name is too long.",
Expand Down Expand Up @@ -1235,7 +1235,7 @@ int cf_util_get_service(const oconfig_item_t *ci, char **ret_string) /* {{{ */
P_ERROR("cf_util_get_service: Out of memory.");
return -1;
}
snprintf(service, 6, "%i", port);
ssnprintf(service, 6, "%i", port);

sfree(*ret_string);
*ret_string = service;
Expand Down
4 changes: 2 additions & 2 deletions src/disk.c
Expand Up @@ -493,10 +493,10 @@ static int disk_read(void) {
sstrncpy(disk_name, props_disk_name_bsd, sizeof(disk_name));
else {
ERROR("disk plugin: can't find bsd disk name.");
snprintf(disk_name, sizeof(disk_name), "%i-%i", disk_major, disk_minor);
ssnprintf(disk_name, sizeof(disk_name), "%i-%i", disk_major, disk_minor);
}
} else
snprintf(disk_name, sizeof(disk_name), "%i-%i", disk_major, disk_minor);
ssnprintf(disk_name, sizeof(disk_name), "%i-%i", disk_major, disk_minor);

DEBUG("disk plugin: disk_name = \"%s\"", disk_name);

Expand Down
24 changes: 12 additions & 12 deletions src/dpdkevents.c
Expand Up @@ -511,16 +511,16 @@ static int dpdk_events_link_status_dispatch(dpdk_helper_ctx_t *phc) {

char dev_name[DATA_MAX_NAME_LEN];
if (ec->config.link_status.port_name[i][0] != 0) {
snprintf(dev_name, sizeof(dev_name), "%s",
ssnprintf(dev_name, sizeof(dev_name), "%s",
ec->config.link_status.port_name[i]);
} else {
snprintf(dev_name, sizeof(dev_name), "port.%d", i);
ssnprintf(dev_name, sizeof(dev_name), "port.%d", i);
}

if (ec->config.link_status.notify) {
int sev = ec->link_info[i].link_status ? NOTIF_OKAY : NOTIF_WARNING;
char msg[DATA_MAX_NAME_LEN];
snprintf(msg, sizeof(msg), "Link Status: %s",
ssnprintf(msg, sizeof(msg), "Link Status: %s",
ec->link_info[i].link_status ? "UP" : "DOWN");
dpdk_events_notification_dispatch(sev, dev_name,
ec->link_info[i].read_time, msg);
Expand Down Expand Up @@ -557,7 +557,7 @@ static void dpdk_events_keep_alive_dispatch(dpdk_helper_ctx_t *phc) {
}

char core_name[DATA_MAX_NAME_LEN];
snprintf(core_name, sizeof(core_name), "lcore%u", i);
ssnprintf(core_name, sizeof(core_name), "lcore%u", i);

if (!ec->config.keep_alive.send_updated ||
(ec->core_info[i].lcore_state !=
Expand All @@ -572,34 +572,34 @@ static void dpdk_events_keep_alive_dispatch(dpdk_helper_ctx_t *phc) {
switch (ec->config.keep_alive.shm->core_state[i]) {
case RTE_KA_STATE_ALIVE:
sev = NOTIF_OKAY;
snprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: ALIVE", i);
ssnprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: ALIVE", i);
break;
case RTE_KA_STATE_MISSING:
snprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: MISSING", i);
ssnprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: MISSING", i);
sev = NOTIF_WARNING;
break;
case RTE_KA_STATE_DEAD:
snprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: DEAD", i);
ssnprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: DEAD", i);
sev = NOTIF_FAILURE;
break;
case RTE_KA_STATE_UNUSED:
snprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: UNUSED", i);
ssnprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: UNUSED", i);
sev = NOTIF_OKAY;
break;
case RTE_KA_STATE_GONE:
snprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: GONE", i);
ssnprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: GONE", i);
sev = NOTIF_FAILURE;
break;
case RTE_KA_STATE_DOZING:
snprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: DOZING", i);
ssnprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: DOZING", i);
sev = NOTIF_OKAY;
break;
case RTE_KA_STATE_SLEEP:
snprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: SLEEP", i);
ssnprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: SLEEP", i);
sev = NOTIF_OKAY;
break;
default:
snprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: UNKNOWN", i);
ssnprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: UNKNOWN", i);
sev = NOTIF_FAILURE;
}

Expand Down
4 changes: 2 additions & 2 deletions src/dpdkstat.c
Expand Up @@ -395,9 +395,9 @@ static int dpdk_stats_counters_dispatch(dpdk_helper_ctx_t *phc) {

char dev_name[64];
if (ctx->config.port_name[i][0] != 0) {
snprintf(dev_name, sizeof(dev_name), "%s", ctx->config.port_name[i]);
ssnprintf(dev_name, sizeof(dev_name), "%s", ctx->config.port_name[i]);
} else {
snprintf(dev_name, sizeof(dev_name), "port.%d", i);
ssnprintf(dev_name, sizeof(dev_name), "port.%d", i);
}

DEBUG(" === Dispatch stats for port %d (name=%s; stats_count=%d)", i,
Expand Down
2 changes: 1 addition & 1 deletion src/gmond.c
Expand Up @@ -397,7 +397,7 @@ static staging_entry_t *staging_entry_get(const char *host, /* {{{ */
if (staging_tree == NULL)
return NULL;

snprintf(key, sizeof(key), "%s/%s/%s", host, type,
ssnprintf(key, sizeof(key), "%s/%s/%s", host, type,
(type_instance != NULL) ? type_instance : "");

se = NULL;
Expand Down
12 changes: 6 additions & 6 deletions src/intel_rdt.c
Expand Up @@ -105,7 +105,7 @@ static void rdt_submit_derive(const char *cgroup, const char *type,
vl.values_len = 1;

sstrncpy(vl.plugin, RDT_PLUGIN, sizeof(vl.plugin));
snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s", cgroup);
ssnprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s", cgroup);
sstrncpy(vl.type, type, sizeof(vl.type));
if (type_instance)
sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
Expand All @@ -121,7 +121,7 @@ static void rdt_submit_gauge(const char *cgroup, const char *type,
vl.values_len = 1;

sstrncpy(vl.plugin, RDT_PLUGIN, sizeof(vl.plugin));
snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s", cgroup);
ssnprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s", cgroup);
sstrncpy(vl.type, type, sizeof(vl.type));
if (type_instance)
sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
Expand All @@ -144,7 +144,7 @@ static void rdt_dump_cgroups(void) {

memset(cores, 0, sizeof(cores));
for (size_t j = 0; j < cgroup->num_cores; j++) {
snprintf(cores + strlen(cores), sizeof(cores) - strlen(cores) - 1, " %d",
ssnprintf(cores + strlen(cores), sizeof(cores) - strlen(cores) - 1, " %d",
cgroup->cores[j]);
}

Expand All @@ -171,7 +171,7 @@ static void rdt_dump_ngroups(void) {
for (size_t i = 0; i < g_rdt->num_ngroups; i++) {
memset(names, 0, sizeof(names));
for (size_t j = 0; j < g_rdt->ngroups[i].num_names; j++)
snprintf(names + strlen(names), sizeof(names) - strlen(names) - 1, " %s",
ssnprintf(names + strlen(names), sizeof(names) - strlen(names) - 1, " %s",
g_rdt->ngroups[i].names[j]);

DEBUG(RDT_PLUGIN ": group[%d]:", (int)i);
Expand Down Expand Up @@ -249,7 +249,7 @@ static void rdt_dump_pids_data(void) {
for (size_t j = 0; j < g_rdt->ngroups[i].num_names; ++j) {
pids_list_t *list = g_rdt->ngroups[i].proc_pids[j]->curr;
for (size_t k = 0; k < list->size; k++)
snprintf(pids + strlen(pids), sizeof(pids) - strlen(pids) - 1, " %u",
ssnprintf(pids + strlen(pids), sizeof(pids) - strlen(pids) - 1, " %u",
list->pids[k]);
}
DEBUG(RDT_PLUGIN ": [%s] %s", g_rdt->ngroups[i].desc, pids);
Expand Down Expand Up @@ -922,7 +922,7 @@ static int rdt_default_cgroups(void) {
cgroup->num_cores = 1;
cgroup->cores[0] = i;

snprintf(desc, sizeof(desc), "%d", g_rdt->pqos_cpu->cores[i].lcore);
ssnprintf(desc, sizeof(desc), "%d", g_rdt->pqos_cpu->cores[i].lcore);
cgroup->desc = strdup(desc);
if (cgroup->desc == NULL) {
ERROR(RDT_PLUGIN ": Error allocating core group description");
Expand Down
2 changes: 1 addition & 1 deletion src/interface.c
Expand Up @@ -297,7 +297,7 @@ static int interface_read(void) {
continue;

if (unique_name)
snprintf(iname, sizeof(iname), "%s_%d_%s", ksp[i]->ks_module,
ssnprintf(iname, sizeof(iname), "%s_%d_%s", ksp[i]->ks_module,
ksp[i]->ks_instance, ksp[i]->ks_name);
else
sstrncpy(iname, ksp[i]->ks_name, sizeof(iname));
Expand Down

0 comments on commit abaa1c8

Please sign in to comment.