From 22f718dcf4f2561f5cd41eaaf33762ec7a5880a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camilla=20L=C3=B6wy?= Date: Tue, 22 Mar 2022 18:46:57 +0100 Subject: [PATCH] Wayland: Fix handling of clipboard set to self Passing any part of the result of glfwGetClipboardString to glfwSetClipboardString would result in, at best, a use-after-free error. (cherry picked from commit 9c95cfb9f1cdf073856e5b479447f5fca6492cd9) --- README.md | 2 ++ src/wl_window.c | 20 +++++++++----------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index e8c1d56d..75d09bb9 100644 --- a/README.md +++ b/README.md @@ -129,6 +129,8 @@ information on what to include when reporting a bug. match event scancode (#1993) - [Win32] Bugfix: Instance-local operations used executable instance (#469,#1296,#1395) - [Cocoa] Bugfix: A connected Apple AirPlay would emit a useless error (#1791) + - [Wayland] Bugfix: `glfwSetClipboardString` would fail if set to result of + `glfwGetClipboardString` ## Contact diff --git a/src/wl_window.c b/src/wl_window.c index a8c35208..0bea8974 100644 --- a/src/wl_window.c +++ b/src/wl_window.c @@ -1798,20 +1798,18 @@ void _glfwPlatformSetClipboardString(const char* string) _glfw.wl.dataSource = NULL; } - if (_glfw.wl.clipboardSendString) + char* copy = _glfw_strdup(string); + if (!copy) { - free(_glfw.wl.clipboardSendString); - _glfw.wl.clipboardSendString = NULL; - } - - _glfw.wl.clipboardSendString = strdup(string); - if (!_glfw.wl.clipboardSendString) - { - _glfwInputError(GLFW_PLATFORM_ERROR, - "Wayland: Impossible to allocate clipboard string"); + _glfwInputError(GLFW_OUT_OF_MEMORY, + "Wayland: Failed to allocate clipboard string"); return; } - _glfw.wl.clipboardSendSize = strlen(string); + + free(_glfw.wl.clipboardSendString); + _glfw.wl.clipboardSendString = copy; + + _glfw.wl.clipboardSendSize = strlen(_glfw.wl.clipboardSendString); _glfw.wl.dataSource = wl_data_device_manager_create_data_source(_glfw.wl.dataDeviceManager); if (!_glfw.wl.dataSource)