]> granicus.if.org Git - nethack/commitdiff
Add and use Strlen(), like strlen() but panics on unreasonably long strings.
authornhkeni <keni@his.com>
Thu, 17 Mar 2022 01:42:00 +0000 (21:42 -0400)
committernhkeni <keni@his.com>
Thu, 17 Mar 2022 01:42:00 +0000 (21:42 -0400)
include/extern.h
include/global.h
src/botl.c
src/eat.c
src/hacklib.c
src/mkmaze.c
src/objnam.c
src/options.c
src/role.c
src/save.c
src/topten.c

index fa7cc5fab468b7e1006a3fb44bac8b4704a7a1a2..862f0315f730cfc90a0a7c8053bd0f0fffb45c2f 100644 (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);
index 0e31377d411bf3bde6f35ccf57305f928af21891..859aad1a43fbb310c7361f88608cb6098a936721 100644 (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
index 4e6af91503e3deb4b3e0fd56689d6a09750000d0..431d295cf814f33db8eb6703e540c7872459e38c 100644 (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;
index 1985ecd14dffd20d7f552ef7cae904efd8dcc48f..05370d67e28888e3b930f17780636caa577ab5ee 100644 (file)
--- 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);
index 33f01c904f1e8a14a41e247096113cf12ef21b6b..4ed7ab8efb14054c7c1f9b85184d079517be7e0e 100644 (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;
 }
 
index 91771ae1585d4265c4d371ce3120bce462cf9ed7..a0426fe5803f4b93da18c8a132beb019b34bde03 100644 (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)
index 8a63159c3a748e0b71aad5c76c54938f93cfce30..79535cbab29052dc245e52eda640a7a05302aa8f 100644 (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;
index ee14ea5fa1473cdc5e46f2471b63c6e73c0d42f0..0cb796b1f354e578fc53e1b8f8cae85924015af9 100644 (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 */
index cd3dedbe39eda00e408b537384dfaf128c2130d5..a5efc9155975c08759053d810cb7967199300017 100644 (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))
index 2c0a94acbae3c8452e9347a6bb67c73126132cdf..bbca8cf9fd6760a25ba851f73593756ab264101a 100644 (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);
index a5f50ae795abe24c0a66a2b30bd85876f6bf80d5..63d7ea2bd47b14c1dc8d584e2819d5e9e8173f56 100644 (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);