improvements identified via static code analysis with cppcheck
Costa Tsaousis (ktsaou) committed
Nov 13, 2016 at 12:20 UTC
a19ead105eeedcf7b01ec14a892d2e799e4dcba6
24 files changed
+38
-63
src/apps_plugin.c
+2
-3
@@ -2787,7 +2787,6 @@ int main(int argc, char **argv)
2787
procfile_adaptive_initial_allocation = 1;
2788
2789
time_t started_t = time(NULL);
2790
- time_t current_t;
2790
get_system_HZ();
2791
get_system_pid_max();
2792
get_system_cpus();
@@ -2831,12 +2830,12 @@ int main(int argc, char **argv)
2830
2831
#ifndef PROFILING_MODE
2832
unsigned long long sunext = (time(NULL) - (time(NULL) % update_every) + update_every) * 1000000ULL;
2834
- unsigned long long sunow;
2833
#endif /* PROFILING_MODE */
2834
2835
global_iterations_counter = 1;
2836
for(;1; global_iterations_counter++) {
2837
#ifndef PROFILING_MODE
2838
+ unsigned long long sunow;
2839
// delay until it is our time to run
2840
while((sunow = time_usec()) < sunext)
2841
sleep_usec(sunext - sunow);
@@ -2878,7 +2877,7 @@ int main(int argc, char **argv)
2877
if(unlikely(debug))
2878
fprintf(stderr, "apps.plugin: done Loop No %llu\n", global_iterations_counter);
2879
2881
- current_t = time(NULL);
2880
+ time_t current_t = time(NULL);
2881
2882
#ifndef PROFILING_MODE
2883
// restart check (14400 seconds)
src/common.c
+4
-11
@@ -864,11 +864,9 @@ uint32_t simple_hash(const char *name) {
864
*/
865
866
void strreverse(char *begin, char *end) {
867
- char aux;
868
-
867
while (end > begin) {
868
// clearer code.
871
- aux = *end;
869
+ char aux = *end;
870
*end-- = *begin;
871
*begin++ = aux;
872
}
@@ -905,11 +903,10 @@ void *mymmap(const char *filename, size_t size, int flags, int ksm) {
903
#ifdef MADV_MERGEABLE
904
static int log_madvise_2 = 1, log_madvise_3 = 1;
905
#endif
908
- int fd;
906
void *mem = NULL;
907
908
errno = 0;
912
- fd = open(filename, O_RDWR | O_CREAT | O_NOATIME, 0664);
909
+ int fd = open(filename, O_RDWR | O_CREAT | O_NOATIME, 0664);
910
if (fd != -1) {
911
if (lseek(fd, size, SEEK_SET) == (off_t) size) {
912
if (write(fd, "", 1) == 1) {
@@ -1090,14 +1087,12 @@ int snprintfz(char *dst, size_t n, const char *fmt, ...) {
1087
1088
int processors = 1;
1089
long get_system_cpus(void) {
1093
- procfile *ff = NULL;
1094
-
1090
processors = 1;
1091
1092
char filename[FILENAME_MAX + 1];
1093
snprintfz(filename, FILENAME_MAX, "%s/proc/stat", global_host_prefix);
1094
1100
- ff = procfile_open(filename, NULL, PROCFILE_FLAG_DEFAULT);
1095
+ procfile *ff = procfile_open(filename, NULL, PROCFILE_FLAG_DEFAULT);
1096
if(!ff) {
1097
error("Cannot open file '%s'. Assuming system has %d processors.", filename, processors);
1098
return processors;
@@ -1127,11 +1122,9 @@ long get_system_cpus(void) {
1122
1123
pid_t pid_max = 32768;
1124
pid_t get_system_pid_max(void) {
1130
- procfile *ff = NULL;
1131
-
1125
char filename[FILENAME_MAX + 1];
1126
snprintfz(filename, FILENAME_MAX, "%s/proc/sys/kernel/pid_max", global_host_prefix);
1134
- ff = procfile_open(filename, NULL, PROCFILE_FLAG_DEFAULT);
1127
+ procfile *ff = procfile_open(filename, NULL, PROCFILE_FLAG_DEFAULT);
1128
if(!ff) {
1129
error("Cannot open file '%s'. Assuming system supports %d pids.", filename, pid_max);
1130
return pid_max;
src/daemon.c
+1
-4
@@ -114,7 +114,6 @@ int become_user(const char *username, int pid_fd)
114
error("Cannot set supplementary groups for user '%s'", username);
115
116
freez(supplementary_groups);
117
- supplementary_groups = NULL;
117
ngroups = 0;
118
}
119
@@ -251,10 +250,8 @@ int become_daemon(int dont_fork, const char *user)
250
create_needed_dir(VARLIB_DIR, getuid(), getgid());
251
}
252
254
- if(pidfd != -1) {
253
+ if(pidfd != -1)
254
close(pidfd);
256
- pidfd = -1;
257
- }
255
256
return(0);
257
}
src/eval.c
+2
-5
@@ -1031,9 +1031,7 @@ static inline EVAL_NODE *parse_rest_of_expression(const char **string, int *erro
1031
1032
// high level function to parse an expression or a sub-expression
1033
static inline EVAL_NODE *parse_full_expression(const char **string, int *error) {
1034
- EVAL_NODE *op1 = NULL;
1035
-
1036
- op1 = parse_one_full_operand(string, error);
1034
+ EVAL_NODE *op1 = parse_one_full_operand(string, error);
1035
if(!op1) {
1036
*error = EVAL_ERROR_MISSING_OPERAND;
1037
return NULL;
@@ -1081,7 +1079,6 @@ int expression_evaluate(EVAL_EXPRESSION *exp) {
1079
EVAL_EXPRESSION *expression_parse(const char *string, const char **failed_at, int *error) {
1080
const char *s = string;
1081
int err = EVAL_ERROR_OK;
1084
- unsigned long pos = 0;
1082
1083
EVAL_NODE *op = parse_full_expression(&s, &err);
1084
@@ -1097,7 +1094,7 @@ EVAL_EXPRESSION *expression_parse(const char *string, const char **failed_at, in
1094
if (error) *error = err;
1095
1096
if(!op) {
1100
- pos = s - string + 1;
1097
+ unsigned long pos = s - string + 1;
1098
error("failed to parse expression '%s': %s at character %lu (i.e.: '%s').", string, expression_strerror(err), pos, s);
1099
return NULL;
1100
}
src/health.c
+1
-2
@@ -141,13 +141,12 @@ static inline void health_alarm_log_save(RRDHOST *host, ALARM_ENTRY *ae) {
141
142
static inline ssize_t health_alarm_log_read(RRDHOST *host, FILE *fp, const char *filename) {
143
static uint32_t max_unique_id = 0, max_alarm_id = 0;
144
- ssize_t loaded = -1, updated = -1, errored = -1, duplicate = -1;
144
145
errno = 0;
146
147
char *s, *buf = mallocz(65536 + 1);
148
size_t line = 0, len = 0;
150
- loaded = updated = errored = duplicate = 0;
149
+ ssize_t loaded = 0, updated = 0, errored = 0, duplicate = 0;
150
151
pthread_rwlock_rdlock(&host->health_log.alarm_log_rwlock);
152
src/ipc.c
+1
-1
@@ -138,7 +138,6 @@ printf ("allocated semaphores = %d\n", status.semaem);
138
*/
139
140
static inline int ipc_sem_get_status(struct ipc_status *st) {
141
- static int error_shown = 0;
141
struct seminfo seminfo;
142
union semun arg;
143
@@ -146,6 +145,7 @@ static inline int ipc_sem_get_status(struct ipc_status *st) {
145
146
if(unlikely(semctl (0, 0, SEM_INFO, arg) < 0)) {
147
/* kernel not configured for semaphores */
148
+ static int error_shown = 0;
149
if(unlikely(!error_shown)) {
150
error("IPC: kernel is not configured for semaphores");
151
error_shown = 1;
src/log.c
+2
-2
@@ -24,7 +24,7 @@ void syslog_init(void) {
24
}
25
26
int open_log_file(int fd, FILE **fp, const char *filename, int *enabled_syslog) {
27
- int f, t;
27
+ int f;
28
29
if(!filename || !*filename || !strcmp(filename, "none"))
30
filename = "/dev/null";
@@ -64,7 +64,7 @@ int open_log_file(int fd, FILE **fp, const char *filename, int *enabled_syslog)
64
65
if(fd != f && fd != -1) {
66
// it automatically closes
67
- t = dup2(f, fd);
67
+ int t = dup2(f, fd);
68
if (t == -1) {
69
error("Cannot dup2() new fd %d to old fd %d for '%s'", f, fd, filename);
70
close(f);
src/plugin_idlejitter.c
+1
-1
@@ -34,7 +34,7 @@ void *cpuidlejitter_main(void *ptr)
34
while(susec < (rrd_update_every * 1000000ULL)) {
35
36
gettimeofday(&before, NULL);
37
- usleep(sleep_ms * 1000);
37
+ sleep_usec(sleep_ms * 1000);
38
gettimeofday(&after, NULL);
39
40
// calculate the time it took for a full loop
src/plugin_proc.c
+1
-1
@@ -63,9 +63,9 @@ void *proc_main(void *ptr)
63
64
// the next time we will run - aligned properly
65
unsigned long long sunext = (time(NULL) - (time(NULL) % rrd_update_every) + rrd_update_every) * 1000000ULL;
66
- unsigned long long sunow;
66
67
for(;;) {
68
+ unsigned long long sunow;
69
if(unlikely(netdata_exit)) break;
70
71
// delay until it is our time to run
src/plugins_d.c
+1
-2
@@ -121,7 +121,6 @@ void *pluginsd_worker_thread(void *arg)
121
info("PLUGINSD: '%s' running on pid %d", cd->fullfilename, cd->pid);
122
123
RRDSET *st = NULL;
124
- char *s;
124
uint32_t hash;
125
126
while(likely(fgets(line, PLUGINSD_LINE_MAX, fp) != NULL)) {
@@ -132,7 +131,7 @@ void *pluginsd_worker_thread(void *arg)
131
// debug(D_PLUGINSD, "PLUGINSD: %s: %s", cd->filename, line);
132
133
int w = pluginsd_split_words(line, words, MAX_WORDS);
135
- s = words[0];
134
+ char *s = words[0];
135
if(unlikely(!s || !*s || !w)) {
136
// debug(D_PLUGINSD, "PLUGINSD: empty line");
137
continue;
src/proc_diskstats.c
+1
-2
@@ -480,7 +480,6 @@ int do_proc_diskstats(int update_every, unsigned long long dt) {
480
if(unlikely(!ff)) return 0; // we return 0, so that we will retry to open it next time
481
482
uint32_t lines = procfile_lines(ff), l;
483
- uint32_t words;
483
484
// this is smart enough not to reload it every time
485
mountinfo_reload(0);
@@ -500,7 +499,7 @@ int do_proc_diskstats(int update_every, unsigned long long dt) {
499
last_writes = 0, last_writesectors = 0, last_writems = 0,
500
last_busy_ms = 0;
501
503
- words = procfile_linewords(ff, l);
502
+ uint32_t words = procfile_linewords(ff, l);
503
if(unlikely(words < 14)) continue;
504
505
major = strtoul(procfile_lineword(ff, l, 0), NULL, 10);
src/proc_interrupts.c
+2
-1
@@ -52,7 +52,7 @@ int do_proc_interrupts(int update_every, unsigned long long dt) {
52
return 0; // we return 0, so that we will retry to open it next time
53
54
uint32_t lines = procfile_lines(ff), l;
55
- uint32_t words = procfile_linewords(ff, 0), w;
55
+ uint32_t words = procfile_linewords(ff, 0);
56
57
if(unlikely(!lines)) {
58
error("Cannot read /proc/interrupts, zero lines reported.");
@@ -61,6 +61,7 @@ int do_proc_interrupts(int update_every, unsigned long long dt) {
61
62
// find how many CPUs are there
63
if(unlikely(cpus == -1)) {
64
+ uint32_t w;
65
cpus = 0;
66
for(w = 0; w < words ; w++) {
67
if(unlikely(strncmp(procfile_lineword(ff, 0, w), "CPU", 3) == 0))
src/proc_meminfo.c
+1
-2
@@ -114,7 +114,6 @@ int do_proc_meminfo(int update_every, unsigned long long dt) {
114
return 0; // we return 0, so that we will retry to open it next time
115
116
uint32_t lines = procfile_lines(ff), l;
117
- uint32_t words;
117
118
int hwcorrupted = 0;
119
@@ -162,7 +161,7 @@ int do_proc_meminfo(int update_every, unsigned long long dt) {
161
HardwareCorrupted = 0;
162
163
for(l = 0; l < lines ;l++) {
165
- words = procfile_linewords(ff, l);
164
+ uint32_t words = procfile_linewords(ff, l);
165
if(unlikely(words < 2)) continue;
166
167
char *name = procfile_lineword(ff, l, 0);
src/proc_net_dev.c
+1
-2
@@ -29,14 +29,13 @@ int do_proc_net_dev(int update_every, unsigned long long dt) {
29
if(do_events == -1) do_events = config_get_boolean_ondemand("plugin:proc:/proc/net/dev", "frames, collisions, carrier counters for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
30
31
uint32_t lines = procfile_lines(ff), l;
32
- uint32_t words;
32
33
char *iface;
34
unsigned long long rbytes, rpackets, rerrors, rdrops, rfifo, rframe, rcompressed, rmulticast;
35
unsigned long long tbytes, tpackets, terrors, tdrops, tfifo, tcollisions, tcarrier, tcompressed;
36
37
for(l = 2; l < lines ;l++) {
39
- words = procfile_linewords(ff, l);
38
+ uint32_t words = procfile_linewords(ff, l);
39
if(words < 17) continue;
40
41
iface = procfile_lineword(ff, l, 0);
src/proc_net_rpc_nfs.c
+1
-2
@@ -159,14 +159,13 @@ int do_proc_net_rpc_nfs(int update_every, unsigned long long dt) {
159
if(do_proc4) do_proc4 = 1;
160
161
uint32_t lines = procfile_lines(ff), l;
162
- uint32_t words;
162
163
char *type;
164
unsigned long long net_count = 0, net_udp_count = 0, net_tcp_count = 0, net_tcp_connections = 0;
165
unsigned long long rpc_calls = 0, rpc_retransmits = 0, rpc_auth_refresh = 0;
166
167
for(l = 0; l < lines ;l++) {
169
- words = procfile_linewords(ff, l);
168
+ uint32_t words = procfile_linewords(ff, l);
169
if(!words) continue;
170
171
type = procfile_lineword(ff, l, 0);
src/proc_net_rpc_nfsd.c
+1
-2
@@ -253,7 +253,6 @@ int do_proc_net_rpc_nfsd(int update_every, unsigned long long dt) {
253
if(do_proc4ops) do_proc4ops = 1;
254
255
uint32_t lines = procfile_lines(ff), l;
256
- uint32_t words;
256
257
char *type;
258
unsigned long long rc_hits = 0, rc_misses = 0, rc_nocache = 0;
@@ -265,7 +264,7 @@ int do_proc_net_rpc_nfsd(int update_every, unsigned long long dt) {
264
unsigned long long rpc_calls = 0, rpc_bad_format = 0, rpc_bad_auth = 0, rpc_bad_client = 0;
265
266
for(l = 0; l < lines ;l++) {
268
- words = procfile_linewords(ff, l);
267
+ uint32_t words = procfile_linewords(ff, l);
268
if(!words) continue;
269
270
type = procfile_lineword(ff, l, 0);
src/proc_net_snmp6.c
+1
-2
@@ -253,7 +253,6 @@ int do_proc_net_snmp6(int update_every, unsigned long long dt) {
253
return 0; // we return 0, so that we will retry to open it next time
254
255
uint32_t lines = procfile_lines(ff), l;
256
- uint32_t words;
256
257
unsigned long long Ip6InReceives = 0ULL;
258
unsigned long long Ip6InHdrErrors = 0ULL;
@@ -349,7 +348,7 @@ int do_proc_net_snmp6(int update_every, unsigned long long dt) {
348
unsigned long long UdpLite6InCsumErrors = 0ULL;
349
350
for(l = 0; l < lines ;l++) {
352
- words = procfile_linewords(ff, l);
351
+ uint32_t words = procfile_linewords(ff, l);
352
if(unlikely(words < 2)) {
353
if(unlikely(words)) error("Cannot read /proc/net/snmp6 line %u. Expected 2 params, read %u.", l, words);
354
continue;
src/proc_net_stat_conntrack.c
+1
-2
@@ -31,13 +31,12 @@ int do_proc_net_stat_conntrack(int update_every, unsigned long long dt) {
31
return 0; // we return 0, so that we will retry to open it next time
32
33
uint32_t lines = procfile_lines(ff), l;
34
- uint32_t words;
34
35
unsigned long long aentries = 0, asearched = 0, afound = 0, anew = 0, ainvalid = 0, aignore = 0, adelete = 0, adelete_list = 0,
36
ainsert = 0, ainsert_failed = 0, adrop = 0, aearly_drop = 0, aicmp_error = 0, aexpect_new = 0, aexpect_create = 0, aexpect_delete = 0, asearch_restart = 0;
37
38
for(l = 1; l < lines ;l++) {
40
- words = procfile_linewords(ff, l);
39
+ uint32_t words = procfile_linewords(ff, l);
40
if(unlikely(words < 17)) {
41
if(unlikely(words)) error("Cannot read /proc/net/stat/nf_conntrack line. Expected 17 params, read %u.", words);
42
continue;
src/proc_self_mountinfo.c
+1
-3
@@ -159,11 +159,9 @@ static char *strdupz_decoding_octal(const char *string) {
159
160
// read the whole mountinfo into a linked list
161
struct mountinfo *mountinfo_read() {
162
- procfile *ff = NULL;
163
-
162
char filename[FILENAME_MAX + 1];
163
snprintfz(filename, FILENAME_MAX, "%s/proc/self/mountinfo", global_host_prefix);
166
- ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
164
+ procfile *ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
165
if(unlikely(!ff)) {
166
snprintfz(filename, FILENAME_MAX, "%s/proc/1/mountinfo", global_host_prefix);
167
ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
src/proc_softirqs.c
+2
-1
@@ -50,7 +50,7 @@ int do_proc_softirqs(int update_every, unsigned long long dt) {
50
if(unlikely(!ff)) return 0; // we return 0, so that we will retry to open it next time
51
52
uint32_t lines = procfile_lines(ff), l;
53
- uint32_t words = procfile_linewords(ff, 0), w;
53
+ uint32_t words = procfile_linewords(ff, 0);
54
55
if(unlikely(!lines)) {
56
error("Cannot read /proc/softirqs, zero lines reported.");
@@ -59,6 +59,7 @@ int do_proc_softirqs(int update_every, unsigned long long dt) {
59
60
// find how many CPUs are there
61
if(unlikely(cpus == -1)) {
62
+ uint32_t w;
63
cpus = 0;
64
for(w = 0; w < words ; w++) {
65
if(unlikely(strncmp(procfile_lineword(ff, 0, w), "CPU", 3) == 0))
src/proc_vmstat.c
+1
-2
@@ -209,7 +209,6 @@ int do_proc_vmstat(int update_every, unsigned long long dt) {
209
if(unlikely(!ff)) return 0; // we return 0, so that we will retry to open it next time
210
211
uint32_t lines = procfile_lines(ff), l;
212
- uint32_t words;
212
213
// unsigned long long allocstall = 0ULL;
214
// unsigned long long compact_blocks_moved = 0ULL;
@@ -305,7 +304,7 @@ int do_proc_vmstat(int update_every, unsigned long long dt) {
304
// unsigned long long unevictable_pgs_stranded = 0ULL;
305
306
for(l = 0; l < lines ;l++) {
308
- words = procfile_linewords(ff, l);
307
+ uint32_t words = procfile_linewords(ff, l);
308
if(unlikely(words < 2)) {
309
if(unlikely(words)) error("Cannot read /proc/vmstat line %u. Expected 2 params, read %u.", l, words);
310
continue;
src/procfile.c
+6
-6
@@ -267,12 +267,12 @@ cleanup:
267
procfile *procfile_readall(procfile *ff) {
268
debug(D_PROCFILE, PF_PREFIX ": Reading file '%s'.", ff->filename);
269
270
- ssize_t s, r = 1, x;
270
+ ssize_t r = 1;
271
ff->len = 0;
272
273
while(likely(r > 0)) {
274
- s = ff->len;
275
- x = ff->size - s;
274
+ ssize_t s = ff->len;
275
+ ssize_t x = ff->size - s;
276
277
if(unlikely(!x)) {
278
debug(D_PROCFILE, PF_PREFIX ": Expanding data buffer for file '%s'.", ff->filename);
@@ -316,13 +316,13 @@ procfile *procfile_readall(procfile *ff) {
316
317
static void procfile_set_separators(procfile *ff, const char *separators) {
318
static char def[256] = { [0 ... 255] = 0 };
319
- int i;
319
320
if(unlikely(!def[255])) {
321
// this is thread safe
322
// we check that the last byte is non-zero
323
// if it is zero, multiple threads may be executing this at the same time
324
// setting in def[] the exact same values
325
+ int i;
326
for(i = 0; likely(i < 256) ;i++) {
327
if(unlikely(i == '\n' || i == '\r')) def[i] = PF_CHAR_IS_NEWLINE;
328
else if(unlikely(isspace(i) || !isprint(i))) def[i] = PF_CHAR_IS_SEPARATOR;
@@ -443,16 +443,16 @@ procfile *procfile_reopen(procfile *ff, const char *filename, const char *separa
443
444
void procfile_print(procfile *ff) {
445
uint32_t lines = procfile_lines(ff), l;
446
- uint32_t words, w;
446
char *s;
447
448
debug(D_PROCFILE, "File '%s' with %u lines and %u words", ff->filename, ff->lines->len, ff->words->len);
449
450
for(l = 0; likely(l < lines) ;l++) {
452
- words = procfile_linewords(ff, l);
451
+ uint32_t words = procfile_linewords(ff, l);
452
453
debug(D_PROCFILE, " line %u starts at word %u and has %u words", l, ff->lines->lines[l].first, ff->lines->lines[l].words);
454
455
+ uint32_t w;
456
for(w = 0; likely(w < words) ;w++) {
457
s = procfile_lineword(ff, l, w);
458
debug(D_PROCFILE, " [%u.%u] '%s'", l, w, s);
src/rrd.c
+1
-2
@@ -474,11 +474,10 @@ RRDSET *rrdset_create(const char *type, const char *id, const char *name, const
474
475
char fullid[RRD_ID_LENGTH_MAX + 1];
476
char fullfilename[FILENAME_MAX + 1];
477
- RRDSET *st = NULL;
477
478
snprintfz(fullid, RRD_ID_LENGTH_MAX, "%s.%s", type, id);
479
481
- st = rrdset_find(fullid);
480
+ RRDSET *st = rrdset_find(fullid);
481
if(st) {
482
error("Cannot create rrd stats for '%s', it already exists.", fullid);
483
return st;
src/sys_fs_cgroup.c
+2
-2
@@ -269,7 +269,7 @@ void cgroup_read_cpuacct_usage(struct cpuacct_usage *ca) {
269
}
270
271
unsigned long i = procfile_linewords(ff, 0);
272
- if(i <= 0) return;
272
+ if(i == 0) return;
273
274
// we may have 1 more CPU reported
275
while(i > 0) {
@@ -1437,11 +1437,11 @@ void *cgroups_main(void *ptr)
1437
1438
// the next time we will run - aligned properly
1439
unsigned long long sunext = (time(NULL) - (time(NULL) % rrd_update_every) + rrd_update_every) * 1000000ULL;
1440
- unsigned long long sunow;
1440
1441
RRDSET *stcpu_thread = NULL;
1442
1443
for(;;) {
1444
+ unsigned long long sunow;
1445
if(unlikely(netdata_exit)) break;
1446
1447
// delay until it is our time to run