Skip to content

Commit de9c4ff

Browse files
author
Peter Schiffer
committed
Fixes from static analysis
This patch fixes multiple issues found by static analysis.
1 parent 33b0ce6 commit de9c4ff

16 files changed

+122
-104
lines changed

cifsiostat.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,8 @@ void save_stats(char *name, int curr, struct cifs_stats *st_io)
241241
/* Unused entry found... */
242242
st_hdr_cifs_i->used = TRUE; /* Indicate it is now used */
243243
st_hdr_cifs_i->active = TRUE;
244-
strcpy(st_hdr_cifs_i->name, name);
244+
strncpy(st_hdr_cifs_i->name, name, MAX_NAME_LEN - 1);
245+
st_hdr_cifs_i->name[MAX_NAME_LEN - 1] = '\0';
245246
st_cifs_i = st_cifs[curr] + i;
246247
*st_cifs_i = *((struct cifs_stats *) st_io);
247248
break;
@@ -283,7 +284,8 @@ void save_stats(char *name, int curr, struct cifs_stats *st_io)
283284
st_hdr_cifs_i = st_hdr_cifs + i;
284285
st_hdr_cifs_i->used = TRUE; /* Indicate it is now used */
285286
st_hdr_cifs_i->active = TRUE;
286-
strcpy(st_hdr_cifs_i->name, name);
287+
strncpy(st_hdr_cifs_i->name, name, MAX_NAME_LEN - 1);
288+
st_hdr_cifs_i->name[MAX_NAME_LEN - 1] = '\0';
287289
st_cifs_i = st_cifs[curr] + i;
288290
*st_cifs_i = *st_io;
289291
}
@@ -318,7 +320,7 @@ void read_cifs_stat(int curr)
318320
long long unsigned all_open = 0;
319321
char cifs_name[MAX_NAME_LEN];
320322
char name_tmp[MAX_NAME_LEN];
321-
struct cifs_stats scifs;
323+
struct cifs_stats scifs = {0, 0, 0, 0, 0, 0, 0};
322324

323325
/* Every CIFS entry is potentially unregistered */
324326
set_entries_inactive();
@@ -659,7 +661,7 @@ int main(int argc, char **argv)
659661

660662
/* Set a handler for SIGALRM */
661663
memset(&alrm_act, 0, sizeof(alrm_act));
662-
alrm_act.sa_handler = (void *) alarm_handler;
664+
alrm_act.sa_handler = alarm_handler;
663665
sigaction(SIGALRM, &alrm_act, NULL);
664666
alarm(interval);
665667

common.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ time_t get_localtime(struct tm *rectime, int d_off)
8787
timer -= SEC_PER_DAY * d_off;
8888
ltm = localtime(&timer);
8989

90-
*rectime = *ltm;
90+
if (ltm) {
91+
*rectime = *ltm;
92+
}
9193
return timer;
9294
}
9395

@@ -114,7 +116,9 @@ time_t get_gmtime(struct tm *rectime, int d_off)
114116
timer -= SEC_PER_DAY * d_off;
115117
ltm = gmtime(&timer);
116118

117-
*rectime = *ltm;
119+
if (ltm) {
120+
*rectime = *ltm;
121+
}
118122
return timer;
119123
}
120124

ioconf.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ int ioc_init(void)
228228

229229
/* maybe it's a full record? */
230230

231-
i = sscanf(buf, "%u:%[^:]:%[^:]:%d:%[^:]:%u:%[^:]:%u:%s",
231+
i = sscanf(buf, "%u:%[^:]:%[^:]:%u:%[^:]:%u:%[^:]:%u:%s",
232232
&major, blkp->name,
233233
cfmt, &iocp->ctrlno,
234234
dfmt, &blkp->dcount,
@@ -294,7 +294,8 @@ int ioc_init(void)
294294

295295
/* basename of device + provided string + controller # */
296296
if (*cfmt == '*') {
297-
strcpy(blkp->cfmt, blkp->name);
297+
strncpy(blkp->cfmt, blkp->name, IOC_FMTLEN);
298+
blkp->cfmt[IOC_FMTLEN] = '\0';
298299
}
299300
else {
300301
sprintf(blkp->cfmt, "%s%s%%d", blkp->name, cfmt);

iostat.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,11 @@ void usage(char *progname)
110110
*/
111111
void set_disk_output_unit(void)
112112
{
113-
char *e;
114-
115113
if (DISPLAY_KILOBYTES(flags) || DISPLAY_MEGABYTES(flags))
116114
return;
117115

118116
/* Check POSIXLY_CORRECT environment variable */
119-
if ((e = getenv(ENV_POSIXLY_CORRECT)) == NULL) {
117+
if (getenv(ENV_POSIXLY_CORRECT) == NULL) {
120118
/* Variable not set: Unit is kB/s and not blocks/s */
121119
flags |= I_D_KILOBYTES;
122120
}
@@ -450,7 +448,8 @@ void save_stats(char *name, int curr, void *st_io, int iodev_nr,
450448
if (!st_hdr_iodev_i->used) {
451449
/* Unused entry found... */
452450
st_hdr_iodev_i->used = TRUE; /* Indicate it is now used */
453-
strcpy(st_hdr_iodev_i->name, name);
451+
strncpy(st_hdr_iodev_i->name, name, MAX_NAME_LEN - 1);
452+
st_hdr_iodev_i->name[MAX_NAME_LEN - 1] = '\0';
454453
st_iodev_i = st_iodev[!curr] + i;
455454
memset(st_iodev_i, 0, IO_STATS_SIZE);
456455
break;
@@ -740,7 +739,8 @@ void read_diskstats_stat(int curr)
740739
* (if different from "nodev") works around known issues
741740
* with EMC PowerPath.
742741
*/
743-
strncpy(dev_name, ioc_dname, MAX_NAME_LEN);
742+
strncpy(dev_name, ioc_dname, MAX_NAME_LEN - 1);
743+
dev_name[MAX_NAME_LEN - 1] = '\0';
744744
}
745745
}
746746

@@ -751,7 +751,8 @@ void read_diskstats_stat(int curr)
751751
*/
752752
dm_name = transform_devmapname(major, minor);
753753
if (dm_name) {
754-
strncpy(dev_name, dm_name, MAX_NAME_LEN);
754+
strncpy(dev_name, dm_name, MAX_NAME_LEN - 1);
755+
dev_name[MAX_NAME_LEN - 1] = '\0';
755756
}
756757
}
757758

@@ -1364,7 +1365,7 @@ int main(int argc, char **argv)
13641365
* MAX_NAME_LEN - 2: one char for the heading space,
13651366
* and one for the trailing '\0'.
13661367
*/
1367-
sprintf(group_name, " %-.*s", MAX_NAME_LEN - 2, argv[opt++]);
1368+
snprintf(group_name, MAX_NAME_LEN, " %-.*s", MAX_NAME_LEN - 2, argv[opt++]);
13681369
}
13691370
else {
13701371
usage(argv[0]);
@@ -1593,7 +1594,7 @@ int main(int argc, char **argv)
15931594

15941595
/* Set a handler for SIGALRM */
15951596
memset(&alrm_act, 0, sizeof(alrm_act));
1596-
alrm_act.sa_handler = (void *) alarm_handler;
1597+
alrm_act.sa_handler = alarm_handler;
15971598
sigaction(SIGALRM, &alrm_act, NULL);
15981599
alarm(interval);
15991600

mpstat.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ void rw_mpstat_loop(int dis_hdr, int rows)
776776

777777
/* Set a handler for SIGALRM */
778778
memset(&alrm_act, 0, sizeof(alrm_act));
779-
alrm_act.sa_handler = (void *) alarm_handler;
779+
alrm_act.sa_handler = alarm_handler;
780780
sigaction(SIGALRM, &alrm_act, NULL);
781781
alarm(interval);
782782

@@ -794,7 +794,7 @@ void rw_mpstat_loop(int dis_hdr, int rows)
794794

795795
/* Set a handler for SIGINT */
796796
memset(&int_act, 0, sizeof(int_act));
797-
int_act.sa_handler = (void *) int_handler;
797+
int_act.sa_handler = int_handler;
798798
sigaction(SIGINT, &int_act, NULL);
799799

800800
pause();

nfsiostat-sysstat.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,11 @@ void usage(char *progname)
8888
*/
8989
void set_output_unit(void)
9090
{
91-
char *e;
92-
9391
if (DISPLAY_KILOBYTES(flags) || DISPLAY_MEGABYTES(flags))
9492
return;
9593

9694
/* Check POSIXLY_CORRECT environment variable */
97-
if ((e = getenv(ENV_POSIXLY_CORRECT)) == NULL) {
95+
if (getenv(ENV_POSIXLY_CORRECT) == NULL) {
9896
/* Variable not set: Unit is kB/s and not blocks/s */
9997
flags |= I_D_KILOBYTES;
10098
}
@@ -271,7 +269,8 @@ void save_stats(char *name, int curr, void *st_io)
271269
st_hdr_ionfs_i->used = TRUE; /* Indicate it is now used */
272270
st_hdr_ionfs_i->active = TRUE;
273271

274-
strcpy(st_hdr_ionfs_i->name, name);
272+
strncpy(st_hdr_ionfs_i->name, name, MAX_NAME_LEN - 1);
273+
st_hdr_ionfs_i->name[MAX_NAME_LEN - 1] = '\0';
275274
st_ionfs_i = st_ionfs[curr] + i;
276275
memset(st_ionfs_i, 0, IO_NFS_STATS_SIZE);
277276
*st_ionfs_i = *((struct io_nfs_stats *) st_io);
@@ -309,7 +308,8 @@ void save_stats(char *name, int curr, void *st_io)
309308
/* Now i shows the first unused entry of the new block */
310309
st_hdr_ionfs_i = st_hdr_ionfs + i;
311310
st_hdr_ionfs_i->used = TRUE; /* Indicate it is now used */
312-
strcpy(st_hdr_ionfs_i->name, name);
311+
strncpy(st_hdr_ionfs_i->name, name, MAX_NAME_LEN - 1);
312+
st_hdr_ionfs_i->name[MAX_NAME_LEN - 1] = '\0';
313313
st_ionfs_i = st_ionfs[curr] + i;
314314
memset(st_ionfs_i, 0, IO_NFS_STATS_SIZE);
315315
}
@@ -344,7 +344,7 @@ void read_nfs_stat(int curr)
344344
char nfs_name[MAX_NAME_LEN];
345345
char mount[10], on[10], prefix[10], aux[32];
346346
char operation[16];
347-
struct io_nfs_stats snfs;
347+
struct io_nfs_stats snfs = {0, 0, 0, 0, 0, 0, 0, 0, 0};
348348
long int v1;
349349

350350
/* Every I/O NFS entry is potentially unregistered */
@@ -418,7 +418,7 @@ void read_nfs_stat(int curr)
418418
while (sw == 4) {
419419
if (fgets(line, sizeof(line), fp) == NULL)
420420
break;
421-
sscanf(line, "%15s %lu", operation, &v1);
421+
sscanf(line, "%15s %ld", operation, &v1);
422422
if (!strncmp(operation, "READ:", 5)) {
423423
snfs.nfs_rops = v1;
424424
}
@@ -730,7 +730,7 @@ int main(int argc, char **argv)
730730

731731
/* Set a handler for SIGALRM */
732732
memset(&alrm_act, 0, sizeof(alrm_act));
733-
alrm_act.sa_handler = (void *) alarm_handler;
733+
alrm_act.sa_handler = alarm_handler;
734734
sigaction(SIGALRM, &alrm_act, NULL);
735735
alarm(interval);
736736

pidstat.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ void init_stats(void)
140140
*/
141141
void salloc_pid_array(unsigned int len)
142142
{
143-
if ((pid_array = (unsigned int *) malloc(sizeof(int) * len)) == NULL) {
143+
if ((pid_array = (unsigned int *) malloc(sizeof(unsigned int) * len)) == NULL) {
144144
perror("malloc");
145145
exit(4);
146146
}
@@ -404,10 +404,10 @@ int read_proc_pid_status(unsigned int pid, struct pid_stats *pst,
404404
while (fgets(line, sizeof(line), fp) != NULL) {
405405

406406
if (!strncmp(line, "Uid:", 4)) {
407-
sscanf(line + 5, "%d", &pst->uid);
407+
sscanf(line + 5, "%u", &pst->uid);
408408
}
409409
else if (!strncmp(line, "Threads:", 8)) {
410-
sscanf(line + 9, "%d", &pst->threads);
410+
sscanf(line + 9, "%u", &pst->threads);
411411
}
412412
else if (!strncmp(line, "voluntary_ctxt_switches:", 24)) {
413413
sscanf(line + 25, "%lu", &pst->nvcsw);
@@ -523,7 +523,7 @@ int read_proc_pid_cmdline(unsigned int pid, struct pid_stats *pst,
523523

524524
memset(line, 0, MAX_CMDLINE_LEN);
525525

526-
if ((len = fread(line, 1, MAX_CMDLINE_LEN - 1, fp)) < 0) {
526+
if ((len = fread(line, 1, MAX_CMDLINE_LEN - 1, fp)) == 0) {
527527
/* Nothing to read doesn't mean that process no longer exists */
528528
fclose(fp);
529529
return 1;
@@ -536,7 +536,8 @@ int read_proc_pid_cmdline(unsigned int pid, struct pid_stats *pst,
536536
}
537537

538538
fclose(fp);
539-
strncpy(pst->cmdline, line, MAX_CMDLINE_LEN);
539+
strncpy(pst->cmdline, line, MAX_CMDLINE_LEN - 1);
540+
pst->cmdline[MAX_CMDLINE_LEN - 1] = '\0';
540541
return 0;
541542
}
542543

@@ -2331,7 +2332,7 @@ void rw_pidstat_loop(int dis_hdr, int rows)
23312332

23322333
/* Set a handler for SIGALRM */
23332334
memset(&alrm_act, 0, sizeof(alrm_act));
2334-
alrm_act.sa_handler = (void *) alarm_handler;
2335+
alrm_act.sa_handler = alarm_handler;
23352336
sigaction(SIGALRM, &alrm_act, NULL);
23362337
alarm(interval);
23372338

@@ -2343,7 +2344,7 @@ void rw_pidstat_loop(int dis_hdr, int rows)
23432344

23442345
/* Set a handler for SIGINT */
23452346
memset(&int_act, 0, sizeof(int_act));
2346-
int_act.sa_handler = (void *) int_handler;
2347+
int_act.sa_handler = int_handler;
23472348
sigaction(SIGINT, &int_act, NULL);
23482349

23492350
/* Wait for SIGALRM (or possibly SIGINT) signal */

rd_sensors.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ void read_fan(struct stats_pwr_fan *st_pwr_fan, int nbr)
6161
int i, j;
6262

6363
memset(st_pwr_fan, 0, STATS_PWR_FAN_SIZE);
64-
int err = 0;
6564

6665
while ((chip = sensors_get_detected_chips(NULL, &chip_nr))) {
6766
i = 0;
@@ -74,12 +73,12 @@ void read_fan(struct stats_pwr_fan *st_pwr_fan, int nbr)
7473
while ((sub = sensors_get_all_subfeatures(chip, feature, &j))) {
7574
if ((sub->type == SENSORS_SUBFEATURE_FAN_INPUT) &&
7675
(sub->flags & SENSORS_MODE_R)) {
77-
if ((err = sensors_get_value(chip, sub->number, &st_pwr_fan_i->rpm))) {
76+
if (sensors_get_value(chip, sub->number, &st_pwr_fan_i->rpm)) {
7877
st_pwr_fan_i->rpm = 0;
7978
}
8079
}
8180
else if ((sub->type == SENSORS_SUBFEATURE_FAN_MIN)) {
82-
if ((err = sensors_get_value(chip, sub->number, &st_pwr_fan_i->rpm_min))) {
81+
if (sensors_get_value(chip, sub->number, &st_pwr_fan_i->rpm_min)) {
8382
st_pwr_fan_i->rpm_min = 0;
8483
}
8584
}
@@ -115,7 +114,6 @@ void read_temp(struct stats_pwr_temp *st_pwr_temp, int nbr)
115114
int i, j;
116115

117116
memset(st_pwr_temp, 0, STATS_PWR_TEMP_SIZE);
118-
int err = 0;
119117

120118
while ((chip = sensors_get_detected_chips(NULL, &chip_nr))) {
121119
i = 0;
@@ -128,17 +126,17 @@ void read_temp(struct stats_pwr_temp *st_pwr_temp, int nbr)
128126
while ((sub = sensors_get_all_subfeatures(chip, feature, &j))) {
129127
if ((sub->type == SENSORS_SUBFEATURE_TEMP_INPUT) &&
130128
(sub->flags & SENSORS_MODE_R)) {
131-
if ((err = sensors_get_value(chip, sub->number, &st_pwr_temp_i->temp))) {
129+
if (sensors_get_value(chip, sub->number, &st_pwr_temp_i->temp)) {
132130
st_pwr_temp_i->temp = 0;
133131
}
134132
}
135133
else if ((sub->type == SENSORS_SUBFEATURE_TEMP_MIN)) {
136-
if ((err = sensors_get_value(chip, sub->number, &st_pwr_temp_i->temp_min))) {
134+
if (sensors_get_value(chip, sub->number, &st_pwr_temp_i->temp_min)) {
137135
st_pwr_temp_i->temp_min = 0;
138136
}
139137
}
140138
else if ((sub->type == SENSORS_SUBFEATURE_TEMP_MAX)) {
141-
if ((err = sensors_get_value(chip, sub->number, &st_pwr_temp_i->temp_max))) {
139+
if (sensors_get_value(chip, sub->number, &st_pwr_temp_i->temp_max)) {
142140
st_pwr_temp_i->temp_max = 0;
143141
}
144142
}
@@ -174,7 +172,6 @@ void read_in(struct stats_pwr_in *st_pwr_in, int nbr)
174172
int i, j;
175173

176174
memset(st_pwr_in, 0, STATS_PWR_IN_SIZE);
177-
int err = 0;
178175

179176
while ((chip = sensors_get_detected_chips(NULL, &chip_nr))) {
180177
i = 0;
@@ -187,17 +184,17 @@ void read_in(struct stats_pwr_in *st_pwr_in, int nbr)
187184
while ((sub = sensors_get_all_subfeatures(chip, feature, &j))) {
188185
if ((sub->type == SENSORS_SUBFEATURE_IN_INPUT) &&
189186
(sub->flags & SENSORS_MODE_R)) {
190-
if ((err = sensors_get_value(chip, sub->number, &st_pwr_in_i->in))) {
187+
if (sensors_get_value(chip, sub->number, &st_pwr_in_i->in)) {
191188
st_pwr_in_i->in = 0;
192189
}
193190
}
194191
else if ((sub->type == SENSORS_SUBFEATURE_IN_MIN)) {
195-
if ((err = sensors_get_value(chip, sub->number, &st_pwr_in_i->in_min))) {
192+
if (sensors_get_value(chip, sub->number, &st_pwr_in_i->in_min)) {
196193
st_pwr_in_i->in_min = 0;
197194
}
198195
}
199196
else if ((sub->type == SENSORS_SUBFEATURE_IN_MAX)) {
200-
if ((err = sensors_get_value(chip, sub->number, &st_pwr_in_i->in_max))) {
197+
if (sensors_get_value(chip, sub->number, &st_pwr_in_i->in_max)) {
201198
st_pwr_in_i->in_max = 0;
202199
}
203200
}

rd_stats.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ void read_loadavg(struct stats_queue *st_queue)
420420
return;
421421

422422
/* Read load averages and queue length */
423-
rc = fscanf(fp, "%d.%d %d.%d %d.%d %ld/%d %*d\n",
423+
rc = fscanf(fp, "%d.%u %d.%u %d.%u %lu/%u %*d\n",
424424
&load_tmp[0], &st_queue->load_avg_1,
425425
&load_tmp[1], &st_queue->load_avg_5,
426426
&load_tmp[2], &st_queue->load_avg_15,

rd_stats.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
#ifndef _RD_STATS_H
77
#define _RD_STATS_H
88

9-
#include "common.h"
10-
119

1210
/*
1311
***************************************************************************

0 commit comments

Comments
 (0)