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

View File

@@ -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);

View File

@@ -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

View File

@@ -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;

View File

@@ -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);

View File

@@ -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;
}

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);
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)

View File

@@ -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;

View File

@@ -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 */

View File

@@ -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))

View File

@@ -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);

View File

@@ -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);