From 1814745646202b16a822da9125856d368f1e3f3b Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Fri, 31 May 2019 02:52:51 -0400 Subject: [PATCH] Wrote a wrapper for vsnprintf() that calls _vcsprintf()/vsnprintf_s() on Windows, because lol. I'm going to try switching from strcpy() to strncpy() for the other cases; let's see if this works. --- common/alloc.c | 1 + common/errors.c | 8 ++------ common/init.c | 6 +----- common/uipriv.h | 1 + sharedbits/strsafe_header.h | 7 +++++++ sharedbits/strsafe_impl.h | 26 ++++++++++++++++++++++++++ test/lib/testing.c | 4 ---- test/lib/testingpriv.c | 12 +++++++----- test/noinitwrongthread.c | 4 ---- 9 files changed, 45 insertions(+), 24 deletions(-) create mode 100644 sharedbits/strsafe_header.h create mode 100644 sharedbits/strsafe_impl.h diff --git a/common/alloc.c b/common/alloc.c index 051b78b8..e91959ee 100644 --- a/common/alloc.c +++ b/common/alloc.c @@ -7,4 +7,5 @@ #define sharedbitsPrefix uipriv #include "../sharedbits/alloc_impl.h" #include "../sharedbits/array_impl.h" +#include "../sharedbits/strsafe_impl.h" #undef sharedbitsPrefix diff --git a/common/errors.c b/common/errors.c index 216b3e60..c458037c 100644 --- a/common/errors.c +++ b/common/errors.c @@ -1,8 +1,4 @@ // 12 may 2019 -// TODO get rid of the need for this (it temporarily silences noise so I can find actual build issues) -#ifdef _MSC_VER -#define _CRT_SECURE_NO_WARNINGS -#endif #include #include #include "ui.h" @@ -20,7 +16,7 @@ void uiprivInternalError(const char *fmt, ...) int n; va_start(ap, fmt); - n = vsnprintf(buf, 256, fmt, ap); + n = uiprivVsnprintf(buf, 256, fmt, ap); va_end(ap); if (n < 0) uiprivReportError(internalErrorPrefix, "internal error string has encoding error", internalErrorSuffix, true); @@ -49,7 +45,7 @@ static void prepareProgrammerError(char *buf, int size, unsigned int which, va_l uiprivInternalError("bad programmer error value %u", which); if (messages[which] == NULL) uiprivInternalError("programmer error %u has no message", which); - n = vsnprintf(buf, size, messages[which], ap); + n = uiprivVsnprintf(buf, size, messages[which], ap); if (n < 0) uiprivInternalError("programmer error string for %u has encoding error", which); if (n >= size) diff --git a/common/init.c b/common/init.c index 689ebca4..ee61c500 100644 --- a/common/init.c +++ b/common/init.c @@ -1,8 +1,4 @@ // 19 april 2019 -// TODO get rid of the need for this (it temporarily silences noise so I can find actual build issues) -#ifdef _MSC_VER -#define _CRT_SECURE_NO_WARNINGS -#endif #include #include #include @@ -39,7 +35,7 @@ bool uiprivInitReturnErrorf(uiInitError *err, const char *msg, ...) va_list ap; va_start(ap, msg); - n = vsnprintf(err->Message, 256, msg, ap); + n = uiprivVsnprintf(err->Message, 256, msg, ap); va_end(ap); if (n < 0) uiprivInternalError("encoding error returning initialization error; this means something is very very wrong with libui itself"); diff --git a/common/uipriv.h b/common/uipriv.h index 335b7094..f395c5b7 100644 --- a/common/uipriv.h +++ b/common/uipriv.h @@ -35,6 +35,7 @@ extern bool uiprivSysCheckThread(void); #define uiprivArrayInit(arr, T, nGrow, what) uiprivArrayInitFull(&(arr), sizeof (T), nGrow, what) #define uiprivArrayFree(arr) uiprivArrayFreeFull(&(arr)) #define uiprivArrayAt(arr, T, n) (((T *) (arr.buf)) + (n)) +#include "../sharedbits/strsafe_header.h" #undef sharedbitsPrefix // errors.c diff --git a/sharedbits/strsafe_header.h b/sharedbits/strsafe_header.h new file mode 100644 index 00000000..cd1ba991 --- /dev/null +++ b/sharedbits/strsafe_header.h @@ -0,0 +1,7 @@ +// 31 may 2019 + +#include "start.h" + +extern int sharedbitsPrefixName(Vsnprintf)(char *s, size_t n, const char *fmt, va_list ap); + +#include "end.h" diff --git a/sharedbits/strsafe_impl.h b/sharedbits/strsafe_impl.h new file mode 100644 index 00000000..041410a1 --- /dev/null +++ b/sharedbits/strsafe_impl.h @@ -0,0 +1,26 @@ +// 31 may 2019 +// only requires strsafe_header.h if you don't define sharedbitsStatic as static + +#include "start.h" + +#ifdef sharedbitsStatic +sharedbitsStatic +#endif +int sharedbitsPrefixName(Vsnprintf)(char *s, size_t n, const char *fmt, va_list ap) +{ +#ifdef _WIN32 + int ret; + + if (s == NULL && n == 0) + return _vscprintf(fmt, ap); + // TODO figure out how to disambiguate between encoding errors (returns negative value; does not have documented errno values), other errors (returns negative value; errno == EINVAL), and truncations (returns -1; does not have documented errno values) + ret = vsnprintf_s(s, n, _TRUNCATE, fmt, ap); + if (ret == -1) + return n; + return ret; +#else + return vsnprintf(s, n, fmt, ap); +#endif +} + +#include "end.h" diff --git a/test/lib/testing.c b/test/lib/testing.c index 5ae98fcc..cb06084e 100644 --- a/test/lib/testing.c +++ b/test/lib/testing.c @@ -1,8 +1,4 @@ // 27 february 2018 -// TODO get rid of the need for this (it temporarily silences noise so I can find actual build issues) -#ifdef _MSC_VER -#define _CRT_SECURE_NO_WARNINGS -#endif #include #include #include diff --git a/test/lib/testingpriv.c b/test/lib/testingpriv.c index 6591201e..1eaddd69 100644 --- a/test/lib/testingpriv.c +++ b/test/lib/testingpriv.c @@ -1,8 +1,4 @@ // 19 may 2019 -// TODO get rid of the need for this (it temporarily silences noise so I can find actual build issues) -#ifdef _MSC_VER -#define _CRT_SECURE_NO_WARNINGS -#endif #include #include #include @@ -26,11 +22,17 @@ void testingprivInternalError(const char *fmt, ...) #include "../../sharedbits/array_impl.h" #undef sharedbitsPrefix +#define sharedbitsPrefix testingprivImpl +#define sharedbitsStatic static +#include "../../sharedbits/strsafe_impl.h" +#undef sharedbitsStatic +#undef sharedbitsPrefix + int testingprivVsnprintf(char *s, size_t n, const char *format, va_list ap) { int ret; - ret = vsnprintf(s, n, format, ap); + ret = testingprivImplVsnprintf(s, n, format, ap); if (ret < 0) testingprivInternalError("encoding error in vsnprintf(); this likely means your call to testingTLogf() and the like is invalid"); return ret; diff --git a/test/noinitwrongthread.c b/test/noinitwrongthread.c index 78992866..2378b1a3 100644 --- a/test/noinitwrongthread.c +++ b/test/noinitwrongthread.c @@ -1,8 +1,4 @@ // 28 may 2019 -// TODO get rid of the need for this (it temporarily silences noise so I can find actual build issues) -#ifdef _MSC_VER -#define _CRT_SECURE_NO_WARNINGS -#endif #include #include #include "lib/thread.h"