From c992226a9c63f80e157d6bd9bf2372acd2a1c795 Mon Sep 17 00:00:00 2001 From: Michael Skec Date: Thu, 9 Nov 2023 15:04:19 +1100 Subject: [PATCH] Linux: Fix memory leak when inotify init failed This introduces regex_compiled boolean to track whether the regex is compiled successfully. Closes #2229 --- src/linux_joystick.c | 8 ++++++-- src/linux_joystick.h | 1 + 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/linux_joystick.c b/src/linux_joystick.c index 26db853e..5b2c3b34 100644 --- a/src/linux_joystick.c +++ b/src/linux_joystick.c @@ -326,7 +326,9 @@ GLFWbool _glfwInitJoysticksLinux(void) // Continue without device connection notifications if inotify fails - if (regcomp(&_glfw.linjs.regex, "^event[0-9]\\+$", 0) != 0) + _glfw.linjs.regex_compiled = regcomp(&_glfw.linjs.regex, + "^event[0-9]\\+$", 0) == 0; + if (!_glfw.linjs.regex_compiled) { _glfwInputError(GLFW_PLATFORM_ERROR, "Linux: Failed to compile regex"); return GLFW_FALSE; @@ -378,8 +380,10 @@ void _glfwTerminateJoysticksLinux(void) inotify_rm_watch(_glfw.linjs.inotify, _glfw.linjs.watch); close(_glfw.linjs.inotify); - regfree(&_glfw.linjs.regex); } + + if (_glfw.linjs.regex_compiled) + regfree(&_glfw.linjs.regex); } GLFWbool _glfwPollJoystickLinux(_GLFWjoystick* js, int mode) diff --git a/src/linux_joystick.h b/src/linux_joystick.h index df605e72..3e1fe8c8 100644 --- a/src/linux_joystick.h +++ b/src/linux_joystick.h @@ -50,6 +50,7 @@ typedef struct _GLFWlibraryLinux int inotify; int watch; regex_t regex; + GLFWbool regex_compiled; GLFWbool dropped; } _GLFWlibraryLinux;