diff options
author | thiagoftsm <thiagoftsm@gmail.com> | 2019-09-20 11:45:11 +0000 |
---|---|---|
committer | Chris Akritidis <43294513+cakrit@users.noreply.github.com> | 2019-09-20 13:45:11 +0200 |
commit | f555472a8ceb189407b103570be9a28031819d4b (patch) | |
tree | f6033bcc7c9f2c29b0320f360d9e8d95ed228f01 /health | |
parent | 46c4b8809cad6a0dc65d91e54429e8a7a581c5a2 (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 'health')
-rw-r--r-- | health/health.c | 38 |
1 files changed, 18 insertions, 20 deletions
diff --git a/health/health.c b/health/health.c index 1460b5ba48..ed43b146c2 100644 --- a/health/health.c +++ b/health/health.c @@ -45,32 +45,30 @@ inline char *health_stock_config_dir(void) { * Function used to initialize the silencer structure. */ void health_silencers_init(void) { - struct stat statbuf; - if (!stat(silencers_filename,&statbuf)) { - off_t length = statbuf.st_size; + FILE *fd = fopen(silencers_filename, "r"); + if (fd) { + fseek(fd, 0 , SEEK_END); + off_t length = (off_t) ftell(fd); + fseek(fd, 0 , SEEK_SET); + if (length && length < HEALTH_SILENCERS_MAX_FILE_LEN) { - FILE *fd = fopen(silencers_filename, "r"); - if (fd) { - char *str = mallocz((length+1)* sizeof(char)); - if(str) { - size_t copied; - copied = fread(str, sizeof(char), length, fd); - if (copied == (length* sizeof(char))) { - str[length] = 0x00; - json_parse(str, NULL, health_silencers_json_read_callback); - info("Parsed health silencers file %s", silencers_filename); - } else { - error("Cannot read the data from health silencers file %s", silencers_filename); - } - freez(str); + char *str = mallocz((length+1)* sizeof(char)); + if(str) { + size_t copied; + copied = fread(str, sizeof(char), length, fd); + if (copied == (length* sizeof(char))) { + str[length] = 0x00; + json_parse(str, NULL, health_silencers_json_read_callback); + info("Parsed health silencers file %s", silencers_filename); + } else { + error("Cannot read the data from health silencers file %s", silencers_filename); } - fclose(fd); - } else { - error("Cannot open the file %s",silencers_filename); + freez(str); } } else { error("Health silencers file %s has the size %ld that is out of range[ 1 , %d ]. Aborting read.", silencers_filename, length, HEALTH_SILENCERS_MAX_FILE_LEN); } + fclose(fd); } else { error("Cannot open the file %s",silencers_filename); } |