From 995716ff7e7a7d20c3fa32088248073087aa5c48 Mon Sep 17 00:00:00 2001 From: BrettRToomey Date: Thu, 2 Jul 2015 14:13:57 -0700 Subject: [PATCH 1/2] Started adding memory allocation checks --- src/cocoa_monitor.m | 24 ++++++++++++++++++++++++ src/cocoa_window.m | 5 +++++ src/egl_context.c | 16 ++++++++++++++++ src/glx_context.c | 7 +++++++ src/input.c | 13 +++++++++++++ src/iokit_joystick.m | 23 +++++++++++++++++++++++ src/linux_joystick.c | 17 ++++++++++++++++- src/mir_init.c | 7 +++++++ src/mir_monitor.c | 8 ++++++++ src/mir_window.c | 17 ++++++++++++++++- src/monitor.c | 16 ++++++++++++++++ src/wgl_context.c | 7 +++++++ src/win32_init.c | 12 ++++++++++++ src/win32_window.c | 14 ++++++++++++++ src/window.c | 7 +++++++ src/wl_init.c | 6 ++++++ src/wl_monitor.c | 13 +++++++++++++ src/x11_monitor.c | 16 ++++++++++++++++ src/x11_window.c | 17 ++++++++++++++++- 19 files changed, 242 insertions(+), 3 deletions(-) diff --git a/src/cocoa_monitor.m b/src/cocoa_monitor.m index a99aa33f4..585ee4cfa 100644 --- a/src/cocoa_monitor.m +++ b/src/cocoa_monitor.m @@ -67,6 +67,15 @@ static char* getDisplayName(CGDirectDisplayID displayID) size = CFStringGetMaximumSizeForEncoding(CFStringGetLength(value), kCFStringEncodingUTF8); name = calloc(size + 1, sizeof(char)); + if (name == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "Cocoa: Failed to allocate name"); + + CFRelease(info); + return strdup("Unknown"); + } + CFStringGetCString(value, name, size, kCFStringEncodingUTF8); CFRelease(info); @@ -253,7 +262,22 @@ _GLFWmonitor** _glfwPlatformGetMonitors(int* count) CGGetOnlineDisplayList(0, NULL, &displayCount); displays = calloc(displayCount, sizeof(CGDirectDisplayID)); + if (displays == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "Failed to allocate displays"); + return NULL; + } + monitors = calloc(displayCount, sizeof(_GLFWmonitor*)); + if (monitors == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "Failed to allocate monitors"); + + free(displays); + return NULL; + } CGGetOnlineDisplayList(displayCount, displays, &displayCount); NSArray* screens = [NSScreen screens]; diff --git a/src/cocoa_window.m b/src/cocoa_window.m index b03632c09..b86d1928f 100644 --- a/src/cocoa_window.m +++ b/src/cocoa_window.m @@ -613,6 +613,11 @@ static int translateKey(unsigned int key) { NSEnumerator* e = [files objectEnumerator]; char** paths = calloc(count, sizeof(char*)); + if (paths == NULL) + { + return NO; + } + int i; for (i = 0; i < count; i++) diff --git a/src/egl_context.c b/src/egl_context.c index d62262381..48d607a9f 100644 --- a/src/egl_context.c +++ b/src/egl_context.c @@ -101,9 +101,25 @@ static GLboolean chooseFBConfigs(const _GLFWctxconfig* ctxconfig, } nativeConfigs = calloc(nativeCount, sizeof(EGLConfig)); + if (nativeConfigs == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "EGL: Failed to allocate native configs"); + return GL_FALSE; + } + eglGetConfigs(_glfw.egl.display, nativeConfigs, nativeCount, &nativeCount); usableConfigs = calloc(nativeCount, sizeof(_GLFWfbconfig)); + if (usableConfigs == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "EGL: Failed to allocate usable configs"); + + free(nativeConfigs); + return GL_FALSE; + } + usableCount = 0; for (i = 0; i < nativeCount; i++) diff --git a/src/glx_context.c b/src/glx_context.c index 1101912f9..6e0a7e79a 100644 --- a/src/glx_context.c +++ b/src/glx_context.c @@ -72,6 +72,13 @@ static GLboolean chooseFBConfig(const _GLFWfbconfig* desired, GLXFBConfig* resul } usableConfigs = calloc(nativeCount, sizeof(_GLFWfbconfig)); + if (usableConfigs == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "GLX: Failed to allocate usable configs"); + return GL_FALSE; + } + usableCount = 0; for (i = 0; i < nativeCount; i++) diff --git a/src/input.c b/src/input.c index eefbf0860..ee43c3cdd 100644 --- a/src/input.c +++ b/src/input.c @@ -366,6 +366,13 @@ GLFWAPI GLFWcursor* glfwCreateCursor(const GLFWimage* image, int xhot, int yhot) _GLFW_REQUIRE_INIT_OR_RETURN(NULL); cursor = calloc(1, sizeof(_GLFWcursor)); + if (cursor == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "Failed to allocate cursor"); + return NULL; + } + cursor->next = _glfw.cursorListHead; _glfw.cursorListHead = cursor; @@ -396,6 +403,12 @@ GLFWAPI GLFWcursor* glfwCreateStandardCursor(int shape) } cursor = calloc(1, sizeof(_GLFWcursor)); + if (cursor == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "Failed to allocate cursor"); + return NULL; + } cursor->next = _glfw.cursorListHead; _glfw.cursorListHead = cursor; diff --git a/src/iokit_joystick.m b/src/iokit_joystick.m index ebce5c8ff..cc7d7b4da 100644 --- a/src/iokit_joystick.m +++ b/src/iokit_joystick.m @@ -110,6 +110,12 @@ static void addJoystickElement(_GLFWjoydevice* joystick, if (elementsArray) { _GLFWjoyelement* element = calloc(1, sizeof(_GLFWjoyelement)); + if (element == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "IOKit: Failed to allocate element"); + return; + } CFArrayAppendValue(elementsArray, element); @@ -313,8 +319,25 @@ static void matchCallback(void* context, joystick->axes = calloc(CFArrayGetCount(joystick->axisElements), sizeof(float)); + if (joystick->axes == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "IOKit: Failed to allocate axes"); + + joystick->present = GL_FALSE; + return; + } + joystick->buttons = calloc(CFArrayGetCount(joystick->buttonElements) + CFArrayGetCount(joystick->hatElements) * 4, 1); + if (joystick->buttons == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "IOKit: Failed to allocate buttons"); + + joystick->present = GL_FALSE; + return; + } } // Callback for user-initiated joystick removal diff --git a/src/linux_joystick.c b/src/linux_joystick.c index 5a9d8e2a2..533e02157 100644 --- a/src/linux_joystick.c +++ b/src/linux_joystick.c @@ -98,8 +98,23 @@ static void openJoystickDevice(const char* path) _glfw.linux_js.js[joy].buttonCount = (int) buttonCount; _glfw.linux_js.js[joy].axes = calloc(axisCount, sizeof(float)); - _glfw.linux_js.js[joy].buttons = calloc(buttonCount, 1); + if (_glfw.linux_js.js[joy].axes == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "Linux: Failed to allocate joystix axes"); + close(fd); + return; + } + _glfw.linux_js.js[joy].buttons = calloc(buttonCount, 1); + if (_glfw.linux_js.js[joy].buttons == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "Linux: Failed to allocate joystick buttons"); + close(fd); + return; + } + _glfw.linux_js.js[joy].present = GL_TRUE; #endif // __linux__ } diff --git a/src/mir_init.c b/src/mir_init.c index 153e40056..3c6b13ba5 100644 --- a/src/mir_init.c +++ b/src/mir_init.c @@ -59,6 +59,13 @@ int _glfwPlatformInit(void) _glfwInitJoysticks(); _glfw.mir.event_queue = calloc(1, sizeof(EventQueue)); + if (_glfw.mir.event_queue == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "Mir: Failed to allocate event queue"); + return GL_FALSE; + } + _glfwInitEventQueue(_glfw.mir.event_queue); error = pthread_mutex_init(&_glfw.mir.event_mutex, NULL); diff --git a/src/mir_monitor.c b/src/mir_monitor.c index 93073d334..dd994e23b 100644 --- a/src/mir_monitor.c +++ b/src/mir_monitor.c @@ -100,6 +100,14 @@ GLFWvidmode* _glfwPlatformGetVideoModes(_GLFWmonitor* monitor, int* found) continue; modes = calloc(out->num_modes, sizeof(GLFWvidmode)); + if (modes == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "Mir: Failed to allocate modes"); + + mir_display_config_destroy(displayConfig); + return NULL; + } for (*found = 0; *found < out->num_modes; (*found)++) { diff --git a/src/mir_window.c b/src/mir_window.c index cbc8e0597..56398b0e8 100644 --- a/src/mir_window.c +++ b/src/mir_window.c @@ -52,8 +52,23 @@ static int emptyEventQueue(EventQueue* queue) static EventNode* newEventNode(MirEvent const* event, _GLFWwindow* context) { EventNode* new_node = calloc(1, sizeof(EventNode)); + if (new_node == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "Mir: Failed to allocate new node"); + return NULL; + } + new_node->event = calloc(1, sizeof(MirEvent)); - new_node->window = context; + if (new_node->event == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "Mir: failed to allocate new node event"); + free(new_node); + return NULL; + } + + new_node->window = context; memcpy(new_node->event, event, sizeof(MirEvent)); return new_node; diff --git a/src/monitor.c b/src/monitor.c index 9a155bd73..e1703daca 100644 --- a/src/monitor.c +++ b/src/monitor.c @@ -166,6 +166,13 @@ void _glfwInputMonitorChange(void) _GLFWmonitor* _glfwAllocMonitor(const char* name, int widthMM, int heightMM) { _GLFWmonitor* monitor = calloc(1, sizeof(_GLFWmonitor)); + if (monitor == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "Failed to allocate monitor"); + return NULL; + } + monitor->name = strdup(name); monitor->widthMM = widthMM; monitor->heightMM = heightMM; @@ -191,6 +198,15 @@ void _glfwAllocGammaArrays(GLFWgammaramp* ramp, unsigned int size) ramp->red = calloc(size, sizeof(unsigned short)); ramp->green = calloc(size, sizeof(unsigned short)); ramp->blue = calloc(size, sizeof(unsigned short)); + + if (ramp->red == NULL || + ramp->green == NULL || + ramp->blue == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "Failed to allocate gamma arrays"); + } + ramp->size = size; } diff --git a/src/wgl_context.c b/src/wgl_context.c index 96ebcfb11..2d3beff8a 100644 --- a/src/wgl_context.c +++ b/src/wgl_context.c @@ -123,6 +123,13 @@ static GLboolean choosePixelFormat(_GLFWwindow* window, } usableConfigs = calloc(nativeCount, sizeof(_GLFWfbconfig)); + if (usableConfigs == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "WGL: Failed to allocate usable configs"); + return GL_FALSE; + } + usableCount = 0; for (i = 0; i < nativeCount; i++) diff --git a/src/win32_init.c b/src/win32_init.c index c39c67c16..45ad32418 100644 --- a/src/win32_init.c +++ b/src/win32_init.c @@ -284,6 +284,12 @@ WCHAR* _glfwCreateWideStringFromUTF8(const char* source) return NULL; target = calloc(length, sizeof(WCHAR)); + if (target == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "Win32 Init: Failed to allocate target"); + return NULL; + } if (!MultiByteToWideChar(CP_UTF8, 0, source, -1, target, length)) { @@ -306,6 +312,12 @@ char* _glfwCreateUTF8FromWideString(const WCHAR* source) return NULL; target = calloc(length, sizeof(char)); + if (target == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "Win32 Init: Failed to allocate target"); + return NULL; + } if (!WideCharToMultiByte(CP_UTF8, 0, source, -1, target, length, NULL, NULL)) { diff --git a/src/win32_window.c b/src/win32_window.c index daad58bf1..881835e02 100644 --- a/src/win32_window.c +++ b/src/win32_window.c @@ -602,6 +602,13 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, const int count = DragQueryFileW(hDrop, 0xffffffff, NULL, 0); char** paths = calloc(count, sizeof(char*)); + if (paths == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "Win32: Failed to allocate paths"); + + return 0; + } // Move the mouse to the position of the drop DragQueryPoint(hDrop, &pt); @@ -611,6 +618,13 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, { const UINT length = DragQueryFileW(hDrop, i, NULL, 0); WCHAR* buffer = calloc(length + 1, sizeof(WCHAR)); + if (buffer == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "Win32: Failed to allocate buffer"); + free(paths); + return 0; + } DragQueryFileW(hDrop, i, buffer, length + 1); paths[i] = _glfwCreateUTF8FromWideString(buffer); diff --git a/src/window.c b/src/window.c index 47964dfdc..690967daf 100644 --- a/src/window.c +++ b/src/window.c @@ -154,6 +154,13 @@ GLFWAPI GLFWwindow* glfwCreateWindow(int width, int height, return NULL; window = calloc(1, sizeof(_GLFWwindow)); + if (window == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "Failed to allocate window"); + return NULL; + } + window->next = _glfw.windowListHead; _glfw.windowListHead = window; diff --git a/src/wl_init.c b/src/wl_init.c index ea621f998..81debdeb7 100644 --- a/src/wl_init.c +++ b/src/wl_init.c @@ -555,6 +555,12 @@ int _glfwPlatformInit(void) wl_registry_add_listener(_glfw.wl.registry, ®istryListener, NULL); _glfw.wl.monitors = calloc(4, sizeof(_GLFWmonitor*)); + if (_glfw.wl.monitors == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "WL: Failed to allocate monitors"); + return GL_FALSE; + } _glfw.wl.monitorsSize = 4; _glfw.wl.xkb.context = xkb_context_new(0); diff --git a/src/wl_monitor.c b/src/wl_monitor.c index fbcefd38d..8cb07f03e 100644 --- a/src/wl_monitor.c +++ b/src/wl_monitor.c @@ -140,6 +140,13 @@ void _glfwAddOutput(uint32_t name, uint32_t version) } monitor->wl.modes = calloc(4, sizeof(_GLFWvidmodeWayland)); + if (monitor->wl.modes == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "Wayland: Failed to allocate modes"); + return; + } + monitor->wl.modesSize = 4; monitor->wl.output = output; @@ -151,6 +158,12 @@ void _glfwAddOutput(uint32_t name, uint32_t version) int size = _glfw.wl.monitorsSize * 2; monitors = realloc(monitors, size * sizeof(_GLFWmonitor*)); + if (monitors == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "Wayland: Failed to allocate monitors"); + return; + } _glfw.wl.monitors = monitors; _glfw.wl.monitorsSize = size; diff --git a/src/x11_monitor.c b/src/x11_monitor.c index 69d872531..fc699bbfc 100644 --- a/src/x11_monitor.c +++ b/src/x11_monitor.c @@ -215,6 +215,14 @@ _GLFWmonitor** _glfwPlatformGetMonitors(int* count) _glfw.x11.root); monitors = calloc(sr->noutput, sizeof(_GLFWmonitor*)); + if (monitors == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "X11: Failed to allocate monitors"); + + *count = 0; + return NULL; + } if (_glfw.x11.xinerama.available) screens = XineramaQueryScreens(_glfw.x11.display, &screenCount); @@ -294,6 +302,14 @@ _GLFWmonitor** _glfwPlatformGetMonitors(int* count) if (!monitors) { monitors = calloc(1, sizeof(_GLFWmonitor*)); + if (monitors == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "X11: Failed to allocate monitors"); + *count = 0; + return NULL; + } + monitors[0] = _glfwAllocMonitor("Display", DisplayWidthMM(_glfw.x11.display, _glfw.x11.screen), diff --git a/src/x11_window.c b/src/x11_window.c index ae27eed9a..6017c86fb 100644 --- a/src/x11_window.c +++ b/src/x11_window.c @@ -224,8 +224,23 @@ static char** parseUriList(char* text, int* count) (*count)++; char* path = calloc(strlen(line) + 1, 1); + if (path == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "X11: Failed to allocate path"); + return NULL; + } + paths = realloc(paths, *count * sizeof(char*)); - paths[*count - 1] = path; + if (paths == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "X11: Failed to allocate paths"); + free(path); + return NULL; + } + + paths[*count - 1] = path; while (*line) { From 0ab09d769ed467db6d0bde27df5090e13581d360 Mon Sep 17 00:00:00 2001 From: BrettRToomey Date: Sat, 4 Jul 2015 13:30:44 -0700 Subject: [PATCH 2/2] Fixed formatting errors --- src/cocoa_monitor.m | 40 ++++++++++++++++++++-------------------- src/cocoa_window.m | 8 ++++---- src/egl_context.c | 28 ++++++++++++++-------------- src/glx_context.c | 12 ++++++------ src/input.c | 24 ++++++++++++------------ src/iokit_joystick.m | 42 +++++++++++++++++++++--------------------- src/linux_joystick.c | 30 +++++++++++++++--------------- src/mir_init.c | 12 ++++++------ src/mir_monitor.c | 16 ++++++++-------- src/mir_window.c | 26 +++++++++++++------------- src/monitor.c | 26 +++++++++++++------------- src/wgl_context.c | 12 ++++++------ src/win32_init.c | 24 ++++++++++++------------ src/win32_window.c | 26 +++++++++++++------------- src/window.c | 12 ++++++------ src/wl_init.c | 12 ++++++------ src/wl_monitor.c | 24 ++++++++++++------------ src/x11_monitor.c | 30 +++++++++++++++--------------- src/x11_window.c | 28 ++++++++++++++-------------- 19 files changed, 216 insertions(+), 216 deletions(-) diff --git a/src/cocoa_monitor.m b/src/cocoa_monitor.m index 585ee4cfa..a50c69717 100644 --- a/src/cocoa_monitor.m +++ b/src/cocoa_monitor.m @@ -67,14 +67,14 @@ static char* getDisplayName(CGDirectDisplayID displayID) size = CFStringGetMaximumSizeForEncoding(CFStringGetLength(value), kCFStringEncodingUTF8); name = calloc(size + 1, sizeof(char)); - if (name == NULL) - { - _glfwInputError(GLFW_OUT_OF_MEMORY, - "Cocoa: Failed to allocate name"); + if (name == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "Cocoa: Failed to allocate name"); - CFRelease(info); - return strdup("Unknown"); - } + CFRelease(info); + return strdup("Unknown"); + } CFStringGetCString(value, name, size, kCFStringEncodingUTF8); @@ -262,22 +262,22 @@ _GLFWmonitor** _glfwPlatformGetMonitors(int* count) CGGetOnlineDisplayList(0, NULL, &displayCount); displays = calloc(displayCount, sizeof(CGDirectDisplayID)); - if (displays == NULL) - { - _glfwInputError(GLFW_OUT_OF_MEMORY, - "Failed to allocate displays"); - return NULL; - } + if (displays == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "Failed to allocate displays"); + return NULL; + } monitors = calloc(displayCount, sizeof(_GLFWmonitor*)); - if (monitors == NULL) - { - _glfwInputError(GLFW_OUT_OF_MEMORY, - "Failed to allocate monitors"); + if (monitors == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "Failed to allocate monitors"); - free(displays); - return NULL; - } + free(displays); + return NULL; + } CGGetOnlineDisplayList(displayCount, displays, &displayCount); NSArray* screens = [NSScreen screens]; diff --git a/src/cocoa_window.m b/src/cocoa_window.m index b86d1928f..7a5d0a7cc 100644 --- a/src/cocoa_window.m +++ b/src/cocoa_window.m @@ -613,10 +613,10 @@ static int translateKey(unsigned int key) { NSEnumerator* e = [files objectEnumerator]; char** paths = calloc(count, sizeof(char*)); - if (paths == NULL) - { - return NO; - } + if (paths == NULL) + { + return NO; + } int i; diff --git a/src/egl_context.c b/src/egl_context.c index 48d607a9f..4bc0a9901 100644 --- a/src/egl_context.c +++ b/src/egl_context.c @@ -101,24 +101,24 @@ static GLboolean chooseFBConfigs(const _GLFWctxconfig* ctxconfig, } nativeConfigs = calloc(nativeCount, sizeof(EGLConfig)); - if (nativeConfigs == NULL) - { - _glfwInputError(GLFW_OUT_OF_MEMORY, - "EGL: Failed to allocate native configs"); - return GL_FALSE; - } + if (nativeConfigs == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "EGL: Failed to allocate native configs"); + return GL_FALSE; + } eglGetConfigs(_glfw.egl.display, nativeConfigs, nativeCount, &nativeCount); usableConfigs = calloc(nativeCount, sizeof(_GLFWfbconfig)); - if (usableConfigs == NULL) - { - _glfwInputError(GLFW_OUT_OF_MEMORY, - "EGL: Failed to allocate usable configs"); - - free(nativeConfigs); - return GL_FALSE; - } + if (usableConfigs == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "EGL: Failed to allocate usable configs"); + + free(nativeConfigs); + return GL_FALSE; + } usableCount = 0; diff --git a/src/glx_context.c b/src/glx_context.c index 6e0a7e79a..c7a65802d 100644 --- a/src/glx_context.c +++ b/src/glx_context.c @@ -72,12 +72,12 @@ static GLboolean chooseFBConfig(const _GLFWfbconfig* desired, GLXFBConfig* resul } usableConfigs = calloc(nativeCount, sizeof(_GLFWfbconfig)); - if (usableConfigs == NULL) - { - _glfwInputError(GLFW_OUT_OF_MEMORY, - "GLX: Failed to allocate usable configs"); - return GL_FALSE; - } + if (usableConfigs == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "GLX: Failed to allocate usable configs"); + return GL_FALSE; + } usableCount = 0; diff --git a/src/input.c b/src/input.c index ee43c3cdd..d391005a5 100644 --- a/src/input.c +++ b/src/input.c @@ -366,12 +366,12 @@ GLFWAPI GLFWcursor* glfwCreateCursor(const GLFWimage* image, int xhot, int yhot) _GLFW_REQUIRE_INIT_OR_RETURN(NULL); cursor = calloc(1, sizeof(_GLFWcursor)); - if (cursor == NULL) - { - _glfwInputError(GLFW_OUT_OF_MEMORY, - "Failed to allocate cursor"); - return NULL; - } + if (cursor == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "Failed to allocate cursor"); + return NULL; + } cursor->next = _glfw.cursorListHead; _glfw.cursorListHead = cursor; @@ -403,12 +403,12 @@ GLFWAPI GLFWcursor* glfwCreateStandardCursor(int shape) } cursor = calloc(1, sizeof(_GLFWcursor)); - if (cursor == NULL) - { - _glfwInputError(GLFW_OUT_OF_MEMORY, - "Failed to allocate cursor"); - return NULL; - } + if (cursor == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "Failed to allocate cursor"); + return NULL; + } cursor->next = _glfw.cursorListHead; _glfw.cursorListHead = cursor; diff --git a/src/iokit_joystick.m b/src/iokit_joystick.m index cc7d7b4da..0763d0530 100644 --- a/src/iokit_joystick.m +++ b/src/iokit_joystick.m @@ -110,12 +110,12 @@ static void addJoystickElement(_GLFWjoydevice* joystick, if (elementsArray) { _GLFWjoyelement* element = calloc(1, sizeof(_GLFWjoyelement)); - if (element == NULL) - { - _glfwInputError(GLFW_OUT_OF_MEMORY, - "IOKit: Failed to allocate element"); - return; - } + if (element == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "IOKit: Failed to allocate element"); + return; + } CFArrayAppendValue(elementsArray, element); @@ -319,25 +319,25 @@ static void matchCallback(void* context, joystick->axes = calloc(CFArrayGetCount(joystick->axisElements), sizeof(float)); - if (joystick->axes == NULL) - { - _glfwInputError(GLFW_OUT_OF_MEMORY, - "IOKit: Failed to allocate axes"); - - joystick->present = GL_FALSE; - return; - } + if (joystick->axes == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "IOKit: Failed to allocate axes"); + + joystick->present = GL_FALSE; + return; + } joystick->buttons = calloc(CFArrayGetCount(joystick->buttonElements) + CFArrayGetCount(joystick->hatElements) * 4, 1); - if (joystick->buttons == NULL) - { - _glfwInputError(GLFW_OUT_OF_MEMORY, - "IOKit: Failed to allocate buttons"); + if (joystick->buttons == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "IOKit: Failed to allocate buttons"); - joystick->present = GL_FALSE; - return; - } + joystick->present = GL_FALSE; + return; + } } // Callback for user-initiated joystick removal diff --git a/src/linux_joystick.c b/src/linux_joystick.c index 533e02157..0783819e8 100644 --- a/src/linux_joystick.c +++ b/src/linux_joystick.c @@ -98,23 +98,23 @@ static void openJoystickDevice(const char* path) _glfw.linux_js.js[joy].buttonCount = (int) buttonCount; _glfw.linux_js.js[joy].axes = calloc(axisCount, sizeof(float)); - if (_glfw.linux_js.js[joy].axes == NULL) - { - _glfwInputError(GLFW_OUT_OF_MEMORY, - "Linux: Failed to allocate joystix axes"); - close(fd); - return; - } + if (_glfw.linux_js.js[joy].axes == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "Linux: Failed to allocate joystix axes"); + close(fd); + return; + } _glfw.linux_js.js[joy].buttons = calloc(buttonCount, 1); - if (_glfw.linux_js.js[joy].buttons == NULL) - { - _glfwInputError(GLFW_OUT_OF_MEMORY, - "Linux: Failed to allocate joystick buttons"); - close(fd); - return; - } - + if (_glfw.linux_js.js[joy].buttons == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "Linux: Failed to allocate joystick buttons"); + close(fd); + return; + } + _glfw.linux_js.js[joy].present = GL_TRUE; #endif // __linux__ } diff --git a/src/mir_init.c b/src/mir_init.c index 3c6b13ba5..14a53e5dc 100644 --- a/src/mir_init.c +++ b/src/mir_init.c @@ -59,12 +59,12 @@ int _glfwPlatformInit(void) _glfwInitJoysticks(); _glfw.mir.event_queue = calloc(1, sizeof(EventQueue)); - if (_glfw.mir.event_queue == NULL) - { - _glfwInputError(GLFW_OUT_OF_MEMORY, - "Mir: Failed to allocate event queue"); - return GL_FALSE; - } + if (_glfw.mir.event_queue == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "Mir: Failed to allocate event queue"); + return GL_FALSE; + } _glfwInitEventQueue(_glfw.mir.event_queue); diff --git a/src/mir_monitor.c b/src/mir_monitor.c index dd994e23b..a533ad689 100644 --- a/src/mir_monitor.c +++ b/src/mir_monitor.c @@ -100,14 +100,14 @@ GLFWvidmode* _glfwPlatformGetVideoModes(_GLFWmonitor* monitor, int* found) continue; modes = calloc(out->num_modes, sizeof(GLFWvidmode)); - if (modes == NULL) - { - _glfwInputError(GLFW_OUT_OF_MEMORY, - "Mir: Failed to allocate modes"); - - mir_display_config_destroy(displayConfig); - return NULL; - } + if (modes == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "Mir: Failed to allocate modes"); + + mir_display_config_destroy(displayConfig); + return NULL; + } for (*found = 0; *found < out->num_modes; (*found)++) { diff --git a/src/mir_window.c b/src/mir_window.c index 56398b0e8..1d39e0288 100644 --- a/src/mir_window.c +++ b/src/mir_window.c @@ -52,23 +52,23 @@ static int emptyEventQueue(EventQueue* queue) static EventNode* newEventNode(MirEvent const* event, _GLFWwindow* context) { EventNode* new_node = calloc(1, sizeof(EventNode)); - if (new_node == NULL) - { - _glfwInputError(GLFW_OUT_OF_MEMORY, - "Mir: Failed to allocate new node"); - return NULL; - } + if (new_node == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "Mir: Failed to allocate new node"); + return NULL; + } new_node->event = calloc(1, sizeof(MirEvent)); if (new_node->event == NULL) - { - _glfwInputError(GLFW_OUT_OF_MEMORY, - "Mir: failed to allocate new node event"); - free(new_node); - return NULL; - } + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "Mir: failed to allocate new node event"); + free(new_node); + return NULL; + } - new_node->window = context; + new_node->window = context; memcpy(new_node->event, event, sizeof(MirEvent)); return new_node; diff --git a/src/monitor.c b/src/monitor.c index e1703daca..74cbd09cc 100644 --- a/src/monitor.c +++ b/src/monitor.c @@ -166,12 +166,12 @@ void _glfwInputMonitorChange(void) _GLFWmonitor* _glfwAllocMonitor(const char* name, int widthMM, int heightMM) { _GLFWmonitor* monitor = calloc(1, sizeof(_GLFWmonitor)); - if (monitor == NULL) - { - _glfwInputError(GLFW_OUT_OF_MEMORY, - "Failed to allocate monitor"); - return NULL; - } + if (monitor == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "Failed to allocate monitor"); + return NULL; + } monitor->name = strdup(name); monitor->widthMM = widthMM; @@ -199,13 +199,13 @@ void _glfwAllocGammaArrays(GLFWgammaramp* ramp, unsigned int size) ramp->green = calloc(size, sizeof(unsigned short)); ramp->blue = calloc(size, sizeof(unsigned short)); - if (ramp->red == NULL || - ramp->green == NULL || - ramp->blue == NULL) - { - _glfwInputError(GLFW_OUT_OF_MEMORY, - "Failed to allocate gamma arrays"); - } + if (ramp->red == NULL || + ramp->green == NULL || + ramp->blue == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "Failed to allocate gamma arrays"); + } ramp->size = size; } diff --git a/src/wgl_context.c b/src/wgl_context.c index 2d3beff8a..a40d4db46 100644 --- a/src/wgl_context.c +++ b/src/wgl_context.c @@ -123,12 +123,12 @@ static GLboolean choosePixelFormat(_GLFWwindow* window, } usableConfigs = calloc(nativeCount, sizeof(_GLFWfbconfig)); - if (usableConfigs == NULL) - { - _glfwInputError(GLFW_OUT_OF_MEMORY, - "WGL: Failed to allocate usable configs"); - return GL_FALSE; - } + if (usableConfigs == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "WGL: Failed to allocate usable configs"); + return GL_FALSE; + } usableCount = 0; diff --git a/src/win32_init.c b/src/win32_init.c index 45ad32418..cbf75e7ac 100644 --- a/src/win32_init.c +++ b/src/win32_init.c @@ -284,12 +284,12 @@ WCHAR* _glfwCreateWideStringFromUTF8(const char* source) return NULL; target = calloc(length, sizeof(WCHAR)); - if (target == NULL) - { - _glfwInputError(GLFW_OUT_OF_MEMORY, - "Win32 Init: Failed to allocate target"); - return NULL; - } + if (target == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "Win32 Init: Failed to allocate target"); + return NULL; + } if (!MultiByteToWideChar(CP_UTF8, 0, source, -1, target, length)) { @@ -312,12 +312,12 @@ char* _glfwCreateUTF8FromWideString(const WCHAR* source) return NULL; target = calloc(length, sizeof(char)); - if (target == NULL) - { - _glfwInputError(GLFW_OUT_OF_MEMORY, - "Win32 Init: Failed to allocate target"); - return NULL; - } + if (target == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "Win32 Init: Failed to allocate target"); + return NULL; + } if (!WideCharToMultiByte(CP_UTF8, 0, source, -1, target, length, NULL, NULL)) { diff --git a/src/win32_window.c b/src/win32_window.c index 881835e02..49b2476c2 100644 --- a/src/win32_window.c +++ b/src/win32_window.c @@ -602,13 +602,13 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, const int count = DragQueryFileW(hDrop, 0xffffffff, NULL, 0); char** paths = calloc(count, sizeof(char*)); - if (paths == NULL) - { - _glfwInputError(GLFW_OUT_OF_MEMORY, - "Win32: Failed to allocate paths"); + if (paths == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "Win32: Failed to allocate paths"); - return 0; - } + return 0; + } // Move the mouse to the position of the drop DragQueryPoint(hDrop, &pt); @@ -618,13 +618,13 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, { const UINT length = DragQueryFileW(hDrop, i, NULL, 0); WCHAR* buffer = calloc(length + 1, sizeof(WCHAR)); - if (buffer == NULL) - { - _glfwInputError(GLFW_OUT_OF_MEMORY, - "Win32: Failed to allocate buffer"); - free(paths); - return 0; - } + if (buffer == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "Win32: Failed to allocate buffer"); + free(paths); + return 0; + } DragQueryFileW(hDrop, i, buffer, length + 1); paths[i] = _glfwCreateUTF8FromWideString(buffer); diff --git a/src/window.c b/src/window.c index 690967daf..f904777de 100644 --- a/src/window.c +++ b/src/window.c @@ -154,12 +154,12 @@ GLFWAPI GLFWwindow* glfwCreateWindow(int width, int height, return NULL; window = calloc(1, sizeof(_GLFWwindow)); - if (window == NULL) - { - _glfwInputError(GLFW_OUT_OF_MEMORY, - "Failed to allocate window"); - return NULL; - } + if (window == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "Failed to allocate window"); + return NULL; + } window->next = _glfw.windowListHead; _glfw.windowListHead = window; diff --git a/src/wl_init.c b/src/wl_init.c index 81debdeb7..326933c9e 100644 --- a/src/wl_init.c +++ b/src/wl_init.c @@ -555,12 +555,12 @@ int _glfwPlatformInit(void) wl_registry_add_listener(_glfw.wl.registry, ®istryListener, NULL); _glfw.wl.monitors = calloc(4, sizeof(_GLFWmonitor*)); - if (_glfw.wl.monitors == NULL) - { - _glfwInputError(GLFW_OUT_OF_MEMORY, - "WL: Failed to allocate monitors"); - return GL_FALSE; - } + if (_glfw.wl.monitors == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "WL: Failed to allocate monitors"); + return GL_FALSE; + } _glfw.wl.monitorsSize = 4; _glfw.wl.xkb.context = xkb_context_new(0); diff --git a/src/wl_monitor.c b/src/wl_monitor.c index 8cb07f03e..e7646be27 100644 --- a/src/wl_monitor.c +++ b/src/wl_monitor.c @@ -140,12 +140,12 @@ void _glfwAddOutput(uint32_t name, uint32_t version) } monitor->wl.modes = calloc(4, sizeof(_GLFWvidmodeWayland)); - if (monitor->wl.modes == NULL) - { - _glfwInputError(GLFW_OUT_OF_MEMORY, - "Wayland: Failed to allocate modes"); - return; - } + if (monitor->wl.modes == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "Wayland: Failed to allocate modes"); + return; + } monitor->wl.modesSize = 4; @@ -158,12 +158,12 @@ void _glfwAddOutput(uint32_t name, uint32_t version) int size = _glfw.wl.monitorsSize * 2; monitors = realloc(monitors, size * sizeof(_GLFWmonitor*)); - if (monitors == NULL) - { - _glfwInputError(GLFW_OUT_OF_MEMORY, - "Wayland: Failed to allocate monitors"); - return; - } + if (monitors == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "Wayland: Failed to allocate monitors"); + return; + } _glfw.wl.monitors = monitors; _glfw.wl.monitorsSize = size; diff --git a/src/x11_monitor.c b/src/x11_monitor.c index fc699bbfc..b455de2b2 100644 --- a/src/x11_monitor.c +++ b/src/x11_monitor.c @@ -215,14 +215,14 @@ _GLFWmonitor** _glfwPlatformGetMonitors(int* count) _glfw.x11.root); monitors = calloc(sr->noutput, sizeof(_GLFWmonitor*)); - if (monitors == NULL) - { - _glfwInputError(GLFW_OUT_OF_MEMORY, - "X11: Failed to allocate monitors"); - - *count = 0; - return NULL; - } + if (monitors == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "X11: Failed to allocate monitors"); + + *count = 0; + return NULL; + } if (_glfw.x11.xinerama.available) screens = XineramaQueryScreens(_glfw.x11.display, &screenCount); @@ -302,13 +302,13 @@ _GLFWmonitor** _glfwPlatformGetMonitors(int* count) if (!monitors) { monitors = calloc(1, sizeof(_GLFWmonitor*)); - if (monitors == NULL) - { - _glfwInputError(GLFW_OUT_OF_MEMORY, - "X11: Failed to allocate monitors"); - *count = 0; - return NULL; - } + if (monitors == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "X11: Failed to allocate monitors"); + *count = 0; + return NULL; + } monitors[0] = _glfwAllocMonitor("Display", DisplayWidthMM(_glfw.x11.display, diff --git a/src/x11_window.c b/src/x11_window.c index 6017c86fb..7fe405ba2 100644 --- a/src/x11_window.c +++ b/src/x11_window.c @@ -224,23 +224,23 @@ static char** parseUriList(char* text, int* count) (*count)++; char* path = calloc(strlen(line) + 1, 1); - if (path == NULL) - { - _glfwInputError(GLFW_OUT_OF_MEMORY, - "X11: Failed to allocate path"); - return NULL; - } + if (path == NULL) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "X11: Failed to allocate path"); + return NULL; + } paths = realloc(paths, *count * sizeof(char*)); if (paths == NULL) - { - _glfwInputError(GLFW_OUT_OF_MEMORY, - "X11: Failed to allocate paths"); - free(path); - return NULL; - } - - paths[*count - 1] = path; + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "X11: Failed to allocate paths"); + free(path); + return NULL; + } + + paths[*count - 1] = path; while (*line) {