Skip to content

Fix memory leak when X11 toolkit window locale SDL_strdup() fails#15138

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

Fix memory leak when X11 toolkit window locale SDL_strdup() fails#15138
slouken merged 1 commit into
libsdl-org:mainfrom
Sackzement:mem_leak_x11toolkit

Conversation

@Sackzement

Copy link
Copy Markdown
Contributor

Freeing dynamically allocated window before returning NULL.

It's unlikely that SDL_strdup would fail, but this at least gets rid of one more clang-tidy clang-analyzer-unix.Malloc warning.

Warning

SDL/src/video/x11/SDL_x11toolkit.c:979:24: warning: Potential leak of memory pointed to by 'window' [clang-analyzer-unix.Malloc]
  979 |                 return NULL;
      |                        ^
/usr/lib/clang/21/include/__stddef_null.h:26:22: note: expanded from macro 'NULL'
   26 | #define NULL ((void*)0)
      |                      ^
SDL/src/video/x11/SDL_x11toolkit.c:957:9: note: Assuming the condition is false
  957 |     if (!SDL_X11_LoadSymbols()) {
      |         ^~~~~~~~~~~~~~~~~~~~~~
SDL/src/video/x11/SDL_x11toolkit.c:957:5: note: Taking false branch
  957 |     if (!SDL_X11_LoadSymbols()) {
      |     ^
SDL/src/video/x11/SDL_x11toolkit.c:964:38: note: Memory is allocated
  964 |     window = (SDL_ToolkitWindowX11 *)SDL_malloc(sizeof(SDL_ToolkitWindowX11));
      |                                      ^
SDL/include/SDL3/SDL_stdinc.h:6037:20: note: expanded from macro 'SDL_malloc'
 6037 | #define SDL_malloc malloc
      |                    ^
SDL/src/video/x11/SDL_x11toolkit.c:965:9: note: Assuming 'window' is non-null
  965 |     if (!window) {
      |         ^~~~~~~
SDL/src/video/x11/SDL_x11toolkit.c:965:5: note: Taking false branch
  965 |     if (!window) {
      |     ^
SDL/src/video/x11/SDL_x11toolkit.c:974:9: note: Assuming 'mode' is equal to SDL_TOOLKIT_WINDOW_MODE_X11_DIALOG
  974 |     if (mode == SDL_TOOLKIT_WINDOW_MODE_X11_DIALOG) {
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SDL/src/video/x11/SDL_x11toolkit.c:974:5: note: Taking true branch
  974 |     if (mode == SDL_TOOLKIT_WINDOW_MODE_X11_DIALOG) {
      |     ^
SDL/src/video/x11/SDL_x11toolkit.c:976:13: note: Assuming field 'origlocale' is non-null
  976 |         if (window->origlocale) {
      |             ^~~~~~~~~~~~~~~~~~
SDL/src/video/x11/SDL_x11toolkit.c:976:9: note: Taking true branch
  976 |         if (window->origlocale) {
      |         ^
SDL/src/video/x11/SDL_x11toolkit.c:978:17: note: Assuming field 'origlocale' is null
  978 |             if (!window->origlocale) {
      |                 ^~~~~~~~~~~~~~~~~~~
SDL/src/video/x11/SDL_x11toolkit.c:978:13: note: Taking true branch
  978 |             if (!window->origlocale) {
      |             ^
SDL/src/video/x11/SDL_x11toolkit.c:979:24: note: Potential leak of memory pointed to by 'window'
  979 |                 return NULL;
      |                        ^
/usr/lib/clang/21/include/__stddef_null.h:26:22: note: expanded from macro 'NULL'
   26 | #define NULL ((void*)0)
      |                      ^

@slouken
slouken merged commit fb1d1ab into libsdl-org:main Mar 1, 2026
44 of 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_x11toolkit branch March 1, 2026 20:04
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