From 8cf79aaf72e0ad8c04c9bffd2373a2cd0d9e8405 Mon Sep 17 00:00:00 2001 From: Misa Date: Tue, 30 Mar 2021 23:23:29 -0700 Subject: [PATCH] Fix memory leak when pasting text According to SDL documentation[1], the returned pointer needs to be freed. A glance at the source code confirms that the function allocates, and also Valgrind complains about it. Also if it couldn't allocate, the game no longer segfaults (std::strings do not check if the pointer is non-NULL for operator+=). [1]: https://wiki.libsdl.org/SDL_GetClipboardText --- desktop_version/src/KeyPoll.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/desktop_version/src/KeyPoll.cpp b/desktop_version/src/KeyPoll.cpp index 60abc843..c6cdb4e5 100644 --- a/desktop_version/src/KeyPoll.cpp +++ b/desktop_version/src/KeyPoll.cpp @@ -141,7 +141,12 @@ void KeyPoll::Poll(void) else if ( evt.key.keysym.sym == SDLK_v && keymap[SDLK_LCTRL] ) { - keybuffer += SDL_GetClipboardText(); + char* text = SDL_GetClipboardText(); + if (text != NULL) + { + keybuffer += text; + SDL_free(text); + } } } break;