summaryrefslogtreecommitdiffstats
path: root/libnetdata
diff options
context:
space:
mode:
authorAndrew Moss <1043609+amoss@users.noreply.github.com>2020-04-03 12:35:00 +0200
committerGitHub <noreply@github.com>2020-04-03 12:35:00 +0200
commit844a2d4e03ffd7406665cc73c49b5a4f750616f3 (patch)
treed7a3e84c109b16f93514df5820e055c759c5e57e /libnetdata
parentc7d8aecfe9675b04ae5acaa84539baec46dd3d2a (diff)
Fix Coverity defects (#8579)
Fix Coverity CID355287 and CID355289: technically it is a false-positive but it is easier to put a pattern in the code that they can recognise as a sanitizer. The compiler will remove it during optimization. Fix CID353973: the security condition is unlikely to occur but we can avoid it completely. Fix resource leak from CID 355286 and CID 355288. Fixing new resource leak introduced by a previous commit (CID355449)
Diffstat (limited to 'libnetdata')
-rw-r--r--libnetdata/libnetdata.c38
-rw-r--r--libnetdata/libnetdata.h1
2 files changed, 39 insertions, 0 deletions
diff --git a/libnetdata/libnetdata.c b/libnetdata/libnetdata.c
index 70b656d838..c9b7ab1983 100644
--- a/libnetdata/libnetdata.c
+++ b/libnetdata/libnetdata.c
@@ -1453,3 +1453,41 @@ void recursive_config_double_dir_load(const char *user_path, const char *stock_p
freez(udir);
freez(sdir);
}
+
+// Returns the number of bytes read from the file if file_size is not NULL.
+// The actual buffer has an extra byte set to zero (not included in the count).
+char *read_by_filename(char *filename, long *file_size)
+{
+ FILE *f = fopen(filename, "r");
+ if (!f)
+ return NULL;
+ if (fseek(f, 0, SEEK_END) < 0) {
+ fclose(f);
+ return NULL;
+ }
+ long size = ftell(f);
+ if (size <= 0 || fseek(f, 0, SEEK_END) < 0) {
+ fclose(f);
+ return NULL;
+ }
+ char *contents = callocz(size + 1, 1);
+ if (!contents) {
+ fclose(f);
+ return NULL;
+ }
+ if (fseek(f, 0, SEEK_SET) < 0) {
+ fclose(f);
+ freez(contents);
+ return NULL;
+ }
+ size_t res = fread(contents, 1, size, f);
+ if ( res != (size_t)size) {
+ freez(contents);
+ fclose(f);
+ return NULL;
+ }
+ fclose(f);
+ if (file_size)
+ *file_size = size;
+ return contents;
+}
diff --git a/libnetdata/libnetdata.h b/libnetdata/libnetdata.h
index b7fa9d5aba..45ac6e81bf 100644
--- a/libnetdata/libnetdata.h
+++ b/libnetdata/libnetdata.h
@@ -278,6 +278,7 @@ extern void recursive_config_double_dir_load(
, void *data
, size_t depth
);
+extern char *read_by_filename(char *filename, long *file_size);
/* fix for alpine linux */
#ifndef RUSAGE_THREAD