From: nhkeni Date: Thu, 17 Mar 2022 01:42:00 +0000 (-0400) Subject: Add and use Strlen(), like strlen() but panics on unreasonably long strings. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1151d54500113a174b6fd0a1fe5228f457143d66;p=nethack Add and use Strlen(), like strlen() but panics on unreasonably long strings. --- diff --git a/include/extern.h b/include/extern.h index fa7cc5fab..862f0315f 100644 --- a/include/extern.h +++ b/include/extern.h @@ -968,7 +968,7 @@ extern char *strip_newline(char *); extern char *stripchars(char *, const char *, const char *); extern char *stripdigits(char *); extern char *eos(char *); -extern unsigned Strlen(const char *); +extern unsigned Strlen_(const char *, const char *, int); extern boolean str_end_is(const char *, const char *); extern int str_lines_maxlen(const char *); extern char *strkitten(char *, char); diff --git a/include/global.h b/include/global.h index 0e31377d4..859aad1a4 100644 --- a/include/global.h +++ b/include/global.h @@ -262,6 +262,7 @@ typedef uchar nhsym; #define Sprintf (void) sprintf #define Strcat (void) strcat #define Strcpy (void) strcpy +#define Strlen(s) Strlen_(s,__func__,__LINE__) #ifdef NEED_VARARGS #define Vprintf (void) vprintf #define Vfprintf (void) vfprintf diff --git a/src/botl.c b/src/botl.c index 4e6af9150..431d295cf 100644 --- a/src/botl.c +++ b/src/botl.c @@ -1048,7 +1048,7 @@ parse_cond_option(boolean negated, char *opts) uniqpart = opts + (sizeof prefix - 1); for (i = 0; i < CONDITION_COUNT; ++i) { compareto = condtests[i].useroption; - sl = strlen(compareto); + sl = Strlen(compareto); if (match_optname(uniqpart, compareto, (sl >= 4) ? 4 : sl, FALSE)) { condopt(i, &condtests[i].choice, negated); return 0; diff --git a/src/eat.c b/src/eat.c index 1985ecd14..05370d67e 100644 --- a/src/eat.c +++ b/src/eat.c @@ -176,7 +176,7 @@ eatmupdate(void) if (altmsg) { /* replace end-of-mimicking message */ - int amlen = Strlen(altmsg); + unsigned amlen = Strlen(altmsg); if (amlen > Strlen(g.eatmbuf)) { free((genericptr_t) g.eatmbuf); g.eatmbuf = (char *) alloc(amlen + 1); diff --git a/src/hacklib.c b/src/hacklib.c index 33f01c904..4ed7ab8ef 100644 --- a/src/hacklib.c +++ b/src/hacklib.c @@ -24,7 +24,7 @@ char * strip_newline (char *) char * stripchars (char *, const char *, const char *) char * stripdigits (char *) - unsigned Strlen (const char *str) + unsigned Strlen_ (const char *str, const char *, int) char * eos (char *) boolean str_end_is (const char *, const char *) int str_lines_maxlen (const char *) @@ -231,11 +231,11 @@ eos(register char *s) /* like strlen(3) but returns unsigned and panics if string is unreasonably long */ unsigned -Strlen(const char *str){ +Strlen_(const char *str, const char *file, int line){ size_t len = strnlen(str, LARGEST_INT); if (len == LARGEST_INT) - panic("string too long"); + panic("%s:%d string too long", file, line); return (unsigned) len; } diff --git a/src/mkmaze.c b/src/mkmaze.c index 91771ae15..a0426fe58 100644 --- a/src/mkmaze.c +++ b/src/mkmaze.c @@ -706,7 +706,7 @@ migr_booty_item(int otyp, const char* gang) otmp = mksobj_migr_to_species(otyp, (unsigned long) M2_ORC, TRUE, FALSE); if (otmp && gang) { - new_oname(otmp, strlen(gang) + 1); /* removes old name if present */ + new_oname(otmp, Strlen(gang) + 1); /* removes old name if present */ Strcpy(ONAME(otmp), gang); if (objects[otyp].oc_class == FOOD_CLASS) { if (otyp == SLIME_MOLD) diff --git a/src/objnam.c b/src/objnam.c index 8a63159c3..79535cbab 100644 --- a/src/objnam.c +++ b/src/objnam.c @@ -1923,7 +1923,7 @@ the(const char* str) if (tmp && (!named || tmp < named)) /* found an "of" */ insert_the = TRUE; /* stupid special case: lacks "of" but needs "the" */ - else if (!named && (l = strlen(str)) >= 31 + else if (!named && (l = Strlen(str)) >= 31 && !strcmp(&str[l - 31], "Platinum Yendorian Express Card")) insert_the = TRUE; diff --git a/src/options.c b/src/options.c index ee14ea5fa..0cb796b1f 100644 --- a/src/options.c +++ b/src/options.c @@ -371,7 +371,7 @@ parseoptions(register char *opts, boolean tinitial, boolean tfrom_file) got_match = FALSE; if (allopt[i].pfx) { - if (!strncmpi(opts, allopt[i].name, strlen(allopt[i].name))) { + if (streq(opts, allopt[i].name, TRUE)) { matchidx = i; got_match = TRUE; } @@ -2717,13 +2717,13 @@ optfn_runmode(int optidx, int req, boolean negated, char *opts, char *op) if (negated) { flags.runmode = RUN_TPORT; } else if (op != empty_optstr) { - if (!strncmpi(op, "teleport", strlen(op))) + if (streq(op, "teleport", TRUE)) flags.runmode = RUN_TPORT; - else if (!strncmpi(op, "run", strlen(op))) + else if (streq(op, "run", TRUE)) flags.runmode = RUN_LEAP; - else if (!strncmpi(op, "walk", strlen(op))) + else if (streq(op, "walk", TRUE)) flags.runmode = RUN_STEP; - else if (!strncmpi(op, "crawl", strlen(op))) + else if (streq(op, "crawl", TRUE)) flags.runmode = RUN_CRAWL; else { config_error_add("Unknown %s parameter '%s'", @@ -5156,7 +5156,7 @@ handler_menu_colors(void) (tmp->attr != ATR_NONE) ? "&" : "", (tmp->attr != ATR_NONE) ? sattr : ""); /* now main string */ - ln = sizeof buf - strlen(buf) - 1; /* length available */ + ln = sizeof buf - Strlen(buf) - 1; /* length available */ Strcpy(mcbuf, "\""); if (strlen(tmp->origstr) > ln) Strcat(strncat(mcbuf, tmp->origstr, ln - 3), "..."); @@ -5229,7 +5229,7 @@ handler_msgtype(void) mtype = msgtype2name(tmp->msgtype); any.a_int = ++mt_idx; Sprintf(mtbuf, "%-5s \"", mtype); - ln = sizeof mtbuf - strlen(mtbuf) - sizeof "\""; + ln = sizeof mtbuf - Strlen(mtbuf) - sizeof "\""; if (strlen(tmp->pattern) > ln) Strcat(strncat(mtbuf, tmp->pattern, ln - 3), "...\""); else @@ -5334,7 +5334,7 @@ determine_ambiguities(void) } } for (i = 0; i < SIZE(allopt) - 1; ++i) { - len = strlen(allopt[i].name); + len = Strlen(allopt[i].name); allopt[i].minmatch = (needed[i] < 3) ? 3 : (needed[i] <= len) ? needed[i] : len; } @@ -7345,8 +7345,9 @@ longest_option_name(int startpass, int endpass) || (is_wc2_option(name) && !wc2_supported(name))) continue; - if (strlen(name) > longest_name_len) - longest_name_len = strlen(name); + unsigned len = Strlen(name); + if (len > longest_name_len) + longest_name_len = len; } return longest_name_len; } @@ -8021,7 +8022,7 @@ match_sym(char *buf) { "S_explode8" , "S_expl_bc" }, { "S_explode9" , "S_expl_br" }, }; - size_t len = strlen(buf); + unsigned len = Strlen(buf); const char *p = index(buf, ':'), *q = index(buf, '='); struct symparse *sp = loadsyms; @@ -8035,7 +8036,7 @@ match_sym(char *buf) len = (int) (p - buf); } while (sp->range) { - if ((len >= strlen(sp->name)) && !strncmpi(buf, sp->name, len)) + if ((len >= Strlen(sp->name)) && !strncmpi(buf, sp->name, len)) return sp; sp++; } @@ -8232,7 +8233,7 @@ next_opt(winid datawin, const char *str) Strcpy(s - 2, "."); /* replace last ", " */ i = COLNO; /* (greater than COLNO - 2) */ } else { - i = strlen(buf) + strlen(str) + 2; + i = Strlen(buf) + Strlen(str) + 2; } if (i > COLNO - 2) { /* rule of thumb */ diff --git a/src/role.c b/src/role.c index cd3dedbe3..a5efc9155 100644 --- a/src/role.c +++ b/src/role.c @@ -814,7 +814,7 @@ str2race(const char *str) return ROLE_NONE; /* Match as much of str as is provided */ - len = strlen(str); + len = Strlen(str); for (i = 0; races[i].noun; i++) { /* Does it match the noun? */ if (!strncmpi(str, races[i].noun, len)) diff --git a/src/save.c b/src/save.c index 2c0a94acb..bbca8cf9f 100644 --- a/src/save.c +++ b/src/save.c @@ -1008,7 +1008,7 @@ save_msghistory(NHFILE* nhfp) /* ask window port for each message in sequence */ while ((msg = getmsghistory(init)) != 0) { init = FALSE; - msglen = strlen(msg); + msglen = Strlen(msg); if (msglen < 1) continue; /* sanity: truncate if necessary (shouldn't happen); diff --git a/src/topten.c b/src/topten.c index a5f50ae79..63d7ea2bd 100644 --- a/src/topten.c +++ b/src/topten.c @@ -1062,7 +1062,7 @@ outentry(int rank, struct toptenentry* t1, boolean so) } else topten_print(linebuf); Snprintf(linebuf, sizeof(linebuf), "%15s %s", "", linebuf3); - lngr = strlen(linebuf); + lngr = Strlen(linebuf); } /* beginning of hp column not including padding */ hppos = COLNO - 7 - (int) strlen(hpbuf);