Removed uiInitError length checking and just truncated too-long messages with an ellipsis. Also removed the non-printf version of uiprivReportInitError.

This commit is contained in:
Pietro Gagliardi 2019-05-30 01:16:33 -04:00
parent 3049adbca0
commit ab5b3076ed
5 changed files with 22 additions and 95 deletions

View File

@ -1,8 +1,4 @@
// 19 april 2019 // 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 <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@ -11,31 +7,6 @@
static bool initialized = false; static bool initialized = false;
#define errAlreadyInitialized "libui already initialized"
#define errOptionsMustBeNULL "options parameter to uiInit() must be NULL"
static const char *commonInitErrors[] = {
errAlreadyInitialized,
errOptionsMustBeNULL,
NULL,
};
static bool checkInitErrorLengths(uiInitError *err, const char **initErrors)
{
const char **p;
if (initErrors == NULL)
return true;
for (p = initErrors; *p != NULL; p++)
if (strlen(*p) > 255) {
strcpy(err->Message, "[INTERNAL] uiInit() error too long: ");
strncat(err->Message, *p, 32);
strcat(err->Message, "...");
return false;
}
return true;
}
bool uiInit(void *options, uiInitError *err) bool uiInit(void *options, uiInitError *err)
{ {
if (err == NULL) if (err == NULL)
@ -43,16 +14,11 @@ bool uiInit(void *options, uiInitError *err)
if (err->Size != sizeof (uiInitError)) if (err->Size != sizeof (uiInitError))
return false; return false;
if (!checkInitErrorLengths(err, commonInitErrors))
return false;
if (!checkInitErrorLengths(err, uiprivSysInitErrors()))
return false;
if (initialized) if (initialized)
return uiprivInitReturnError(err, errAlreadyInitialized); return uiprivInitReturnErrorf(err, "libui already initialized");
if (options != NULL) if (options != NULL)
return uiprivInitReturnError(err, errOptionsMustBeNULL); return uiprivInitReturnErrorf(err, "options parameter to uiInit() must be NULL");
if (!uiprivSysInit(options, err)) if (!uiprivSysInit(options, err))
return false; return false;
@ -60,21 +26,24 @@ bool uiInit(void *options, uiInitError *err)
return true; return true;
} }
bool uiprivInitReturnError(uiInitError *err, const char *msg) // TODO rename all msgs to fmt
{
// checkInitErrorLengths() above ensures that err->Message[255] will always be '\0'
strncpy(err->Message, msg, 256);
return false;
}
bool uiprivInitReturnErrorf(uiInitError *err, const char *msg, ...) bool uiprivInitReturnErrorf(uiInitError *err, const char *msg, ...)
{ {
int n;
va_list ap; va_list ap;
// checkInitErrorLengths() above ensures that err->Message[255] will always be '\0' assuming the formatted string in msg passed to checkInitErrorLengths() is valid
va_start(ap, msg); va_start(ap, msg);
vsnprintf(err->Message, 256, msg, ap); n = vsnprintf(err->Message, 256, msg, ap);
va_end(ap); va_end(ap);
if (n < 0)
uiprivInternalError("encoding error returning initialization error; this means something is very very wrong with libui itself");
if (n >= 256) {
// the formatted message is too long; truncate it
err->Message[252] = '.';
err->Message[253] = '.';
err->Message[254] = '.';
err->Message[255] = '\0';
}
return false; return false;
} }

View File

@ -20,9 +20,7 @@ extern "C" {
#endif #endif
// init.c // init.c
extern const char **uiprivSysInitErrors(void);
extern bool uiprivSysInit(void *options, uiInitError *err); extern bool uiprivSysInit(void *options, uiInitError *err);
extern bool uiprivInitReturnError(uiInitError *err, const char *msg);
extern bool uiprivInitReturnErrorf(uiInitError *err, const char *msg, ...); extern bool uiprivInitReturnErrorf(uiInitError *err, const char *msg, ...);
extern bool uiprivCheckInitializedAndThreadImpl(const char *func); extern bool uiprivCheckInitializedAndThreadImpl(const char *func);
#define uiprivCheckInitializedAndThread() uiprivCheckInitializedAndThreadImpl(uiprivFunc) #define uiprivCheckInitializedAndThread() uiprivCheckInitializedAndThreadImpl(uiprivFunc)

View File

@ -36,18 +36,6 @@ static uiprivApplicationDelegate *uiprivAppDelegate;
@end @end
#define errNSAppAlreadyInitialized "NSApp is not of type uiprivApplication; was likely already initialized beforehand"
static const char *initErrors[] = {
errNSAppAlreadyInitialized,
NULL,
};
const char **uiprivSysInitErrors(void)
{
return initErrors;
}
static pthread_t mainThread; static pthread_t mainThread;
static BOOL initialized = NO; // TODO deduplicate this from common/init.c static BOOL initialized = NO; // TODO deduplicate this from common/init.c
@ -55,7 +43,7 @@ bool uiprivSysInit(void *options, uiInitError *err)
{ {
uiprivApp = [uiprivApplication sharedApplication]; uiprivApp = [uiprivApplication sharedApplication];
if (![NSApp isKindOfClass:[uiprivApplication class]]) if (![NSApp isKindOfClass:[uiprivApplication class]])
return uiprivInitReturnError(err, errNSAppAlreadyInitialized); return uiprivInitReturnErrorf(err, "NSApp is not of type uiprivApplication; was likely already initialized beforehand");
// don't check for a NO return; something (launch services?) causes running from application bundles to always return NO when asking to change activation policy, even if the change is to the same activation policy! // don't check for a NO return; something (launch services?) causes running from application bundles to always return NO when asking to change activation policy, even if the change is to the same activation policy!
// see https://github.com/andlabs/ui/issues/6 // see https://github.com/andlabs/ui/issues/6

View File

@ -1,11 +1,6 @@
// 6 april 2015 // 6 april 2015
#include "uipriv_unix.h" #include "uipriv_unix.h"
const char **uiprivSysInitErrors(void)
{
return NULL;
}
static pthread_t mainThread; static pthread_t mainThread;
static gboolean initialized = FALSE; // TODO deduplicate this from common/init.c static gboolean initialized = FALSE; // TODO deduplicate this from common/init.c
@ -14,8 +9,7 @@ bool uiprivSysInit(void *options, uiInitError *err)
GError *gerr = NULL; GError *gerr = NULL;
if (gtk_init_with_args(NULL, NULL, NULL, NULL, NULL, &gerr) == FALSE) { if (gtk_init_with_args(NULL, NULL, NULL, NULL, NULL, &gerr) == FALSE) {
// TODO make sure this is safe uiprivReturnErrorf(err, "%s", gerr->message);
strncpy(err->Message, gerr->message, 255);
g_error_free(gerr); g_error_free(gerr);
return false; return false;
} }

View File

@ -42,30 +42,8 @@ static inline void setHInstance(void)
ICC_DATE_CLASSES | /* date/time picker */ \ ICC_DATE_CLASSES | /* date/time picker */ \
0) 0)
#define errLoadDefaultIconFailed "failed to load default icon"
#define errLoadDefaultCursorFailed "failed to load default cursor"
#define errInitUtilWindowFailed "failed to initialize the utility window"
#define errICCFailed "InitCommonControlsEx() failed"
#define errICCFailedNoLastError "InitCommonControlsEx() failed, but didn't specify why. This usually means you forgot the Common Controls v6 manifest; refer to the libui documentation for instructions."
#define errCoInitializeFailed "CoInitialize() failed"
#define errHRESULTInitErrorsSuffix ": 0x00000000"
static const char *initErrors[] = {
errLoadDefaultIconFailed errHRESULTInitErrorsSuffix
errLoadDefaultCursorFailed errHRESULTInitErrorsSuffix
errInitUtilWindowFailed errHRESULTInitErrorsSuffix
errICCFailed errHRESULTInitErrorsSuffix,
errICCFailedNoLastError,
errCoInitializeFailed errHRESULTInitErrorsSuffix,
NULL,
};
#define uiprivInitReturnHRESULT(err, msg, hr) uiprivInitReturnErrorf(err, "%s: 0x%08I32X", msg, hr) #define uiprivInitReturnHRESULT(err, msg, hr) uiprivInitReturnErrorf(err, "%s: 0x%08I32X", msg, hr)
const char **uiprivSysInitErrors(void)
{
return initErrors;
}
static DWORD mainThread; static DWORD mainThread;
static BOOL initialized = FALSE; // TODO deduplicate this from common/init.c static BOOL initialized = FALSE; // TODO deduplicate this from common/init.c
@ -85,14 +63,14 @@ bool uiprivSysInit(void *options, uiInitError *err)
hr = uiprivHrLoadIconW(NULL, IDI_APPLICATION, &hDefaultIcon); hr = uiprivHrLoadIconW(NULL, IDI_APPLICATION, &hDefaultIcon);
if (hr != S_OK) if (hr != S_OK)
return uiprivInitReturnHRESULT(err, errLoadDefaultIconFailed, hr); return uiprivInitReturnHRESULT(err, "failed to load default icon", hr);
hr = uiprivHrLoadCursorW(NULL, IDC_ARROW, &hDefaultCursor); hr = uiprivHrLoadCursorW(NULL, IDC_ARROW, &hDefaultCursor);
if (hr != S_OK) if (hr != S_OK)
return uiprivInitReturnHRESULT(err, errLoadDefaultCursorFailed, hr); return uiprivInitReturnHRESULT(err, "failed to load default cursor", hr);
hr = uiprivInitUtilWindow(hDefaultIcon, hDefaultCursor); hr = uiprivInitUtilWindow(hDefaultIcon, hDefaultCursor);
if (hr != S_OK) if (hr != S_OK)
return uiprivInitReturnHRESULT(err, errInitUtilWindowFailed, hr); return uiprivInitReturnHRESULT(err, "failed to initialize the utility window", hr);
ZeroMemory(&icc, sizeof (INITCOMMONCONTROLSEX)); ZeroMemory(&icc, sizeof (INITCOMMONCONTROLSEX));
icc.dwSize = sizeof (INITCOMMONCONTROLSEX); icc.dwSize = sizeof (INITCOMMONCONTROLSEX);
@ -102,14 +80,14 @@ bool uiprivSysInit(void *options, uiInitError *err)
lasterr = GetLastError(); lasterr = GetLastError();
if (lasterr == 0) if (lasterr == 0)
return uiprivInitReturnError(err, errICCFailedNoLastError); return uiprivInitReturnErrorf(err, "InitCommonControlsEx() failed, but didn't specify why. This usually means you forgot the Common Controls v6 manifest; refer to the libui documentation for instructions.");
hr = HRESULT_FROM_WIN32(lasterr); hr = HRESULT_FROM_WIN32(lasterr);
return uiprivInitReturnHRESULT(err, errICCFailed, hr); return uiprivInitReturnHRESULT(err, "InitCommonControlsEx() failed", hr);
} }
/* hr = CoInitialize(NULL); /* hr = CoInitialize(NULL);
if (hr != S_OK && hr != S_FALSE) if (hr != S_OK && hr != S_FALSE)
return ieHRESULT("initializing COM", hr); return uiprivInitReturnHRESULT(err, "CoInitialize() failed", hr);
// LONGTERM initialize COM security // LONGTERM initialize COM security
// LONGTERM turn off COM exception handling // LONGTERM turn off COM exception handling
*/ */