From cc205e5d244d903fd73338104560ec9e1d4117bc Mon Sep 17 00:00:00 2001 From: Teemu Toivola Date: Fri, 6 Sep 2024 00:40:25 +0300 Subject: [PATCH] fix cppcheck findings --- src/daemon.c | 8 ++++---- src/dbaccess.c | 2 +- src/dbjson.c | 2 +- src/dbshow.c | 14 +++++++------- src/dbxml.c | 2 +- src/fs.c | 5 +++-- src/id.c | 4 ++-- src/ifinfo.c | 5 +++-- src/image.c | 14 +++++++------- src/image_support.c | 2 +- src/percentile.c | 2 +- 11 files changed, 31 insertions(+), 29 deletions(-) diff --git a/src/daemon.c b/src/daemon.c index 98c9f5bc..4f7146a0 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -227,7 +227,7 @@ unsigned int addinterfaces(DSTATE *s) void detectboot(DSTATE *s) { char buffer[32]; - char *btime_buffer; + const char *btime_buffer; uint64_t current_btime, db_btime; current_btime = getbtime(); @@ -359,7 +359,7 @@ void preparedatabase(DSTATE *s) unsigned int importlegacydbs(void) { DIR *dir; - struct dirent *di; + const struct dirent *di; unsigned int importcount = 0; if ((dir = opendir(cfg.dbdir)) == NULL) { @@ -787,7 +787,7 @@ void cleanremovedinterfaces(DSTATE *s) void rescandatabaseforinterfaces(DSTATE *s) { - short interface_already_monitored = 0; + short interface_already_monitored; uint64_t dbifcount = s->dbifcount; datacache *iterator = NULL; iflist *dbifl = NULL, *dbifl_iterator = NULL; @@ -1076,7 +1076,7 @@ short getcurrenthour(void) { int ret = 0; time_t current; - struct tm *stm; + const struct tm *stm; char buffer[4]; current = time(NULL); diff --git a/src/dbaccess.c b/src/dbaccess.c index 6572ce20..ba6fd141 100644 --- a/src/dbaccess.c +++ b/src/dbaccess.c @@ -32,7 +32,7 @@ int insertlegacydata(DATA *data, const char *iface) { int i, year; time_t yeartime; - struct tm *stm; + const struct tm *stm; uint64_t rx, tx; if (!db_begintransaction()) { diff --git a/src/dbjson.c b/src/dbjson.c index 8a4dba7b..9009ced4 100644 --- a/src/dbjson.c +++ b/src/dbjson.c @@ -344,7 +344,7 @@ void jsoninterfaceinfo(const interfaceinfo *ifaceinfo) void jsondate(const time_t *date, const int type) { - struct tm *d; + const struct tm *d; d = localtime(date); diff --git a/src/dbshow.c b/src/dbshow.c index 7c25dc8f..75282aa7 100644 --- a/src/dbshow.c +++ b/src/dbshow.c @@ -67,7 +67,7 @@ void showdb(const char *interface, int qmode, const char *databegin, const char void showsummary(const interfaceinfo *ifaceinfo, const int shortmode) { - struct tm *d; + const struct tm *d; char datebuff[DATEBUFFLEN]; char todaystr[DATEBUFFLEN], yesterdaystr[DATEBUFFLEN]; char fieldseparator[8]; @@ -303,11 +303,11 @@ void showlist(const interfaceinfo *ifaceinfo, const char *listname, const char * ListType listtype = LT_None; int offset = 0, i = 1; int estimatevisible = 0; - struct tm *d; + const struct tm *d; time_t current; char datebuff[DATEBUFFLEN], daybuff[DATEBUFFLEN]; char titlename[16], colname[8], stampformat[64]; - uint64_t e_rx = 0, e_tx = 0, e_secs = 0; + uint64_t e_rx = 0, e_tx = 0, e_secs; dbdatalist *datalist = NULL, *datalist_i = NULL; dbdatalistinfo datainfo; @@ -584,7 +584,7 @@ void showlist(const interfaceinfo *ifaceinfo, const char *listname, const char * void showoneline(const interfaceinfo *ifaceinfo) { - struct tm *d; + const struct tm *d; char daytemp[DATEBUFFLEN]; uint64_t div; dbdatalist *datalist = NULL; @@ -688,7 +688,7 @@ void showhours(const interfaceinfo *ifaceinfo) uint64_t max = 1; char matrix[HGLINES][81]; /* width is one over 80 so that snprintf can write the end char */ char unit[4]; - struct tm *d; + const struct tm *d; dbdatalist *datalist = NULL, *datalist_i = NULL; dbdatalistinfo datainfo; HOURDATA hourdata[24]; @@ -841,7 +841,7 @@ void showhours(const interfaceinfo *ifaceinfo) for (i = 0; i <= 7; i++) { s = (int)tmax + i + 1; for (j = 0; j < 3; j++) { - printf("%02d %" DECCONV "10.*f %" DECCONV "10.*f", ((unsigned int)s + (j * 8)) % 24, + printf("%02u %" DECCONV "10.*f %" DECCONV "10.*f", ((unsigned int)s + (j * 8)) % 24, declen, (double)hourdata[((unsigned int)s + (j * 8)) % 24].rx / (double)div, declen, (double)hourdata[((unsigned int)s + (j * 8)) % 24].tx / (double)div); @@ -869,7 +869,7 @@ void showhours(const interfaceinfo *ifaceinfo) void show95thpercentile(const interfaceinfo *ifaceinfo) { - struct tm *d; + const struct tm *d; char datebuff[DATEBUFFLEN]; percentiledata pdata; diff --git a/src/dbxml.c b/src/dbxml.c index 145e865d..017f5549 100644 --- a/src/dbxml.c +++ b/src/dbxml.c @@ -157,7 +157,7 @@ void xmlpercentile(const interfaceinfo *ifaceinfo) void xmldate(const time_t *date, const int type) { - struct tm *d; + const struct tm *d; d = localtime(date); diff --git a/src/fs.c b/src/fs.c index 05083e69..45454c47 100644 --- a/src/fs.c +++ b/src/fs.c @@ -88,7 +88,8 @@ int mkpath(const char *dir, const mode_t mode) void preparevnstatdir(const char *dir, const char *user, const char *group) { size_t i, len, lastslash = 0; - char *path, *base; + char *path; + const char *base; if (dir == NULL) { return; @@ -164,7 +165,7 @@ void updatedirowner(const char *dir, const char *user, const char *group) void updatedirownerid(const char *dir, const uid_t uid, const gid_t gid) { DIR *d; - struct dirent *di; + const struct dirent *di; struct stat statbuf; char entryname[512]; int dir_fd, file_fd; diff --git a/src/id.c b/src/id.c index 13509cab..359c8f96 100644 --- a/src/id.c +++ b/src/id.c @@ -3,7 +3,7 @@ uid_t getuser(const char *user) { - struct passwd *pw; + const struct passwd *pw; uid_t uid; if (!strlen(user)) { @@ -32,7 +32,7 @@ uid_t getuser(const char *user) gid_t getgroup(const char *group) { - struct group *gr; + const struct group *gr; gid_t gid; if (!strlen(group)) { diff --git a/src/ifinfo.c b/src/ifinfo.c index 6297e796..15a52320 100644 --- a/src/ifinfo.c +++ b/src/ifinfo.c @@ -130,7 +130,7 @@ int getiflist_linux(iflist **ifl, const int getspeed, const int validate) char interface[MAXIFLEN]; FILE *fp; DIR *dp; - struct dirent *di; + const struct dirent *di; char procline[512]; uint32_t bwlimit = 0; @@ -218,7 +218,8 @@ int getiflist_bsd(iflist **ifl, const int getspeed, const int validate) int readproc(const char *iface) { FILE *fp; - char temp[4][64], procline[512], *proclineptr, ifaceid[MAXIFLEN + 1]; + char temp[4][64], procline[512], ifaceid[MAXIFLEN + 1]; + const char *proclineptr; int check; if ((fp = fopen(PROCNETDEV, "r")) == NULL) { diff --git a/src/image.c b/src/image.c index 81d77ca0..7d87d41f 100644 --- a/src/image.c +++ b/src/image.c @@ -119,7 +119,7 @@ int drawhours(IMAGECONTENT *ic, const int xpos, const int ypos, const int israte double ratediv; uint64_t max = 1, scaleunit = 0; char buffer[32]; - struct tm *d; + const struct tm *d; dbdatalist *datalist = NULL, *datalist_i = NULL; dbdatalistinfo datainfo; HOURDATA hourdata[24]; @@ -315,10 +315,10 @@ void drawlist(IMAGECONTENT *ic, const char *listname) int width, height, headermod, i = 1, rowcount = 0; int estimateavailable = 0, estimatevisible = 0; int32_t limit; - uint64_t e_rx = 0, e_tx = 0, e_secs = 0; + uint64_t e_rx = 0, e_tx = 0, e_secs; char buffer[512], datebuff[16], daybuff[16]; char stampformat[64], titlename[16], colname[8]; - struct tm *d; + const struct tm *d; time_t current; dbdatalist *datalist = NULL, *datalist_i = NULL; dbdatalistinfo datainfo; @@ -703,7 +703,7 @@ void drawsummary(IMAGECONTENT *ic, const int layout, const int israte) void drawsummary_alltime(IMAGECONTENT *ic, const int x, const int y) { - struct tm *d; + const struct tm *d; char buffer[512], datebuff[16], daytemp[32]; gdFontPtr titlefont; @@ -736,7 +736,7 @@ void drawsummary_digest(IMAGECONTENT *ic, const int x, const int y, const char * double rxp, txp, mod; char buffer[512], datebuff[16], daytemp[32]; time_t yesterday; - struct tm *d = NULL; + const struct tm *d = NULL; dbdatalist *datalist = NULL; dbdatalist *data_current = NULL, *data_previous = NULL; dbdatalistinfo datainfo; @@ -932,7 +932,7 @@ int drawfiveminutes(IMAGECONTENT *ic, const int xpos, const int ypos, const int time_t timestamp; double ratediv, e; char buffer[32]; - struct tm *d; + const struct tm *d; dbdatalist *datalist = NULL, *datalist_i = NULL; dbdatalistinfo datainfo; gdFontPtr font; @@ -1142,7 +1142,7 @@ void drawpercentile(IMAGECONTENT *ic, const int mode, const int xpos, const int int i, l, x = xpos, y = ypos, s = 0, step = 0, prev = 0, last = 0, color; uint64_t scaleunit, max, percentile; double ratediv, percentileratediv; - struct tm *d; + const struct tm *d; time_t current; char datebuff[DATEBUFFLEN]; dbdatalist *datalist = NULL, *datalist_i = NULL; diff --git a/src/image_support.c b/src/image_support.c index 131cd8a4..feae1072 100644 --- a/src/image_support.c +++ b/src/image_support.c @@ -120,7 +120,7 @@ void colorinitcheck(const char *color, const int value, const char *cfgtext, con void layoutinit(IMAGECONTENT *ic, const char *title, const int width, const int height) { - struct tm *d; + const struct tm *d; char datestring[64], buffer[512]; gdFontPtr datefont; diff --git a/src/percentile.c b/src/percentile.c index 2a1fc14a..ec57ecfd 100644 --- a/src/percentile.c +++ b/src/percentile.c @@ -6,7 +6,7 @@ int getpercentiledata(percentiledata *pdata, const char *iface, const uint64_t u { uint32_t entry = 0, entrylimit; uint64_t *rxdata, *txdata, *sumdata; - struct tm *d; + const struct tm *d; char datebuff[DATEBUFFLEN]; dbdatalist *datalist = NULL, *datalist_i = NULL; dbdatalistinfo datainfo;