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) {