From: Florian Forster Date: Wed, 20 Feb 2013 08:06:39 +0000 (+0100) Subject: snort plugin: Improve error handling. X-Git-Tag: collectd-5.3.0~26^2~12 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=c97c8756a28969fea924a80bfc42c989dcd20ced;p=collectd.git snort plugin: Improve error handling. This prevents file descriptors and memory from leaking if something goes wrong, e.g. the last line in the file is a comment. --- diff --git a/src/snort.c b/src/snort.c index 6e7e4a7f..a49cb5fe 100644 --- a/src/snort.c +++ b/src/snort.c @@ -116,11 +116,13 @@ static int snort_read(user_data_t *ud){ if ((fstat(fd, &sb) != 0) || (!S_ISREG(sb.st_mode))){ ERROR("snort plugin: `%s' is not a file.", id->path); + close (fd); return (-1); } if (sb.st_size == 0){ ERROR("snort plugin: `%s' is empty.", id->path); + close (fd); return (-1); } @@ -128,6 +130,7 @@ static int snort_read(user_data_t *ud){ /* offset = */ 0); if (p_start == MAP_FAILED){ ERROR("snort plugin: mmap error"); + close (fd); return (-1); } @@ -147,11 +150,15 @@ static int snort_read(user_data_t *ud){ if (metrics_num == 1){ ERROR("snort plugin: last line of `%s' does not contain enough values.", id->path); + close (fd); + munmap(p_start, sb.st_size); return (-1); } if (*p_end == '#'){ ERROR("snort plugin: last line of `%s' is a comment.", id->path); + close (fd); + munmap(p_start, sb.st_size); return (-1); } @@ -166,6 +173,7 @@ static int snort_read(user_data_t *ud){ metrics = calloc (metrics_num, sizeof (*metrics)); if (metrics == NULL) { ERROR ("snort plugin: calloc failed."); + sfree (buf); return (-1); }