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)
Andrew Moss committed
Apr 3, 2020 at 12:35 UTC
844a2d4e03ffd7406665cc73c49b5a4f750616f3
6 files changed
+51
-49
aclk/aclk_common.c
+1
-1
@@ -42,7 +42,7 @@ ACLK_PROXY_TYPE aclk_verify_proxy(const char *string)
42
if (!string)
43
return PROXY_TYPE_UNKNOWN;
44
45
- while (*string == 0x20)
45
+ while (*string == 0x20 && *string!=0) // Help coverity (compiler will remove)
46
string++;
47
48
if (!*string)
aclk/aclk_lws_https_client.c
+1
@@ -176,6 +176,7 @@ int aclk_send_https_request(char *method, char *host, char *port, char *url, cha
176
context = lws_create_context(&info);
177
if (!context) {
178
error("Error creating LWS context");
179
+ freez(data);
180
return 1;
181
}
182
aclk/agent_cloud_link.c
+6
-19
@@ -146,29 +146,16 @@ int cloud_to_agent_parse(JSON_ENTRY *e)
146
static RSA *aclk_private_key = NULL;
147
static int create_private_key()
148
{
149
- char filename[FILENAME_MAX + 1]; struct stat statbuf;
149
+ char filename[FILENAME_MAX + 1];
150
snprintfz(filename, FILENAME_MAX, "%s/claim.d/private.pem", netdata_configured_user_config_dir);
151
152
- if (lstat(filename, &statbuf) != 0) {
153
- error("Claimed agent cannot establish ACLK - private key not found '%s' failed.", filename);
152
+ long bytes_read;
153
+ char *private_key = read_by_filename(filename, &bytes_read);
154
+ if (!private_key) {
155
+ error("Claimed agent cannot establish ACLK - unable to load private key '%s' failed.", filename);
156
return 1;
157
}
156
- if (unlikely(statbuf.st_size == 0)) {
157
- info("Claimed agent cannot establish ACLK - private key '%s' is empty.", filename);
158
- return 1;
159
- }
160
-
161
- FILE *f = fopen(filename, "rt");
162
- if (unlikely(f == NULL)) {
163
- error("Claimed agent cannot establish ACLK - unable to open private key '%s'.", filename);
164
- return 1;
165
- }
166
-
167
- char *private_key = callocz(1, statbuf.st_size + 1);
168
- size_t bytes_read = fread(private_key, 1, statbuf.st_size, f);
169
- private_key[bytes_read] = 0;
170
- debug(D_ACLK, "Claimed agent loaded private key len=%zu bytes", bytes_read);
171
- fclose(f);
158
+ debug(D_ACLK, "Claimed agent loaded private key len=%ld bytes", bytes_read);
159
160
BIO *key_bio = BIO_new_mem_buf(private_key, -1);
161
if (key_bio==NULL) {
claim/claim.c
+4
-29
@@ -51,15 +51,7 @@ void claim_agent(char *claiming_arguments)
51
char command_buffer[CLAIMING_COMMAND_LENGTH + 1];
52
FILE *fp;
53
54
- char *cloud_base_hostname = NULL; // Initializers are over-written but prevent gcc complaining about clobbering.
55
- char *cloud_base_port = NULL;
54
char *cloud_base_url = config_get(CONFIG_SECTION_CLOUD, "cloud base url", DEFAULT_CLOUD_BASE_URL);
57
- if( aclk_decode_base_url(cloud_base_url, &cloud_base_hostname, &cloud_base_port))
58
- {
59
- error("Configuration error - cannot decode \"cloud base url\"");
60
- return;
61
- }
62
-
55
const char *proxy_str;
56
ACLK_PROXY_TYPE proxy_type;
57
char proxy_flag[CLAIMING_PROXY_LENGTH] = "-noproxy";
@@ -120,31 +112,14 @@ void load_claiming_state(void)
112
}
113
114
char filename[FILENAME_MAX + 1];
123
- struct stat statbuf;
124
-
115
snprintfz(filename, FILENAME_MAX, "%s/claim.d/claimed_id", netdata_configured_user_config_dir);
116
127
- // check if the file exists
128
- if (lstat(filename, &statbuf) != 0) {
129
- info("lstat on File '%s' failed reason=\"%s\". Setting state to AGENT_UNCLAIMED.", filename, strerror(errno));
130
- return;
131
- }
132
- if (unlikely(statbuf.st_size == 0)) {
133
- info("File '%s' has no contents. Setting state to AGENT_UNCLAIMED.", filename);
134
- return;
135
- }
136
-
137
- FILE *f = fopen(filename, "rt");
138
- if (unlikely(f == NULL)) {
139
- error("File '%s' cannot be opened. Setting state to AGENT_UNCLAIMED.", filename);
117
+ long bytes_read;
118
+ claimed_id = read_by_filename(filename, &bytes_read);
119
+ if (!claimed_id) {
120
+ info("Unable to load '%s', setting state to AGENT_UNCLAIMED", filename);
121
return;
122
}
123
143
- claimed_id = callocz(1, statbuf.st_size + 1);
144
- size_t bytes_read = fread(claimed_id, 1, statbuf.st_size, f);
145
- claimed_id[bytes_read] = 0;
124
info("File '%s' was found. Setting state to AGENT_CLAIMED.", filename);
147
- fclose(f);
148
-
149
- snprintfz(filename, FILENAME_MAX, "%s/claim.d/private.pem", netdata_configured_user_config_dir);
125
}
libnetdata/libnetdata.c
+38
@@ -1453,3 +1453,41 @@ void recursive_config_double_dir_load(const char *user_path, const char *stock_p
1453
freez(udir);
1454
freez(sdir);
1455
}
1456
+
1457
+// Returns the number of bytes read from the file if file_size is not NULL.
1458
+// The actual buffer has an extra byte set to zero (not included in the count).
1459
+char *read_by_filename(char *filename, long *file_size)
1460
+{
1461
+ FILE *f = fopen(filename, "r");
1462
+ if (!f)
1463
+ return NULL;
1464
+ if (fseek(f, 0, SEEK_END) < 0) {
1465
+ fclose(f);
1466
+ return NULL;
1467
+ }
1468
+ long size = ftell(f);
1469
+ if (size <= 0 || fseek(f, 0, SEEK_END) < 0) {
1470
+ fclose(f);
1471
+ return NULL;
1472
+ }
1473
+ char *contents = callocz(size + 1, 1);
1474
+ if (!contents) {
1475
+ fclose(f);
1476
+ return NULL;
1477
+ }
1478
+ if (fseek(f, 0, SEEK_SET) < 0) {
1479
+ fclose(f);
1480
+ freez(contents);
1481
+ return NULL;
1482
+ }
1483
+ size_t res = fread(contents, 1, size, f);
1484
+ if ( res != (size_t)size) {
1485
+ freez(contents);
1486
+ fclose(f);
1487
+ return NULL;
1488
+ }
1489
+ fclose(f);
1490
+ if (file_size)
1491
+ *file_size = size;
1492
+ return contents;
1493
+}
libnetdata/libnetdata.h
+1
@@ -278,6 +278,7 @@ extern void recursive_config_double_dir_load(
278
, void *data
279
, size_t depth
280
);
281
+extern char *read_by_filename(char *filename, long *file_size);
282
283
/* fix for alpine linux */
284
#ifndef RUSAGE_THREAD