Skip to content

Fix potential memory leak in SDL_HIDAPI_HapticOpenFromJoystick() on error#15140

Merged
slouken merged 1 commit into
libsdl-org:mainfrom
Sackzement:mem_leak_hidapihaptic
Mar 1, 2026
Merged

Fix potential memory leak in SDL_HIDAPI_HapticOpenFromJoystick() on error#15140
slouken merged 1 commit into
libsdl-org:mainfrom
Sackzement:mem_leak_hidapihaptic

Conversation

@Sackzement

Copy link
Copy Markdown
Contributor

Freeing list_node before returning.

Warning

SDL/src/haptic/hidapi/SDL_hidapihaptic.c:150:17: warning: Potential leak of memory pointed to by 'list_node' [clang-analyzer-unix.Malloc]
  150 |                 device->driver->Close(device);
      |                 ^
SDL/src/haptic/hidapi/SDL_hidapihaptic.c:103:9: note: Assuming the condition is false
  103 |     if (joystick->driver != &SDL_HIDAPI_JoystickDriver) {
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SDL/src/haptic/hidapi/SDL_hidapihaptic.c:103:5: note: Taking false branch
  103 |     if (joystick->driver != &SDL_HIDAPI_JoystickDriver) {
      |     ^
SDL/src/haptic/hidapi/SDL_hidapihaptic.c:107:5: note: Loop condition is true.  Entering loop body
  107 |     for (i = 0; i < numdrivers; ++i) {
      |     ^
SDL/src/haptic/hidapi/SDL_hidapihaptic.c:108:13: note: Assuming the condition is true
  108 |         if (drivers[i]->JoystickSupported(joystick)) {
      |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SDL/src/haptic/hidapi/SDL_hidapihaptic.c:108:9: note: Taking true branch
  108 |         if (drivers[i]->JoystickSupported(joystick)) {
      |         ^
SDL/src/haptic/hidapi/SDL_hidapihaptic.c:113:17: note: Assuming 'ctx' is not equal to NULL
  113 |             if (ctx == NULL) {
      |                 ^~~~~~~~~~~
SDL/src/haptic/hidapi/SDL_hidapihaptic.c:113:13: note: Taking false branch
  113 |             if (ctx == NULL) {
      |             ^
SDL/src/haptic/hidapi/SDL_hidapihaptic.c:118:17: note: Assuming 'device' is not equal to NULL
  118 |             if (device == NULL) {
      |                 ^~~~~~~~~~~~~~
SDL/src/haptic/hidapi/SDL_hidapihaptic.c:118:13: note: Taking false branch
  118 |             if (device == NULL) {
      |             ^
SDL/src/haptic/hidapi/SDL_hidapihaptic.c:132:25: note: Memory is allocated
  132 |             list_node = SDL_malloc(sizeof(haptic_list_node));
      |                         ^
SDL/include/SDL3/SDL_stdinc.h:6037:20: note: expanded from macro 'SDL_malloc'
 6037 | #define SDL_malloc malloc
      |                    ^
SDL/src/haptic/hidapi/SDL_hidapihaptic.c:133:17: note: Assuming 'list_node' is not equal to NULL
  133 |             if (list_node == NULL) {
      |                 ^~~~~~~~~~~~~~~~~
SDL/src/haptic/hidapi/SDL_hidapihaptic.c:133:13: note: Taking false branch
  133 |             if (list_node == NULL) {
      |             ^
SDL/src/haptic/hidapi/SDL_hidapihaptic.c:149:17: note: Assuming field 'effects' is equal to NULL
  149 |             if (haptic->effects == NULL) {
      |                 ^~~~~~~~~~~~~~~~~~~~~~~
SDL/src/haptic/hidapi/SDL_hidapihaptic.c:149:13: note: Taking true branch
  149 |             if (haptic->effects == NULL) {
      |             ^
SDL/src/haptic/hidapi/SDL_hidapihaptic.c:150:17: note: Potential leak of memory pointed to by 'list_node'
  150 |                 device->driver->Close(device);
      |                 ^

@slouken
slouken merged commit 6cf03ca into libsdl-org:main Mar 1, 2026
45 checks passed
@slouken

slouken commented Mar 1, 2026

Copy link
Copy Markdown
Collaborator

Merged, thanks!

@slouken slouken added this to the 3.4.4 milestone Mar 1, 2026
@Sackzement
Sackzement deleted the mem_leak_hidapihaptic branch March 1, 2026 20:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants