From 18f21693afe3a169acee4820dd8b75814bfb4e63 Mon Sep 17 00:00:00 2001 From: Dianne Skoll Date: Tue, 7 Feb 2023 10:28:02 -0500 Subject: [PATCH] Clean up some warnings from cppcheck static analyzer. --- src/calendar.c | 3 +-- src/expr.c | 4 +--- src/funcs.c | 2 +- src/globals.h | 2 +- src/json.c | 44 ++++++++++++++++++++++---------------------- src/main.c | 2 +- src/protos.h | 3 --- src/rem2ps.c | 6 +++--- src/token.c | 1 - src/trigger.c | 37 ------------------------------------- src/userfns.c | 3 ++- src/utils.c | 23 ----------------------- 12 files changed, 32 insertions(+), 98 deletions(-) diff --git a/src/calendar.c b/src/calendar.c index ad2b5f5f..0cc5c058 100644 --- a/src/calendar.c +++ b/src/calendar.c @@ -497,7 +497,7 @@ get_month_abbrev(char const *mon) return buf; #else char *s; - wchar_t tmp_buf[128]; + wchar_t tmp_buf[128] = {0}; wchar_t *ws; int i; int len; @@ -621,7 +621,6 @@ Colorize256(int r, int g, int b, int bg, int clamp) best = (int) i; } } - cur = &XTerm256Colors[best]; if (bg) { sprintf(buf, "\x1B[48;5;%dm", best); } else { diff --git a/src/expr.c b/src/expr.c index 609bf30c..0bd6f72d 100644 --- a/src/expr.c +++ b/src/expr.c @@ -376,12 +376,10 @@ int Evaluate(char const **s, Var *locals, ParsePtr p) DBufFree(&ExprBuf); r = Evaluate(s, locals, p); /* Leaves the last parsed token in ExprBuf */ if (r) return r; - r = OK; - if (*DBufValue(&ExprBuf) != ')') { + if (*DBufValue(&ExprBuf) != ')') { DBufFree(&ExprBuf); return E_MISS_RIGHT_PAREN; } - if (r) return r; } else if (*DBufValue(&ExprBuf) == '+') { continue; /* Ignore unary + */ } diff --git a/src/funcs.c b/src/funcs.c index d2fdc7c6..7814890b 100644 --- a/src/funcs.c +++ b/src/funcs.c @@ -957,7 +957,7 @@ static int parse_color_helper(char const *str, int *r, int *g, int *b) static int FAnsicolor(func_info *info) { int r=0, g=0, b=0, bg=0, clamp=1; - int status = 0; + int status; int index = 0; bg = 0; clamp = 1; diff --git a/src/globals.h b/src/globals.h index c63f0085..af59f4d2 100644 --- a/src/globals.h +++ b/src/globals.h @@ -39,7 +39,7 @@ EXTERN FILE *ErrFp; #define IsLeapYear(y) (((y) % 4) ? 0 : ((!((y) % 100) && ((y) % 400)) ? 0 : 1 )) #define DaysInMonth(m, y) ((m) != 1 ? MonthDays[m] : 28 + IsLeapYear(y)) -#define DestroyValue(x) (void) (((x).type == STR_TYPE && (x).v.str) ? (free((x).v.str),(x).type = ERR_TYPE) : 0) +#define DestroyValue(x) (void) (((x).type == STR_TYPE && (x).v.str) ? (free((x).v.str),(x).v.str = NULL,(x).type = ERR_TYPE) : 0) EXTERN int DSEToday; EXTERN int RealToday; diff --git a/src/json.c b/src/json.c index cb2e6105..bb54a277 100644 --- a/src/json.c +++ b/src/json.c @@ -249,7 +249,7 @@ json_value * json_parse_ex (json_settings * settings, const json_char * end; json_value * top, * root, * alloc = 0; json_state state = { 0 }; - long flags = 0; + long flags; double num_digits = 0, num_e = 0; double num_fraction = 0; @@ -299,7 +299,7 @@ json_value * json_parse_ex (json_settings * settings, if (flags & flag_string) { if (!b) - { sprintf (error, "Unexpected EOF in string (at %d:%d)", line_and_col); + { sprintf (error, "Unexpected EOF in string (at %u:%u)", line_and_col); goto e_failed; } @@ -325,7 +325,7 @@ json_value * json_parse_ex (json_settings * settings, (uc_b3 = hex_value (*++ state.ptr)) == 0xFF || (uc_b4 = hex_value (*++ state.ptr)) == 0xFF) { - sprintf (error, "Invalid character value `%c` (at %d:%d)", b, line_and_col); + sprintf (error, "Invalid character value `%c` (at %u:%u)", b, line_and_col); goto e_failed; } @@ -342,7 +342,7 @@ json_value * json_parse_ex (json_settings * settings, (uc_b3 = hex_value (*++ state.ptr)) == 0xFF || (uc_b4 = hex_value (*++ state.ptr)) == 0xFF) { - sprintf (error, "Invalid character value `%c` (at %d:%d)", b, line_and_col); + sprintf (error, "Invalid character value `%c` (at %u:%u)", b, line_and_col); goto e_failed; } @@ -472,7 +472,7 @@ json_value * json_parse_ex (json_settings * settings, if (flags & flag_block_comment) { if (!b) - { sprintf (error, "%d:%d: Unexpected EOF in block comment", line_and_col); + { sprintf (error, "%u:%u: Unexpected EOF in block comment", line_and_col); goto e_failed; } @@ -488,12 +488,12 @@ json_value * json_parse_ex (json_settings * settings, else if (b == '/') { if (! (flags & (flag_seek_value | flag_done)) && top->type != json_object) - { sprintf (error, "%d:%d: Comment not allowed here", line_and_col); + { sprintf (error, "%u:%u: Comment not allowed here", line_and_col); goto e_failed; } if (++ state.ptr == end) - { sprintf (error, "%d:%d: EOF unexpected", line_and_col); + { sprintf (error, "%u:%u: EOF unexpected", line_and_col); goto e_failed; } @@ -508,7 +508,7 @@ json_value * json_parse_ex (json_settings * settings, continue; default: - sprintf (error, "%d:%d: Unexpected `%c` in comment opening sequence", line_and_col, b); + sprintf (error, "%u:%u: Unexpected `%c` in comment opening sequence", line_and_col, b); goto e_failed; }; } @@ -526,7 +526,7 @@ json_value * json_parse_ex (json_settings * settings, default: - sprintf (error, "%d:%d: Trailing garbage: `%c`", + sprintf (error, "%u:%u: Trailing garbage: `%c`", state.cur_line, state.cur_col, b); goto e_failed; @@ -545,7 +545,7 @@ json_value * json_parse_ex (json_settings * settings, if (top && top->type == json_array) flags = (flags & ~ (flag_need_comma | flag_seek_value)) | flag_next; else - { sprintf (error, "%d:%d: Unexpected ]", line_and_col); + { sprintf (error, "%u:%u: Unexpected ]", line_and_col); goto e_failed; } @@ -561,7 +561,7 @@ json_value * json_parse_ex (json_settings * settings, } else { - sprintf (error, "%d:%d: Expected , before %c", + sprintf (error, "%u:%u: Expected , before %c", state.cur_line, state.cur_col, b); goto e_failed; @@ -576,7 +576,7 @@ json_value * json_parse_ex (json_settings * settings, } else { - sprintf (error, "%d:%d: Expected : before %c", + sprintf (error, "%u:%u: Expected : before %c", state.cur_line, state.cur_col, b); goto e_failed; @@ -702,7 +702,7 @@ json_value * json_parse_ex (json_settings * settings, continue; } else - { sprintf (error, "%d:%d: Unexpected %c when seeking value", line_and_col, b); + { sprintf (error, "%u:%u: Unexpected %c when seeking value", line_and_col, b); goto e_failed; } }; @@ -722,7 +722,7 @@ json_value * json_parse_ex (json_settings * settings, case '"': if (flags & flag_need_comma) - { sprintf (error, "%d:%d: Expected , before \"", line_and_col); + { sprintf (error, "%u:%u: Expected , before \"", line_and_col); goto e_failed; } @@ -747,7 +747,7 @@ json_value * json_parse_ex (json_settings * settings, } /* FALLTHROUGH */ default: - sprintf (error, "%d:%d: Unexpected `%c` in object", line_and_col, b); + sprintf (error, "%u:%u: Unexpected `%c` in object", line_and_col, b); goto e_failed; }; @@ -765,7 +765,7 @@ json_value * json_parse_ex (json_settings * settings, if (! (flags & flag_num_e)) { if (flags & flag_num_zero) - { sprintf (error, "%d:%d: Unexpected `0` before `%c`", line_and_col, b); + { sprintf (error, "%u:%u: Unexpected `0` before `%c`", line_and_col, b); goto e_failed; } @@ -814,7 +814,7 @@ json_value * json_parse_ex (json_settings * settings, else if (b == '.' && top->type == json_integer) { if (!num_digits) - { sprintf (error, "%d:%d: Expected digit before `.`", line_and_col); + { sprintf (error, "%u:%u: Expected digit before `.`", line_and_col); goto e_failed; } @@ -831,7 +831,7 @@ json_value * json_parse_ex (json_settings * settings, if (top->type == json_double) { if (!num_digits) - { sprintf (error, "%d:%d: Expected digit after `.`", line_and_col); + { sprintf (error, "%u:%u: Expected digit after `.`", line_and_col); goto e_failed; } @@ -857,11 +857,11 @@ json_value * json_parse_ex (json_settings * settings, else { if (!num_digits) - { sprintf (error, "%d:%d: Expected digit after `e`", line_and_col); + { sprintf (error, "%u:%u: Expected digit after `e`", line_and_col); goto e_failed; } - top->u.dbl *= pow (10.0, (flags & flag_num_e_negative ? - num_e : num_e)); + top->u.dbl *= pow (10.0, ((flags & flag_num_e_negative) ? - num_e : num_e)); } if (flags & flag_num_negative) @@ -942,7 +942,7 @@ json_value * json_parse_ex (json_settings * settings, e_unknown_value: - sprintf (error, "%d:%d: Unknown value", line_and_col); + sprintf (error, "%u:%u: Unknown value", line_and_col); goto e_failed; e_alloc_failure: @@ -952,7 +952,7 @@ e_alloc_failure: e_overflow: - sprintf (error, "%d:%d: Too long (caught overflow)", line_and_col); + sprintf (error, "%u:%u: Too long (caught overflow)", line_and_col); goto e_failed; e_failed: diff --git a/src/main.c b/src/main.c index 7b3f05e1..bbf17dfa 100644 --- a/src/main.c +++ b/src/main.c @@ -1651,7 +1651,7 @@ System(char const *cmd) int r; r = system(cmd); if (r == 0) { - r = 1; + return; } } diff --git a/src/protos.h b/src/protos.h index 6d9b0393..a265cea2 100644 --- a/src/protos.h +++ b/src/protos.h @@ -98,9 +98,7 @@ void FindNumericToken (char const *s, Token *t); int ComputeTrigger (int today, Trigger *trig, TimeTrig *tim, int *err, int save_in_globals); int ComputeTriggerNoAdjustDuration (int today, Trigger *trig, TimeTrig *tim, int *err, int save_in_globals, int duration_days); int AdjustTriggerForDuration(int today, int r, Trigger *trig, TimeTrig *tim, int save_in_globals); -int ComputeScanStart(int today, Trigger *trig, TimeTrig *tt); char *StrnCpy (char *dest, char const *source, int n); -int StrMatch (char const *s1, char const *s2, int n); int StrinCmp (char const *s1, char const *s2, int n); char *StrDup (char const *s); int StrCmpi (char const *s1, char const *s2); @@ -177,7 +175,6 @@ char const *get_month_name(int mon); int push_call(char const *filename, char const *func, int lineno); void clear_callstack(void); -int have_callstack(void); int print_callstack(FILE *fp); void pop_call(void); #ifdef REM_USE_WCHAR diff --git a/src/rem2ps.c b/src/rem2ps.c index 4616f2d6..de97baff 100644 --- a/src/rem2ps.c +++ b/src/rem2ps.c @@ -390,7 +390,7 @@ void DoPsCal(void) month */ DBufInit(&buf); DBufGets(&buf, stdin); - sscanf(DBufValue(&buf), "%s %s %d %d %d", month, year, &days, &wkday, + sscanf(DBufValue(&buf), "%39s %39s %d %d %d", month, year, &days, &wkday, &MondayFirst); /* Replace underscores in month name with spaces */ @@ -422,9 +422,9 @@ void DoPsCal(void) } DBufGets(&buf, stdin); - sscanf(DBufValue(&buf), "%s %d", prevm, &prevdays); + sscanf(DBufValue(&buf), "%39s %d", prevm, &prevdays); DBufGets(&buf, stdin); - sscanf(DBufValue(&buf), "%s %d", nextm, &nextdays); + sscanf(DBufValue(&buf), "%39s %d", nextm, &nextdays); /* Replace underscores with spaces in names of next/prev month */ s = prevm; diff --git a/src/token.c b/src/token.c index 12f75481..1befc5fd 100644 --- a/src/token.c +++ b/src/token.c @@ -258,7 +258,6 @@ void FindNumericToken(char const *s, Token *t) /* If we hit a comma, swallow it. This allows stuff like Jan 6, 1998 */ if (*s == ',') { - s++; /* Classify the number we've got */ if (t->val >= BASE && t->val <= BASE+YR_RANGE) t->type = T_Year; else if (t->val >= 1 && t->val <= 31) t->type = T_Day; diff --git a/src/trigger.c b/src/trigger.c index 864feb4a..d564e9af 100644 --- a/src/trigger.c +++ b/src/trigger.c @@ -662,40 +662,3 @@ int ComputeTriggerNoAdjustDuration(int today, Trigger *trig, TimeTrig *tim, return -1; } -/***************************************************************/ -/* */ -/* ComputeScanStart */ -/* */ -/* Figure out where to start scan from by examining SCANFROM */ -/* and DURATION */ -/* */ -/***************************************************************/ -int -ComputeScanStart(int today, Trigger *trig, TimeTrig *tt) -{ - int minutes, days; - - /* If we don't have a time/duration, just use scanfrom */ - if (tt->ttime == NO_TIME || - tt->duration == NO_TIME) { - if (trig->scanfrom == NO_DATE) { - return today; - } - return trig->scanfrom; - } - - /* Calculate time-based SCANFROM */ - minutes = tt->ttime + tt->duration - 1; - - /* Figure out how many days to scan backwards from */ - days = minutes / MINUTES_PER_DAY; - - if (trig->scanfrom != NO_DATE) { - if (trig->scanfrom <= today - days) { - return trig->scanfrom; - } else { - return today - days; - } - } - return today - days; -} diff --git a/src/userfns.c b/src/userfns.c index 9e61d291..d6b97be8 100644 --- a/src/userfns.c +++ b/src/userfns.c @@ -126,6 +126,7 @@ int DoFset(ParsePtr p) } func->filename = StrDup(FileName); if (!func->filename) { + free(func); return E_NO_MEM; } func->lineno = LineNo; @@ -182,7 +183,7 @@ int DoFset(ParsePtr p) /* Allow an optional = sign: FSET f(x) = x*x */ c = ParseNonSpaceChar(p, &r, 1); if (c == '=') { - c = ParseNonSpaceChar(p, &r, 0); + (void) ParseNonSpaceChar(p, &r, 0); } /* Copy the text over */ if (p->isnested) { diff --git a/src/utils.c b/src/utils.c index 33df645b..75b26b07 100644 --- a/src/utils.c +++ b/src/utils.c @@ -41,22 +41,6 @@ char *StrnCpy(char *dest, char const *source, int n) return odest; } -/***************************************************************/ -/* */ -/* StrMatch */ -/* */ -/* Checks that two strings match (case-insensitive) to at */ -/* least the specified number of characters, or the length */ -/* of the first string, whichever is greater. */ -/* */ -/***************************************************************/ -int StrMatch(char const *s1, char const *s2, int n) -{ - int l; - if ((l = strlen(s1)) < n) return 0; - return !StrinCmp(s1, s2, l); -} - /***************************************************************/ /* */ /* StrinCmp - compare strings, case-insensitive */ @@ -223,13 +207,6 @@ clear_callstack(void) callstack = NULL; } -int -have_callstack(void) -{ - if (callstack) return 1; - return 0; -} - static void print_callstack_aux(FILE *fp, cs *entry) {