Create and use a snprintf wrapper in the core code

Use a wrapper around snprintf to consilidate all use, add
error checking, and remove gcc 9 warnings about not checking
the result.

Replace the prevous use of snprintf added to weapon.c with the
new scheme.

Update a second spot that has a gcc sprintf warning.  While
there, simplify the code.
This commit is contained in:
Dean Luick
2021-01-15 11:30:02 -06:00
parent 35f9115fae
commit 8143d55d76
4 changed files with 67 additions and 22 deletions

View File

@@ -74,6 +74,8 @@
char * nonconst (const char *, char *)
int swapbits (int, int, int)
void shuffle_int_array (int *, int)
void nh_snprintf (const char *, int, char *, size_t,
const char *, ...)
=*/
#ifdef LINT
#define Static /* pacify lint */
@@ -1319,4 +1321,39 @@ int count;
}
}
/*
* Wrap snprintf for use in the main code.
*
* Wrap reasons:
* 1. If there are any platform issues, we have one spot to fix them -
* snprintf is a routine with a troubling history of bad implementations.
* 2. Add combersome error checking in one spot. Problems with text wrangling
* do not have to be fatal.
* 3. Gcc 9+ will issue a warning unless the return value is used.
* Annoyingly, explicitly casting to void does not remove the error.
* So, use the result - see reason #2.
*/
void
nh_snprintf(const char *func, int line, char *str, size_t size,
const char *fmt, ...)
{
va_list ap;
int n;
va_start(ap, fmt);
#ifdef NO_VSNPRINTF
n = vsprintf(str, fmt, ap);
#else
n = vsnprintf(str, size, fmt, ap);
#endif
va_end(ap);
if (n < 0 || (size_t)n >= size) { /* is there a problem? */
impossible("snprintf %s: func %s, file line %d",
n < 0 ? "format error"
: "overflow",
func, line);
str[size-1] = 0; /* make sure it is nul terminated */
}
}
/*hacklib.c*/