From 0735250eb107dcf484a61e47cee50c2f8f1eb4fa Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Camilla=20L=C3=B6wy?= <elmindreda@glfw.org>
Date: Mon, 16 Jan 2017 01:36:14 +0100
Subject: [PATCH] X11: Fix potential window property data leaks

---
 src/x11_init.c   | 23 ++++++++++-------------
 src/x11_window.c | 19 ++++++++++++-------
 2 files changed, 22 insertions(+), 20 deletions(-)

diff --git a/src/x11_init.c b/src/x11_init.c
index b755f7f8..2bb2b65a 100644
--- a/src/x11_init.c
+++ b/src/x11_init.c
@@ -381,13 +381,11 @@ static void detectEWMH(void)
         XInternAtom(_glfw.x11.display, "_NET_SUPPORTED", False);
 
     // Then we look for the _NET_SUPPORTING_WM_CHECK property of the root window
-    if (_glfwGetWindowPropertyX11(_glfw.x11.root,
-                                  supportingWmCheck,
-                                  XA_WINDOW,
-                                  (unsigned char**) &windowFromRoot) != 1)
+    if (!_glfwGetWindowPropertyX11(_glfw.x11.root,
+                                   supportingWmCheck,
+                                   XA_WINDOW,
+                                   (unsigned char**) &windowFromRoot))
     {
-        if (windowFromRoot)
-            XFree(windowFromRoot);
         return;
     }
 
@@ -395,14 +393,12 @@ static void detectEWMH(void)
 
     // It should be the ID of a child window (of the root)
     // Then we look for the same property on the child window
-    if (_glfwGetWindowPropertyX11(*windowFromRoot,
-                                  supportingWmCheck,
-                                  XA_WINDOW,
-                                  (unsigned char**) &windowFromChild) != 1)
+    if (!_glfwGetWindowPropertyX11(*windowFromRoot,
+                                   supportingWmCheck,
+                                   XA_WINDOW,
+                                   (unsigned char**) &windowFromChild))
     {
         XFree(windowFromRoot);
-        if (windowFromChild)
-            XFree(windowFromChild);
         return;
     }
 
@@ -455,7 +451,8 @@ static void detectEWMH(void)
     _glfw.x11.NET_REQUEST_FRAME_EXTENTS =
         getSupportedAtom(supportedAtoms, atomCount, "_NET_REQUEST_FRAME_EXTENTS");
 
-    XFree(supportedAtoms);
+    if (supportedAtoms)
+        XFree(supportedAtoms);
 }
 
 // Initialize X11 display and look for supported X11 extensions
diff --git a/src/x11_window.c b/src/x11_window.c
index 35064cc0..980ae149 100644
--- a/src/x11_window.c
+++ b/src/x11_window.c
@@ -130,7 +130,9 @@ static int getWindowState(_GLFWwindow* window)
         result = state->state;
     }
 
-    XFree(state);
+    if (state)
+        XFree(state);
+
     return result;
 }
 
@@ -1324,7 +1326,8 @@ static void processEvent(XEvent *event)
                     free(paths);
                 }
 
-                XFree(data);
+                if (data)
+                    XFree(data);
 
                 XEvent reply;
                 memset(&reply, 0, sizeof(reply));
@@ -1481,9 +1484,6 @@ unsigned long _glfwGetWindowPropertyX11(Window window,
                        &bytesAfter,
                        value);
 
-    if (type != AnyPropertyType && actualType != type)
-        return 0;
-
     return itemCount;
 }
 
@@ -2042,7 +2042,9 @@ int _glfwPlatformWindowMaximized(_GLFWwindow* window)
         }
     }
 
-    XFree(states);
+    if (states)
+        XFree(states);
+
     return maximized;
 }
 
@@ -2107,6 +2109,8 @@ void _glfwPlatformSetWindowFloating(_GLFWwindow* window, GLFWbool enabled)
                                           _glfw.x11.NET_WM_STATE,
                                           XA_ATOM,
                                           (unsigned char**) &states);
+        if (!states)
+            return;
 
         if (enabled)
         {
@@ -2396,7 +2400,8 @@ const char* _glfwPlatformGetClipboardString(_GLFWwindow* window)
             _glfw.x11.clipboardString = strdup(data);
         }
 
-        XFree(data);
+        if (data)
+            XFree(data);
 
         XDeleteProperty(_glfw.x11.display,
                         event.xselection.requestor,