MCPcopy Create free account
hub / github.com/NetHack/NetHack / nh_snprintf

Function nh_snprintf

src/hacklib.c:839–875  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

837}
838
839DISABLE_WARNING_FORMAT_NONLITERAL
840
841/*
842 * Wrap snprintf for use in the main code.
843 *
844 * Wrap reasons:
845 * 1. If there are any platform issues, we have one spot to fix them -
846 * snprintf is a routine with a troubling history of bad implementations.
847 * 2. Add cumbersome error checking in one spot. Problems with text
848 * wrangling do not have to be fatal.
849 * 3. Gcc 9+ will issue a warning unless the return value is used.
850 * Annoyingly, explicitly casting to void does not remove the error.
851 * So, use the result - see reason #2.
852 */
853void
854nh_snprintf(
855 const char *func UNUSED, int line UNUSED,
856 char *str, size_t size,
857 const char *fmt, ...)
858{
859 va_list ap;
860 int n;
861
862 va_start(ap, fmt);
863 n = vsnprintf(str, size, fmt, ap);
864 va_end(ap);
865 if (n < 0 || (size_t) n >= size) { /* is there a problem? */
866#if 0
867TODO: add set_impossible(), impossible -> func pointer,
868 test funcpointer before call
869 impossible("snprintf %s: func %s, file line %d",
870 (n < 0) ? "format error" : "overflow",
871 func, line);
872#endif
873 str[size - 1] = '\0'; /* make sure it is nul terminated */
874 }
875}
876
877RESTORE_WARNING_FORMAT_NONLITERAL
878

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected