From 980f955f6c6ccc2f00ff8eaab049da3a3acc46b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camilla=20L=C3=B6wy?= Date: Thu, 16 Feb 2017 05:23:10 +0100 Subject: [PATCH] Fix undefined behavior Fixes #950. --- src/cocoa_monitor.m | 11 ++++++++--- src/win32_monitor.c | 13 ++++++++----- src/x11_monitor.c | 13 ++++++++----- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/cocoa_monitor.m b/src/cocoa_monitor.m index 382cec8c..3b2eccfb 100644 --- a/src/cocoa_monitor.m +++ b/src/cocoa_monitor.m @@ -215,15 +215,20 @@ void _glfwPollMonitorsNS(void) { uint32_t i, j, displayCount, disconnectedCount; CGDirectDisplayID* displays; - _GLFWmonitor** disconnected; + _GLFWmonitor** disconnected = NULL; CGGetOnlineDisplayList(0, NULL, &displayCount); displays = calloc(displayCount, sizeof(CGDirectDisplayID)); CGGetOnlineDisplayList(displayCount, displays, &displayCount); disconnectedCount = _glfw.monitorCount; - disconnected = calloc(_glfw.monitorCount, sizeof(_GLFWmonitor*)); - memcpy(disconnected, _glfw.monitors, _glfw.monitorCount * sizeof(_GLFWmonitor*)); + if (disconnectedCount) + { + disconnected = calloc(_glfw.monitorCount, sizeof(_GLFWmonitor*)); + memcpy(disconnected, + _glfw.monitors, + _glfw.monitorCount * sizeof(_GLFWmonitor*)); + } for (i = 0; i < displayCount; i++) { diff --git a/src/win32_monitor.c b/src/win32_monitor.c index 58a69d7d..cc5e24f8 100644 --- a/src/win32_monitor.c +++ b/src/win32_monitor.c @@ -91,16 +91,19 @@ static _GLFWmonitor* createMonitor(DISPLAY_DEVICEW* adapter, void _glfwPollMonitorsWin32(void) { int i, disconnectedCount; - _GLFWmonitor** disconnected; + _GLFWmonitor** disconnected = NULL; DWORD adapterIndex, displayIndex; DISPLAY_DEVICEW adapter, display; GLFWbool hasDisplays = GLFW_FALSE; disconnectedCount = _glfw.monitorCount; - disconnected = calloc(_glfw.monitorCount, sizeof(_GLFWmonitor*)); - memcpy(disconnected, - _glfw.monitors, - _glfw.monitorCount * sizeof(_GLFWmonitor*)); + if (disconnectedCount) + { + disconnected = calloc(_glfw.monitorCount, sizeof(_GLFWmonitor*)); + memcpy(disconnected, + _glfw.monitors, + _glfw.monitorCount * sizeof(_GLFWmonitor*)); + } // HACK: Check if any active adapters have connected displays // If not, this is a headless system or a VMware guest diff --git a/src/x11_monitor.c b/src/x11_monitor.c index e22c16d7..a32d01f4 100644 --- a/src/x11_monitor.c +++ b/src/x11_monitor.c @@ -102,7 +102,7 @@ void _glfwPollMonitorsX11(void) if (_glfw.x11.randr.available && !_glfw.x11.randr.monitorBroken) { int i, j, disconnectedCount, screenCount = 0; - _GLFWmonitor** disconnected; + _GLFWmonitor** disconnected = NULL; XineramaScreenInfo* screens = NULL; XRRScreenResources* sr = XRRGetScreenResourcesCurrent(_glfw.x11.display, _glfw.x11.root); @@ -113,10 +113,13 @@ void _glfwPollMonitorsX11(void) screens = XineramaQueryScreens(_glfw.x11.display, &screenCount); disconnectedCount = _glfw.monitorCount; - disconnected = calloc(_glfw.monitorCount, sizeof(_GLFWmonitor*)); - memcpy(disconnected, - _glfw.monitors, - _glfw.monitorCount * sizeof(_GLFWmonitor*)); + if (disconnectedCount) + { + disconnected = calloc(_glfw.monitorCount, sizeof(_GLFWmonitor*)); + memcpy(disconnected, + _glfw.monitors, + _glfw.monitorCount * sizeof(_GLFWmonitor*)); + } for (i = 0; i < sr->noutput; i++) {