buf sizes in version.c

there were some 'sizeof buf' on a passed pointer variable buf.
pass the actual buffer size in a second argument.
This commit is contained in:
nhmall
2022-05-12 19:38:50 -04:00
parent 0e83e15d6b
commit fe5cb1011f
3 changed files with 15 additions and 14 deletions
+2 -2
View File
@@ -3005,8 +3005,8 @@ extern void vault_gd_watching(unsigned int);
/* ### version.c ### */ /* ### version.c ### */
extern char *version_string(char *); extern char *version_string(char *, size_t bufsz);
extern char *getversionstring(char *); extern char *getversionstring(char *, size_t bufsz);
extern int doversion(void); extern int doversion(void);
extern int doextversion(void); extern int doextversion(void);
#ifdef MICRO #ifdef MICRO
+1 -1
View File
@@ -762,7 +762,7 @@ dump_everything(
it's conceivable that the game started with a different it's conceivable that the game started with a different
build date+time or even with an older nethack version, build date+time or even with an older nethack version,
but we only have access to the one it finished under */ 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, ""); putstr(0, 0, "");
/* game start and end date+time to disambiguate version date+time */ /* game start and end date+time to disambiguate version date+time */
+12 -11
View File
@@ -14,14 +14,15 @@ static void insert_rtoption(char *);
/* fill buffer with short version (so caller can avoid including date.h) */ /* fill buffer with short version (so caller can avoid including date.h) */
char * 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 */ /* fill and return the given buffer with the long nethack version string */
char * char *
getversionstring(char *buf) getversionstring(char *buf, size_t bufsz)
{ {
Strcpy(buf, nomakedefs.version_id); Strcpy(buf, nomakedefs.version_id);
@@ -39,25 +40,25 @@ getversionstring(char *buf)
#if defined(RUNTIME_PORT_ID) #if defined(RUNTIME_PORT_ID)
tmp = get_port_id(tmpbuf); tmp = get_port_id(tmpbuf);
if (tmp) if (tmp)
Snprintf(eos(buf), (sizeof buf - strlen(buf)) - 1, Snprintf(eos(buf), (bufsz - strlen(buf)) - 1,
"%s%s", c++ ? "," : "", tmp); "%s%s", c++ ? "," : "", tmp);
#endif #endif
if (nomakedefs.git_sha) 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); "%s%s", c++ ? "," : "", nomakedefs.git_sha);
#if (NH_DEVEL_STATUS != NH_STATUS_RELEASED) #if (NH_DEVEL_STATUS != NH_STATUS_RELEASED)
if (nomakedefs.git_branch) if (nomakedefs.git_branch)
Snprintf(eos(buf), (sizeof buf - strlen(buf)) - 1, Snprintf(eos(buf), (bufsz - strlen(buf)) - 1,
"%sbranch:%s", "%sbranch:%s",
c++ ? "," : "", nomakedefs.git_branch); c++ ? "," : "", nomakedefs.git_branch);
#endif #endif
if (c) if (c)
Snprintf(eos(buf), (sizeof buf - strlen(buf)) - 1, Snprintf(eos(buf), (bufsz - strlen(buf)) - 1,
"%s", ")"); "%s", ")");
else /* if nothing has been added, strip " (" back off */ else /* if nothing has been added, strip " (" back off */
*p = '\0'; *p = '\0';
if (dotoff) if (dotoff)
Snprintf(eos(buf), (sizeof buf - strlen(buf)) - 1, Snprintf(eos(buf), (bufsz - strlen(buf)) - 1,
"%s", "."); "%s", ".");
} }
return buf; return buf;
@@ -69,7 +70,7 @@ doversion(void)
{ {
char buf[BUFSZ]; char buf[BUFSZ];
pline("%s", getversionstring(buf)); pline("%s", getversionstring(buf, sizeof buf));
return ECMD_OK; return ECMD_OK;
} }
@@ -113,7 +114,7 @@ doextversion(void)
/* instead of using ``display_file(OPTIONS_USED,TRUE)'' we handle /* instead of using ``display_file(OPTIONS_USED,TRUE)'' we handle
the file manually so we can include dynamic version info */ 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 /* if extra text (git info) is present, put it on separate line
but don't wrap on (x86) */ but don't wrap on (x86) */
if (strlen(buf) >= COLNO) if (strlen(buf) >= COLNO)
@@ -209,7 +210,7 @@ early_version_info(boolean pastebuf)
Snprintf(buf1, sizeof(buf1), "test"); Snprintf(buf1, sizeof(buf1), "test");
/* this is early enough that we have to do our own line-splitting */ /* 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 */ tmp = strstri(buf1, " ("); /* split at start of version info */
if (tmp) { if (tmp) {
/* retain one buffer so that it all goes into the paste buffer */ /* retain one buffer so that it all goes into the paste buffer */