From 9927e264b53f5b396bf5f4b72dcf96c9338dfb78 Mon Sep 17 00:00:00 2001 From: PatR Date: Fri, 23 Feb 2024 20:02:01 -0800 Subject: [PATCH] hacklib.c NONNULL functions A bunch of routines return a pointer which is never Null but weren't telling the compiler that such was the case. A couple (strsubst(), stripchars()) were accepting Null output argument and then returning Null, but callers had no reason to use them that way, so they've been changed. (upstart() could have been changed similarly; I've already forgotten why I left it as-is.) --- include/extern.h | 51 +++++++++++++++++++------------------ src/files.c | 2 +- src/hacklib.c | 66 ++++++++++++++++++++++++++---------------------- 3 files changed, 64 insertions(+), 55 deletions(-) diff --git a/include/extern.h b/include/extern.h index f03e0e0f5..ac5ec9dd2 100644 --- a/include/extern.h +++ b/include/extern.h @@ -1127,36 +1127,39 @@ extern boolean digit(char); extern boolean letter(char); extern char highc(char); extern char lowc(char); -extern char *lcase(char *) NONNULLARG1; -extern char *ucase(char *) NONNULLARG1; -extern char *upstart(char *); -extern char *upwords(char *) NONNULLARG1; -extern char *mungspaces(char *) NONNULLARG1; -extern char *trimspaces(char *) NONNULLARG1; -extern char *strip_newline(char *) NONNULLARG1; -extern char *stripchars(char *, const char *, const char *) NONNULLPTRS; -extern char *stripdigits(char *) NONNULLARG1; -extern char *eos(char *) NONNULLARG1; -extern const char *c_eos(const char *) NONNULLARG1; +extern char *lcase(char *) NONNULL NONNULLARG1; +extern char *ucase(char *) NONNULL NONNULLARG1; +extern char *upstart(char *); /* ought to be changed to NONNULL NONNULLARG1 + * and the code changed to not allow NULL arg */ +extern char *upwords(char *) NONNULL NONNULLARG1; +extern char *mungspaces(char *) NONNULL NONNULLARG1; +extern char *trimspaces(char *) NONNULL NONNULLARG1; +extern char *strip_newline(char *) NONNULL NONNULLARG1; +extern char *eos(char *) NONNULL NONNULLARG1; +extern const char *c_eos(const char *) NONNULL NONNULLARG1; extern unsigned Strlen_(const char *, const char *, int) NONNULLPTRS; extern boolean str_start_is(const char *, const char *, boolean) NONNULLPTRS; extern boolean str_end_is(const char *, const char *) NONNULLPTRS; extern int str_lines_maxlen(const char *); -extern char *strkitten(char *, char) NONNULLARG1; +extern char *strkitten(char *, char) NONNULL NONNULLARG1; extern void copynchars(char *, const char *, int) NONNULLARG12; extern char chrcasecpy(int, int); -extern char *strcasecpy(char *, const char *) NONNULLPTRS; -extern char *s_suffix(const char *) NONNULLARG1; -extern char *ing_suffix(const char *) NONNULLARG1; -extern char *xcrypt(const char *, char *) NONNULLPTRS; +extern char *strcasecpy(char *, const char *) NONNULL NONNULLPTRS; +extern char *s_suffix(const char *) NONNULL NONNULLARG1; +extern char *ing_suffix(const char *) NONNULL NONNULLARG1; +extern char *xcrypt(const char *, char *) NONNULL NONNULLPTRS; extern boolean onlyspace(const char *) NONNULLARG1; -extern char *tabexpand(char *) NONNULLARG1; -extern char *visctrl(char); -extern char *strsubst(char *, const char *, const char *); +extern char *tabexpand(char *) NONNULL NONNULLARG1; +extern char *visctrl(char) NONNULL; +extern char *stripchars(char *, const char *, + const char *) NONNULL NONNULLPTRS; +extern char *stripdigits(char *) NONNULL NONNULLARG1; +extern char *strsubst(char *, const char *, const char *) NONNULL NONNULLPTRS; extern int strNsubst(char *, const char *, const char *, int) NONNULLPTRS; -extern const char *findword(const char *, const char *, int, boolean); -extern const char *ordin(int); -extern char *sitoa(int); +extern const char *findword(const char *, const char *, int, + boolean) NONNULLARG2; +extern const char *ordin(int) NONNULL; +extern char *sitoa(int) NONNULL; extern int sgn(int); extern int rounddiv(long, int); extern int dist2(coordxy, coordxy, coordxy, coordxy); @@ -1181,11 +1184,11 @@ extern void reseed_random(int(*fn)(int)); extern time_t getnow(void); extern int getyear(void); #if 0 -extern char *yymmdd(time_t); +extern char *yymmdd(time_t) NONNULL; #endif extern long yyyymmdd(time_t); extern long hhmmss(time_t); -extern char *yyyymmddhhmmss(time_t); +extern char *yyyymmddhhmmss(time_t) NONNULL; extern time_t time_from_yyyymmddhhmmss(char *); extern int phase_of_the_moon(void); extern boolean friday_13th(void); diff --git a/src/files.c b/src/files.c index e64d9f3a5..9a8c35f2f 100644 --- a/src/files.c +++ b/src/files.c @@ -3460,7 +3460,7 @@ config_erradd(const char *buf) /* if buf[] doesn't end in a period, exclamation point, or question mark, we'll include a period (in the message, not appended to buf[]) */ - punct = eos((char *) buf) - 1; /* eos(buf)-1 is valid; cast away const */ + punct = c_eos((char *) buf) - 1; /* eos(buf)-1 is valid */ punct = strchr(".!?", *punct) ? "" : "."; if (!gp.program_state.config_error_ready) { diff --git a/src/hacklib.c b/src/hacklib.c index 055628fc1..f8a5a475d 100644 --- a/src/hacklib.c +++ b/src/hacklib.c @@ -224,6 +224,7 @@ eos(char *s) return s; } +/* version of eos() which takes a const* arg and returns that result */ const char * c_eos(const char *s) { @@ -519,22 +520,23 @@ visctrl(char c) /* caller is responsible for ensuring that bp is a valid pointer to a BUFSZ buffer */ char * -stripchars(char *bp, const char *stuff_to_strip, const char *orig) +stripchars( + char *bp, + const char *stuff_to_strip, + const char *orig) { int i = 0; char *s = bp; - if (s) { - while (*orig && i < (BUFSZ - 1)) { - if (!strchr(stuff_to_strip, *orig)) { - *s++ = *orig; - i++; - } - orig++; + while (*orig && i < (BUFSZ - 1)) { + if (!strchr(stuff_to_strip, *orig)) { + *s++ = *orig; + i++; } - *s = '\0'; - } else - impossible("no output buf in stripchars"); + orig++; + } + *s = '\0'; + return bp; } @@ -552,21 +554,22 @@ stripdigits(char *s) return s; } -/* substitute a word or phrase in a string (in place) */ -/* caller is responsible for ensuring that bp points to big enough buffer */ +/* substitute a word or phrase in a string (in place); + caller is responsible for ensuring that bp points to big enough buffer */ char * -strsubst(char *bp, const char *orig, const char *replacement) +strsubst( + char *bp, + const char *orig, + const char *replacement) { char *found, buf[BUFSZ]; + /* [this could be replaced by strNsubst(bp, orig, replacement, 1)] */ - if (bp) { - /* [this could be replaced by strNsubst(bp, orig, replacement, 1)] */ - found = strstr(bp, orig); - if (found) { - Strcpy(buf, found + strlen(orig)); - Strcpy(found, replacement); - Strcat(bp, buf); - } + found = strstr(bp, orig); + if (found) { + Strcpy(buf, found + strlen(orig)); + Strcpy(found, replacement); + Strcat(bp, buf); } return bp; } @@ -828,8 +831,9 @@ pmatchz(const char *patrn, const char *strng) /* case insensitive counted string comparison */ /*{ aka strncasecmp }*/ int -strncmpi(const char *s1, const char *s2, - int n) /*(should probably be size_t, which is unsigned)*/ +strncmpi( + const char *s1, const char *s2, + int n) /*(should probably be size_t, which is unsigned)*/ { char t1, t2; @@ -895,8 +899,10 @@ strstri(const char *str, const char *sub) /* compare two strings for equality, ignoring the presence of specified characters (typically whitespace) and possibly ignoring case */ boolean -fuzzymatch(const char *s1, const char *s2, const char *ignore_chars, - boolean caseblind) +fuzzymatch( + const char *s1, const char *s2, + const char *ignore_chars, + boolean caseblind) { char c1, c2; @@ -1158,13 +1164,13 @@ time_from_yyyymmddhhmmss(char *buf) t.tm_sec = atoi(s); timeresult = mktime(&t); } - if (timeresult == (time_t) -1) + if (timeresult == (time_t) -1) { debugpline1("time_from_yyyymmddhhmmss(%s) would have returned -1", buf ? buf : ""); - else - return timeresult; + timeresult = (time_t) 0; + } } - return (time_t) 0; + return timeresult; } /*