]> granicus.if.org Git - nethack/commitdiff
buf sizes in version.c
authornhmall <nhmall@nethack.org>
Thu, 12 May 2022 23:38:50 +0000 (19:38 -0400)
committernhmall <nhmall@nethack.org>
Thu, 12 May 2022 23:38:50 +0000 (19:38 -0400)
there were some 'sizeof buf' on a passed pointer variable buf.
pass the actual buffer size in a second argument.

include/extern.h
src/end.c
src/version.c

index 5eb65674f694a2c58a03c5b13f35b38991ad27d1..2481e762aba78617a2d0b0d6f8e604c91bc0f251 100644 (file)
@@ -3005,8 +3005,8 @@ extern void vault_gd_watching(unsigned int);
 
 /* ### version.c ### */
 
-extern char *version_string(char *);
-extern char *getversionstring(char *);
+extern char *version_string(char *, size_t bufsz);
+extern char *getversionstring(char *, size_t bufsz);
 extern int doversion(void);
 extern int doextversion(void);
 #ifdef MICRO
index 7cc61559fb1d752ba57da8966cfd3553c3de43d3..a79dfa674a64430d450d5f775bf594509be36afe 100644 (file)
--- a/src/end.c
+++ b/src/end.c
@@ -762,7 +762,7 @@ dump_everything(
        it's conceivable that the game started with a different
        build date+time or even with an older nethack version,
        but we only have access to the one it finished under */
-    putstr(0, 0, getversionstring(pbuf));
+    putstr(0, 0, getversionstring(pbuf, sizeof pbuf));
     putstr(0, 0, "");
 
     /* game start and end date+time to disambiguate version date+time */
index 1cbbaf4da83abf12fcf8a8a35a003f5f8f27d8b9..8958a3de4682d95b8c58e3dcc1789906e83e81e5 100644 (file)
@@ -14,14 +14,15 @@ static void insert_rtoption(char *);
 
 /* fill buffer with short version (so caller can avoid including date.h) */
 char *
-version_string(char *buf)
+version_string(char *buf, size_t bufsz)
 {
-    return strcpy(buf, nomakedefs.version_string);
+    Snprintf(buf, bufsz, "%s", nomakedefs.version_string);
+    return buf;
 }
 
 /* fill and return the given buffer with the long nethack version string */
 char *
-getversionstring(char *buf)
+getversionstring(char *buf, size_t bufsz)
 {
     Strcpy(buf, nomakedefs.version_id);
 
@@ -39,25 +40,25 @@ getversionstring(char *buf)
 #if defined(RUNTIME_PORT_ID)
         tmp = get_port_id(tmpbuf);
         if (tmp)
-            Snprintf(eos(buf), (sizeof buf - strlen(buf)) - 1,
+            Snprintf(eos(buf), (bufsz - strlen(buf)) - 1,
                      "%s%s", c++ ? "," : "", tmp);
 #endif
         if (nomakedefs.git_sha)
-            Snprintf(eos(buf), (sizeof buf - strlen(buf)) - 1,
+            Snprintf(eos(buf), (bufsz - strlen(buf)) - 1,
                      "%s%s", c++ ? "," : "", nomakedefs.git_sha);
 #if (NH_DEVEL_STATUS != NH_STATUS_RELEASED)
         if (nomakedefs.git_branch)
-            Snprintf(eos(buf), (sizeof buf - strlen(buf)) - 1,
+            Snprintf(eos(buf), (bufsz - strlen(buf)) - 1,
                      "%sbranch:%s",
                      c++ ? "," : "", nomakedefs.git_branch);
 #endif
         if (c)
-            Snprintf(eos(buf), (sizeof buf - strlen(buf)) - 1,
+            Snprintf(eos(buf), (bufsz - strlen(buf)) - 1,
                      "%s", ")");
         else /* if nothing has been added, strip " (" back off */
             *p = '\0';
         if (dotoff)
-            Snprintf(eos(buf), (sizeof buf - strlen(buf)) - 1,
+            Snprintf(eos(buf), (bufsz - strlen(buf)) - 1,
                      "%s", ".");
     }
     return buf;
@@ -69,7 +70,7 @@ doversion(void)
 {
     char buf[BUFSZ];
 
-    pline("%s", getversionstring(buf));
+    pline("%s", getversionstring(buf, sizeof buf));
     return ECMD_OK;
 }
 
@@ -113,7 +114,7 @@ doextversion(void)
     /* instead of using ``display_file(OPTIONS_USED,TRUE)'' we handle
        the file manually so we can include dynamic version info */
 
-    (void) getversionstring(buf);
+    (void) getversionstring(buf, sizeof buf);
     /* if extra text (git info) is present, put it on separate line
        but don't wrap on (x86) */
     if (strlen(buf) >= COLNO)
@@ -209,7 +210,7 @@ early_version_info(boolean pastebuf)
 
     Snprintf(buf1, sizeof(buf1), "test");
     /* this is early enough that we have to do our own line-splitting */
-    getversionstring(buf1);
+    getversionstring(buf1, sizeof buf1);
     tmp = strstri(buf1, " ("); /* split at start of version info */
     if (tmp) {
         /* retain one buffer so that it all goes into the paste buffer */