From 0c45ceb5fd00375f1573cae394e386f97d7c399c Mon Sep 17 00:00:00 2001 From: Olaf Rempel Date: Thu, 22 Jun 2006 20:33:30 +0200 Subject: [PATCH] snprintf retval checks --- logging.c | 2 +- p_ctstat.c | 6 ++++-- p_mount.c | 2 +- p_netdev.c | 5 ++++- p_rtstat.c | 6 ++++-- p_stat.c | 6 ++++-- plugins.c | 15 ++++++++------- rrdtool.c | 12 ++++++------ sammler.c | 2 +- 9 files changed, 33 insertions(+), 23 deletions(-) diff --git a/logging.c b/logging.c index 521c2be..d4fd706 100644 --- a/logging.c +++ b/logging.c @@ -48,7 +48,7 @@ void log_print(int prio, const char *fmt, ...) len = vsnprintf(buffer, BUFSIZE, fmt, az); va_end(az); - if (len >= BUFSIZE) { + if (len < 0 || len >= BUFSIZE) { log_print(LOG_ERROR, "log_print: arguments too long"); errno = 0; return; diff --git a/p_ctstat.c b/p_ctstat.c index 2974bcc..80354bc 100644 --- a/p_ctstat.c +++ b/p_ctstat.c @@ -57,7 +57,7 @@ static void probe(void) FILE *fp; char *buffer, *val[16], filename[16]; unsigned long long arr[16]; - int i, cpu = 0; + int i, len, cpu = 0; buffer = malloc(BUFSIZE); if (buffer == NULL) { @@ -82,7 +82,9 @@ static void probe(void) for (i = 0; i < 16; i++) arr[i] = strtoll(val[i], NULL, 16); - snprintf(filename, sizeof(filename), "ctstat-%d.rrd", cpu); + len = snprintf(filename, sizeof(filename), "ctstat-%d.rrd", cpu); + if (len < 0 || len >= sizeof(filename)) + continue; probe_submit(&plugin, filename, 0, "%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu", diff --git a/p_mount.c b/p_mount.c index a5dfb83..412f39f 100644 --- a/p_mount.c +++ b/p_mount.c @@ -86,7 +86,7 @@ static void probe(void) } len = snprintf(filename, sizeof(filename), "mount%s.rrd", mnt->mnt_fsname); - if (len >= sizeof(filename)) { + if (len < 0 || len >= sizeof(filename)) { log_print(LOG_WARN, "plugin mount: file name too long", mnt->mnt_fsname); continue; } diff --git a/p_netdev.c b/p_netdev.c index c62ff68..eca0d79 100644 --- a/p_netdev.c +++ b/p_netdev.c @@ -45,6 +45,7 @@ static void probe(void) FILE *fp; char *buffer, *stats, *device; char *val[16], filename[32]; + int len; buffer = malloc(BUFSIZE); if (buffer == NULL) { @@ -76,7 +77,9 @@ static void probe(void) if (strsplit(stats, val, 16) != 16) continue; - snprintf(filename, sizeof(filename), "net-%s.rrd", device); + len = snprintf(filename, sizeof(filename), "net-%s.rrd", device); + if (len < 0 || len >= sizeof(filename)) + continue; probe_submit(&plugin, filename, 0, "%s:%s:%s:%s", val[0], val[8], val[1], val[9]); diff --git a/p_rtstat.c b/p_rtstat.c index 0612d14..c081833 100644 --- a/p_rtstat.c +++ b/p_rtstat.c @@ -58,7 +58,7 @@ static void probe(void) FILE *fp; char *buffer, *val[17], filename[16]; unsigned long long arr[17]; - int i, cpu = 0; + int i, len, cpu = 0; buffer = malloc(BUFSIZE); if (buffer == NULL) { @@ -83,7 +83,9 @@ static void probe(void) for (i = 0; i < 17; i++) arr[i] = strtoll(val[i], NULL, 16); - snprintf(filename, sizeof(filename), "rtstat-%d.rrd", cpu); + len = snprintf(filename, sizeof(filename), "rtstat-%d.rrd", cpu); + if (len < 0 || len >= sizeof(filename)) + continue; probe_submit(&plugin, filename, 0, "%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu", diff --git a/p_stat.c b/p_stat.c index 4104994..b1bb038 100644 --- a/p_stat.c +++ b/p_stat.c @@ -90,11 +90,13 @@ static void probe(void) while (fgets(buffer, BUFSIZE, fp) != NULL) { if (!strncmp(buffer, "cpu", 3)) { char *val[9], filename[16]; - int numfields, cpu; + int numfields, len, cpu; if ((buffer[3] >= '0') && (buffer[3] <= '9')) { cpu = atoi(buffer +3); - snprintf(filename, sizeof(filename), "cpu-%d.rrd", cpu); + len = snprintf(filename, sizeof(filename), "cpu-%d.rrd", cpu); + if (len < 0 || len >= sizeof(filename)) + continue; } else { strncpy(filename, "cpu.rrd", sizeof(filename)); diff --git a/plugins.c b/plugins.c index e27d97a..dae1a4e 100644 --- a/plugins.c +++ b/plugins.c @@ -54,7 +54,7 @@ static void plugin_load(char *filename) } len = snprintf(buffer, BUFSIZE, "%s/%s", plugin_dir, filename); - if (len >= BUFSIZE) { + if (len < 0 || len >= BUFSIZE) { log_print(LOG_ERROR, "plugin_load: file name too long: %s/%s", plugin_dir, filename); free(buffer); return; @@ -90,11 +90,12 @@ void plugin_load_all() struct conf_tupel *tupel; section = config_get_section("global"); - if (section) { - list_for_each_entry(tupel, §ion->tupel, list) - if (!strcmp(tupel->option, "plugin")) - plugin_load(tupel->parameter); - } + if (section == NULL) + return; + + list_for_each_entry(tupel, §ion->tupel, list) + if (!strcmp(tupel->option, "plugin")) + plugin_load(tupel->parameter); } void plugins_probe(void) @@ -136,7 +137,7 @@ void probe_submit(struct sammler_plugin *plugin, char *filename, int ds_id, cons len = vsnprintf(buffer, BUFSIZE, fmt, az); va_end(az); - if (len >= BUFSIZE) { + if (len < 0 || len >= BUFSIZE) { log_print(LOG_ERROR, "probe_submit: %s arguments too long", plugin->name); free(buffer); return; diff --git a/rrdtool.c b/rrdtool.c index 2f06aeb..716ffb6 100644 --- a/rrdtool.c +++ b/rrdtool.c @@ -60,7 +60,7 @@ static int append_rra_config(char *buffer, int size, int *pos) continue; len = snprintf(buffer + *pos, size - *pos, "%s ", tupel->parameter); - if (len >= size - *pos) { + if (len < 0 || len >= size - *pos) { log_print(LOG_ERROR, "append_ds_config: arguments too long"); return -1; } @@ -84,14 +84,14 @@ static int append_ds_config(char *buffer, int size, int *pos, int heartbeat, cha while (*ds_def != NULL) { len = snprintf(dsbuild, BUFSIZE, *ds_def, heartbeat); - if (len >= BUFSIZE) { + if (len < 0 || len >= BUFSIZE) { log_print(LOG_ERROR, "append_ds_config: arguments too long"); free(dsbuild); return -1; } len = snprintf(buffer + *pos, size - *pos, "%s ", dsbuild); - if (len >= size - *pos) { + if (len < 0 || len >= size - *pos) { log_print(LOG_ERROR, "append_ds_config: arguments too long"); free(dsbuild); return -1; @@ -151,7 +151,7 @@ static int rrd_create_file(char *filename, char **ds_def) heartbeat = (step * 2) + (step / 2); pos = snprintf(buffer, ARGVSIZE, "create %s -s %d ", filename, step); - if (pos >= ARGVSIZE) { + if (pos < 0 || pos >= ARGVSIZE) { log_print(LOG_ERROR, "rrd_create_file: arguments too long"); free(buffer); return -1; @@ -185,7 +185,7 @@ static int rrd_update_file(char *filename, char *values) } pos = snprintf(buffer, ARGVSIZE, "update %s %lu:%s", filename, time(NULL), values); - if (pos >= ARGVSIZE) { + if (pos < 0 || pos >= ARGVSIZE) { log_print(LOG_ERROR, "rrd_update_file: arguments too long"); free(buffer); return -1; @@ -272,7 +272,7 @@ void rrd_submit(char *plugin, int version, char *filename, int ds_id, char *data } len = snprintf(fullfile, BUFSIZE, "%s/%s", rrd_dir, filename); - if (len >= BUFSIZE) { + if (len < 0 || len >= BUFSIZE) { log_print(LOG_ERROR, "rrd_submit: arguments too long"); free(fullfile); return; diff --git a/sammler.c b/sammler.c index 823cba4..b85f761 100644 --- a/sammler.c +++ b/sammler.c @@ -80,7 +80,7 @@ int main(int argc, char *argv[]) /* check logfile */ char *logfile = config_get_string("global", "logfile", DEFAULT_LOGFILE); - if (logfile && !debug) { + if (logfile != NULL && debug != 0) { /* start logging */ if (!log_init(logfile)) exit(-1);