From 7c7cc59889883fa0c19daee122373271670e3b26 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 (cherry picked from commit c992226a9c63f80e157d6bd9bf2372acd2a1c795) --- src/linux_joystick.c | 7 ++++++- src/linux_joystick.h | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/linux_joystick.c b/src/linux_joystick.c index 5d8c8d04..6940cb38 100644 --- a/src/linux_joystick.c +++ b/src/linux_joystick.c @@ -283,7 +283,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; @@ -342,6 +344,9 @@ void _glfwTerminateJoysticksLinux(void) close(_glfw.linjs.inotify); } + + if (_glfw.linjs.regex_compiled) + regfree(&_glfw.linjs.regex); } void _glfwDetectJoystickConnectionLinux(void) diff --git a/src/linux_joystick.h b/src/linux_joystick.h index 25a2a2ee..e5e6f5dc 100644 --- a/src/linux_joystick.h +++ b/src/linux_joystick.h @@ -53,6 +53,7 @@ typedef struct _GLFWlibraryLinux int inotify; int watch; regex_t regex; + GLFWbool regex_compiled; GLFWbool dropped; } _GLFWlibraryLinux;