From 44e13e53bffc896840412dc5620548fac768b39d Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sun, 9 Feb 2020 18:27:19 -0500 Subject: [PATCH] Started restructuring common/errors.c to only produce a single error string (so we can pass it to Haiku's debugger() function without further allocations). In this case, the buffer size is 256 + the size of the prefix + the size of the suffix now. --- common/errors.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/common/errors.c b/common/errors.c index c7a8448d..94ce0c0b 100644 --- a/common/errors.c +++ b/common/errors.c @@ -2,22 +2,27 @@ #include "uipriv.h" #include "testhooks.h" +#define maxErrorBuf 256 +#define conststrlen(s) ((sizeof (s) / sizeof (char)) - 1) +#define errorBufSize(prefix, suffix) (conststrlen(prefix) + maxErrorBuf + conststrlen(suffix) + 1) + #define internalErrorPrefix "libui internal error" // TODO add debugging advice? #define internalErrorSuffix "This likely means there is a bug in libui itself. Contact the libui authors." +#define internalErrorBufSize errorBufSize(internalErrorPrefix, internalErrorSuffix) void uiprivInternalError(const char *fmt, ...) { va_list ap; - char buf[256]; + char buf[internalErrorBufSize]; int n; va_start(ap, fmt); - n = uiprivVsnprintf(buf, 256, fmt, ap); + n = uiprivVsnprintf(buf, internalErrorBufSize, fmt, ap); va_end(ap); if (n < 0) uiprivReportError(internalErrorPrefix, "internal error string has encoding error", internalErrorSuffix, true); - if (n >= 256) + if (n >= internalErrorBufSize) uiprivReportError(internalErrorPrefix, "internal error string too long", internalErrorSuffix, true); uiprivReportError(internalErrorPrefix, buf, internalErrorSuffix, true); } @@ -25,6 +30,7 @@ void uiprivInternalError(const char *fmt, ...) #define programmerErrorPrefix "libui programmer error" // TODO add debugging advice? #define programmerErrorSuffix "This likely means you are using libui incorrectly. Check your source code and try again. If you have received this warning in error, contact the libui authors." +#define programmerErrorBufSize errorBufSize(programmerErrorPrefix, programmerErrorSuffix) static uiprivTestHookReportProgrammerErrorFunc reportProgrammerErrorTestHook = NULL; static void *reportProgrammerErrorTestHookData = NULL; @@ -38,14 +44,14 @@ void uiprivTestHookReportProgrammerError(uiprivTestHookReportProgrammerErrorFunc void uiprivProgrammerError(const char *fmt, ...) { va_list ap; + char buf[programmerErrorBufSize]; int n; - char buf[256]; va_start(ap, fmt); - n = uiprivVsnprintf(buf, 256, fmt, ap); + n = uiprivVsnprintf(buf, programmerErrorBufSize, fmt, ap); if (n < 0) uiprivInternalError("programmer error has encoding error"); - if (n >= 256) + if (n >= programmerErrorBufSize) uiprivInternalError("programmer error string too long (%d)", n); va_end(ap); if (reportProgrammerErrorTestHook != NULL) {