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.

This commit is contained in:
Pietro Gagliardi 2019-05-31 02:52:51 -04:00
parent 3ddaf3052e
commit 1814745646
9 changed files with 45 additions and 24 deletions

View File

@ -7,4 +7,5 @@
#define sharedbitsPrefix uipriv
#include "../sharedbits/alloc_impl.h"
#include "../sharedbits/array_impl.h"
#include "../sharedbits/strsafe_impl.h"
#undef sharedbitsPrefix

View File

@ -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 <stdarg.h>
#include <stdio.h>
#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)

View File

@ -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 <stdarg.h>
#include <stdio.h>
#include <string.h>
@ -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");

View File

@ -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

View File

@ -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"

26
sharedbits/strsafe_impl.h Normal file
View File

@ -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"

View File

@ -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 <ctype.h>
#include <stdio.h>
#include <stdlib.h>

View File

@ -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 <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -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;

View File

@ -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 <stdlib.h>
#include <string.h>
#include "lib/thread.h"