Add and use Strlen(), like strlen() but panics on unreasonably long strings.

This commit is contained in:
nhkeni
2022-03-16 21:42:00 -04:00
parent ff1289e828
commit 1151d54500
11 changed files with 26 additions and 24 deletions
+1 -1
View File
@@ -968,7 +968,7 @@ extern char *strip_newline(char *);
extern char *stripchars(char *, const char *, const char *); extern char *stripchars(char *, const char *, const char *);
extern char *stripdigits(char *); extern char *stripdigits(char *);
extern char *eos(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 boolean str_end_is(const char *, const char *);
extern int str_lines_maxlen(const char *); extern int str_lines_maxlen(const char *);
extern char *strkitten(char *, char); extern char *strkitten(char *, char);
+1
View File
@@ -262,6 +262,7 @@ typedef uchar nhsym;
#define Sprintf (void) sprintf #define Sprintf (void) sprintf
#define Strcat (void) strcat #define Strcat (void) strcat
#define Strcpy (void) strcpy #define Strcpy (void) strcpy
#define Strlen(s) Strlen_(s,__func__,__LINE__)
#ifdef NEED_VARARGS #ifdef NEED_VARARGS
#define Vprintf (void) vprintf #define Vprintf (void) vprintf
#define Vfprintf (void) vfprintf #define Vfprintf (void) vfprintf
+1 -1
View File
@@ -1048,7 +1048,7 @@ parse_cond_option(boolean negated, char *opts)
uniqpart = opts + (sizeof prefix - 1); uniqpart = opts + (sizeof prefix - 1);
for (i = 0; i < CONDITION_COUNT; ++i) { for (i = 0; i < CONDITION_COUNT; ++i) {
compareto = condtests[i].useroption; compareto = condtests[i].useroption;
sl = strlen(compareto); sl = Strlen(compareto);
if (match_optname(uniqpart, compareto, (sl >= 4) ? 4 : sl, FALSE)) { if (match_optname(uniqpart, compareto, (sl >= 4) ? 4 : sl, FALSE)) {
condopt(i, &condtests[i].choice, negated); condopt(i, &condtests[i].choice, negated);
return 0; return 0;
+1 -1
View File
@@ -176,7 +176,7 @@ eatmupdate(void)
if (altmsg) { if (altmsg) {
/* replace end-of-mimicking message */ /* replace end-of-mimicking message */
int amlen = Strlen(altmsg); unsigned amlen = Strlen(altmsg);
if (amlen > Strlen(g.eatmbuf)) { if (amlen > Strlen(g.eatmbuf)) {
free((genericptr_t) g.eatmbuf); free((genericptr_t) g.eatmbuf);
g.eatmbuf = (char *) alloc(amlen + 1); g.eatmbuf = (char *) alloc(amlen + 1);
+3 -3
View File
@@ -24,7 +24,7 @@
char * strip_newline (char *) char * strip_newline (char *)
char * stripchars (char *, const char *, const char *) char * stripchars (char *, const char *, const char *)
char * stripdigits (char *) char * stripdigits (char *)
unsigned Strlen (const char *str) unsigned Strlen_ (const char *str, const char *, int)
char * eos (char *) char * eos (char *)
boolean str_end_is (const char *, const char *) boolean str_end_is (const char *, const char *)
int str_lines_maxlen (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 */ /* like strlen(3) but returns unsigned and panics if string is unreasonably long */
unsigned unsigned
Strlen(const char *str){ Strlen_(const char *str, const char *file, int line){
size_t len = strnlen(str, LARGEST_INT); size_t len = strnlen(str, LARGEST_INT);
if (len == LARGEST_INT) if (len == LARGEST_INT)
panic("string too long"); panic("%s:%d string too long", file, line);
return (unsigned) len; return (unsigned) len;
} }
+1 -1
View File
@@ -706,7 +706,7 @@ migr_booty_item(int otyp, const char* gang)
otmp = mksobj_migr_to_species(otyp, (unsigned long) M2_ORC, TRUE, FALSE); otmp = mksobj_migr_to_species(otyp, (unsigned long) M2_ORC, TRUE, FALSE);
if (otmp && gang) { 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); Strcpy(ONAME(otmp), gang);
if (objects[otyp].oc_class == FOOD_CLASS) { if (objects[otyp].oc_class == FOOD_CLASS) {
if (otyp == SLIME_MOLD) if (otyp == SLIME_MOLD)
+1 -1
View File
@@ -1923,7 +1923,7 @@ the(const char* str)
if (tmp && (!named || tmp < named)) /* found an "of" */ if (tmp && (!named || tmp < named)) /* found an "of" */
insert_the = TRUE; insert_the = TRUE;
/* stupid special case: lacks "of" but needs "the" */ /* 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], && !strcmp(&str[l - 31],
"Platinum Yendorian Express Card")) "Platinum Yendorian Express Card"))
insert_the = TRUE; insert_the = TRUE;
+14 -13
View File
@@ -371,7 +371,7 @@ parseoptions(register char *opts, boolean tinitial, boolean tfrom_file)
got_match = FALSE; got_match = FALSE;
if (allopt[i].pfx) { if (allopt[i].pfx) {
if (!strncmpi(opts, allopt[i].name, strlen(allopt[i].name))) { if (streq(opts, allopt[i].name, TRUE)) {
matchidx = i; matchidx = i;
got_match = TRUE; got_match = TRUE;
} }
@@ -2717,13 +2717,13 @@ optfn_runmode(int optidx, int req, boolean negated, char *opts, char *op)
if (negated) { if (negated) {
flags.runmode = RUN_TPORT; flags.runmode = RUN_TPORT;
} else if (op != empty_optstr) { } else if (op != empty_optstr) {
if (!strncmpi(op, "teleport", strlen(op))) if (streq(op, "teleport", TRUE))
flags.runmode = RUN_TPORT; flags.runmode = RUN_TPORT;
else if (!strncmpi(op, "run", strlen(op))) else if (streq(op, "run", TRUE))
flags.runmode = RUN_LEAP; flags.runmode = RUN_LEAP;
else if (!strncmpi(op, "walk", strlen(op))) else if (streq(op, "walk", TRUE))
flags.runmode = RUN_STEP; flags.runmode = RUN_STEP;
else if (!strncmpi(op, "crawl", strlen(op))) else if (streq(op, "crawl", TRUE))
flags.runmode = RUN_CRAWL; flags.runmode = RUN_CRAWL;
else { else {
config_error_add("Unknown %s parameter '%s'", config_error_add("Unknown %s parameter '%s'",
@@ -5156,7 +5156,7 @@ handler_menu_colors(void)
(tmp->attr != ATR_NONE) ? "&" : "", (tmp->attr != ATR_NONE) ? "&" : "",
(tmp->attr != ATR_NONE) ? sattr : ""); (tmp->attr != ATR_NONE) ? sattr : "");
/* now main string */ /* now main string */
ln = sizeof buf - strlen(buf) - 1; /* length available */ ln = sizeof buf - Strlen(buf) - 1; /* length available */
Strcpy(mcbuf, "\""); Strcpy(mcbuf, "\"");
if (strlen(tmp->origstr) > ln) if (strlen(tmp->origstr) > ln)
Strcat(strncat(mcbuf, tmp->origstr, ln - 3), "..."); Strcat(strncat(mcbuf, tmp->origstr, ln - 3), "...");
@@ -5229,7 +5229,7 @@ handler_msgtype(void)
mtype = msgtype2name(tmp->msgtype); mtype = msgtype2name(tmp->msgtype);
any.a_int = ++mt_idx; any.a_int = ++mt_idx;
Sprintf(mtbuf, "%-5s \"", mtype); Sprintf(mtbuf, "%-5s \"", mtype);
ln = sizeof mtbuf - strlen(mtbuf) - sizeof "\""; ln = sizeof mtbuf - Strlen(mtbuf) - sizeof "\"";
if (strlen(tmp->pattern) > ln) if (strlen(tmp->pattern) > ln)
Strcat(strncat(mtbuf, tmp->pattern, ln - 3), "...\""); Strcat(strncat(mtbuf, tmp->pattern, ln - 3), "...\"");
else else
@@ -5334,7 +5334,7 @@ determine_ambiguities(void)
} }
} }
for (i = 0; i < SIZE(allopt) - 1; ++i) { 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 allopt[i].minmatch = (needed[i] < 3) ? 3
: (needed[i] <= len) ? needed[i] : len; : (needed[i] <= len) ? needed[i] : len;
} }
@@ -7345,8 +7345,9 @@ longest_option_name(int startpass, int endpass)
|| (is_wc2_option(name) && !wc2_supported(name))) || (is_wc2_option(name) && !wc2_supported(name)))
continue; continue;
if (strlen(name) > longest_name_len) unsigned len = Strlen(name);
longest_name_len = strlen(name); if (len > longest_name_len)
longest_name_len = len;
} }
return longest_name_len; return longest_name_len;
} }
@@ -8021,7 +8022,7 @@ match_sym(char *buf)
{ "S_explode8" , "S_expl_bc" }, { "S_explode8" , "S_expl_bc" },
{ "S_explode9" , "S_expl_br" }, { "S_explode9" , "S_expl_br" },
}; };
size_t len = strlen(buf); unsigned len = Strlen(buf);
const char *p = index(buf, ':'), *q = index(buf, '='); const char *p = index(buf, ':'), *q = index(buf, '=');
struct symparse *sp = loadsyms; struct symparse *sp = loadsyms;
@@ -8035,7 +8036,7 @@ match_sym(char *buf)
len = (int) (p - buf); len = (int) (p - buf);
} }
while (sp->range) { 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; return sp;
sp++; sp++;
} }
@@ -8232,7 +8233,7 @@ next_opt(winid datawin, const char *str)
Strcpy(s - 2, "."); /* replace last ", " */ Strcpy(s - 2, "."); /* replace last ", " */
i = COLNO; /* (greater than COLNO - 2) */ i = COLNO; /* (greater than COLNO - 2) */
} else { } else {
i = strlen(buf) + strlen(str) + 2; i = Strlen(buf) + Strlen(str) + 2;
} }
if (i > COLNO - 2) { /* rule of thumb */ if (i > COLNO - 2) { /* rule of thumb */
+1 -1
View File
@@ -814,7 +814,7 @@ str2race(const char *str)
return ROLE_NONE; return ROLE_NONE;
/* Match as much of str as is provided */ /* Match as much of str as is provided */
len = strlen(str); len = Strlen(str);
for (i = 0; races[i].noun; i++) { for (i = 0; races[i].noun; i++) {
/* Does it match the noun? */ /* Does it match the noun? */
if (!strncmpi(str, races[i].noun, len)) if (!strncmpi(str, races[i].noun, len))
+1 -1
View File
@@ -1008,7 +1008,7 @@ save_msghistory(NHFILE* nhfp)
/* ask window port for each message in sequence */ /* ask window port for each message in sequence */
while ((msg = getmsghistory(init)) != 0) { while ((msg = getmsghistory(init)) != 0) {
init = FALSE; init = FALSE;
msglen = strlen(msg); msglen = Strlen(msg);
if (msglen < 1) if (msglen < 1)
continue; continue;
/* sanity: truncate if necessary (shouldn't happen); /* sanity: truncate if necessary (shouldn't happen);
+1 -1
View File
@@ -1062,7 +1062,7 @@ outentry(int rank, struct toptenentry* t1, boolean so)
} else } else
topten_print(linebuf); topten_print(linebuf);
Snprintf(linebuf, sizeof(linebuf), "%15s %s", "", linebuf3); Snprintf(linebuf, sizeof(linebuf), "%15s %s", "", linebuf3);
lngr = strlen(linebuf); lngr = Strlen(linebuf);
} }
/* beginning of hp column not including padding */ /* beginning of hp column not including padding */
hppos = COLNO - 7 - (int) strlen(hpbuf); hppos = COLNO - 7 - (int) strlen(hpbuf);