summaryrefslogtreecommitdiffstats
path: root/collectors/proc.plugin
diff options
context:
space:
mode:
authorthiagoftsm <thiagoftsm@gmail.com>2019-09-20 11:45:11 +0000
committerChris Akritidis <43294513+cakrit@users.noreply.github.com>2019-09-20 13:45:11 +0200
commitf555472a8ceb189407b103570be9a28031819d4b (patch)
treef6033bcc7c9f2c29b0320f360d9e8d95ed228f01 /collectors/proc.plugin
parent46c4b8809cad6a0dc65d91e54429e8a7a581c5a2 (diff)
Fix some errors reported by Coverity (#6797)
* coverity_20190905: Fix reported bugs This commit has fixes for some bugs reported by Coverity in the present day * coverity_20190905: Fix missing report FIx a missing report of error * coverity_20190905: Pipe close The previous fix had an error that wolud allow a socket continue opened, this commit fixes this * coverity_20190905: Error pattern The call of perror would generate a different error report, instead I am using strerror() to keep pattern * coverity_20190905: Error function Rewrite the call to error function * coverity_20190905: Fix missing tests The previous fix did not have correct tests after to clean the variables * coverity_20190905: Fix readable I changed for an else instead a new if, it is more clean this way * coverity_20190905: remove unecessary test This commit is removing an unecessary test for a variable that will never be NULL. * coverity_20190905: Add neccessary NULLL After to clean the variable, I am setting NULL to variable to avoid clean again * coverity_20190905: Remove false error The condition added to fix Coverity was generating false positives, so we are changing to debug * coverity_20190905: Remove false error The condition added to fix Coverity was generating false positives, so we are changing to debug * coverity_20190905: Bring else to avoid error Bring an else to solve the problem to read a FD not opened * coverity_20190905: Return After to analyse the last changes, I decided to return, because they were not necessary * coverity_20190905: Remove NULL Remove unecessary set of variable to NULL
Diffstat (limited to 'collectors/proc.plugin')
-rw-r--r--collectors/proc.plugin/proc_mdstat.c2
-rw-r--r--collectors/proc.plugin/sys_class_power_supply.c98
2 files changed, 54 insertions, 46 deletions
diff --git a/collectors/proc.plugin/proc_mdstat.c b/collectors/proc.plugin/proc_mdstat.c
index 5c29d31c7b..abfd2ff122 100644
--- a/collectors/proc.plugin/proc_mdstat.c
+++ b/collectors/proc.plugin/proc_mdstat.c
@@ -197,7 +197,7 @@ int do_proc_mdstat(int update_every, usec_t dt) {
}
s++;
}
- if(unlikely(str_total[0] == '\0' || str_inuse[0] == '\0')) {
+ if(unlikely(str_total[0] == '\0' || !str_inuse || str_inuse[0] == '\0')) {
error("Cannot read /proc/mdstat raid health status. Unexpected format.");
continue;
}
diff --git a/collectors/proc.plugin/sys_class_power_supply.c b/collectors/proc.plugin/sys_class_power_supply.c
index 5e37ad5aba..c558a384da 100644
--- a/collectors/proc.plugin/sys_class_power_supply.c
+++ b/collectors/proc.plugin/sys_class_power_supply.c
@@ -245,67 +245,75 @@ int do_sys_class_power_supply(int update_every, usec_t dt) {
if(unlikely(ps->capacity->fd == -1)) {
error("Cannot open file '%s'", ps->capacity->filename);
power_supply_free(ps);
+ ps = NULL;
}
}
- ssize_t r = read(ps->capacity->fd, buffer, 30);
- if(unlikely(r < 1)) {
- error("Cannot read file '%s'", ps->capacity->filename);
- power_supply_free(ps);
- }
- else {
- buffer[r] = '\0';
- ps->capacity->value = str2ull(buffer);
- }
+ if (ps)
+ {
+ ssize_t r = read(ps->capacity->fd, buffer, 30);
+ if(unlikely(r < 1)) {
+ error("Cannot read file '%s'", ps->capacity->filename);
+ power_supply_free(ps);
+ ps = NULL;
+ }
+ else {
+ buffer[r] = '\0';
+ ps->capacity->value = str2ull(buffer);
- if(unlikely(!keep_fds_open)) {
- close(ps->capacity->fd);
- ps->capacity->fd = -1;
- }
- else if(unlikely(lseek(ps->capacity->fd, 0, SEEK_SET) == -1)) {
- error("Cannot seek in file '%s'", ps->capacity->filename);
- close(ps->capacity->fd);
- ps->capacity->fd = -1;
+ if(unlikely(!keep_fds_open)) {
+ close(ps->capacity->fd);
+ ps->capacity->fd = -1;
+ }
+ else if(unlikely(lseek(ps->capacity->fd, 0, SEEK_SET) == -1)) {
+ error("Cannot seek in file '%s'", ps->capacity->filename);
+ close(ps->capacity->fd);
+ ps->capacity->fd = -1;
+ }
+ }
}
}
// read property files
int read_error = 0;
struct ps_property *pr;
- for(pr = ps->property_root; pr && !read_error; pr = pr->next) {
- struct ps_property_dim *pd;
- for(pd = pr->property_dim_root; pd; pd = pd->next) {
- if(likely(!pd->always_zero)) {
- char buffer[30 + 1];
-
- if(unlikely(pd->fd == -1)) {
- pd->fd = open(pd->filename, O_RDONLY, 0666);
+ if (ps)
+ {
+ for(pr = ps->property_root; pr && !read_error; pr = pr->next) {
+ struct ps_property_dim *pd;
+ for(pd = pr->property_dim_root; pd; pd = pd->next) {
+ if(likely(!pd->always_zero)) {
+ char buffer[30 + 1];
+
if(unlikely(pd->fd == -1)) {
- error("Cannot open file '%s'", pd->filename);
+ pd->fd = open(pd->filename, O_RDONLY, 0666);
+ if(unlikely(pd->fd == -1)) {
+ error("Cannot open file '%s'", pd->filename);
+ read_error = 1;
+ power_supply_free(ps);
+ break;
+ }
+ }
+
+ ssize_t r = read(pd->fd, buffer, 30);
+ if(unlikely(r < 1)) {
+ error("Cannot read file '%s'", pd->filename);
read_error = 1;
power_supply_free(ps);
break;
}
- }
-
- ssize_t r = read(pd->fd, buffer, 30);
- if(unlikely(r < 1)) {
- error("Cannot read file '%s'", pd->filename);
- read_error = 1;
- power_supply_free(ps);
- break;
- }
- buffer[r] = '\0';
- pd->value = str2ull(buffer);
+ buffer[r] = '\0';
+ pd->value = str2ull(buffer);
- if(unlikely(!keep_fds_open)) {
- close(pd->fd);
- pd->fd = -1;
- }
- else if(unlikely(lseek(pd->fd, 0, SEEK_SET) == -1)) {
- error("Cannot seek in file '%s'", pd->filename);
- close(pd->fd);
- pd->fd = -1;
+ if(unlikely(!keep_fds_open)) {
+ close(pd->fd);
+ pd->fd = -1;
+ }
+ else if(unlikely(lseek(pd->fd, 0, SEEK_SET) == -1)) {
+ error("Cannot seek in file '%s'", pd->filename);
+ close(pd->fd);
+ pd->fd = -1;
+ }
}
}
}