snprintf retval checks

This commit is contained in:
Olaf Rempel 2006-06-22 20:33:30 +02:00
parent 2a56a66bb9
commit 0c45ceb5fd
9 changed files with 33 additions and 23 deletions

View File

@ -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;

View File

@ -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",

View File

@ -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;
}

View File

@ -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]);

View File

@ -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",

View File

@ -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));

View File

@ -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) {
if (section == NULL)
return;
list_for_each_entry(tupel, &section->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;

View File

@ -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;

View File

@ -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);