[PATCH] ui/cocoa: Fix clipboard text release

Akihiko Odaki posted 1 patch 1 year, 10 months ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20220614212131.94696-1-akihiko.odaki@gmail.com
Maintainers: Peter Maydell <peter.maydell@linaro.org>, "Philippe Mathieu-Daudé" <f4bug@amsat.org>, Akihiko Odaki <akihiko.odaki@gmail.com>, Gerd Hoffmann <kraxel@redhat.com>
ui/cocoa.m | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
[PATCH] ui/cocoa: Fix clipboard text release
Posted by Akihiko Odaki 1 year, 10 months ago
[-NSPasteboard dataForType:] returns an autoreleased NSString,
and callings its release method will result in double-free when
the global autorelease pool is released. Use NSAutoreleasePool to
release it properly.

Signed-off-by: Akihiko Odaki <akihiko.odaki@gmail.com>
---
 ui/cocoa.m | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/ui/cocoa.m b/ui/cocoa.m
index 84c84e98fc5..6a4dccff7f0 100644
--- a/ui/cocoa.m
+++ b/ui/cocoa.m
@@ -1894,16 +1894,18 @@ static void cocoa_clipboard_notify(Notifier *notifier, void *data)
 static void cocoa_clipboard_request(QemuClipboardInfo *info,
                                     QemuClipboardType type)
 {
+    NSAutoreleasePool *pool;
     NSData *text;
 
     switch (type) {
     case QEMU_CLIPBOARD_TYPE_TEXT:
+        pool = [[NSAutoreleasePool alloc] init];
         text = [[NSPasteboard generalPasteboard] dataForType:NSPasteboardTypeString];
         if (text) {
             qemu_clipboard_set_data(&cbpeer, info, type,
                                     [text length], [text bytes], true);
-            [text release];
         }
+        [pool release];
         break;
     default:
         break;
-- 
2.32.1 (Apple Git-133)
Re: [PATCH] ui/cocoa: Fix clipboard text release
Posted by Peter Maydell 1 year, 10 months ago
On Tue, 14 Jun 2022 at 22:21, Akihiko Odaki <akihiko.odaki@gmail.com> wrote:
>
> [-NSPasteboard dataForType:] returns an autoreleased NSString,
> and callings its release method will result in double-free when
> the global autorelease pool is released. Use NSAutoreleasePool to
> release it properly.
>
> Signed-off-by: Akihiko Odaki <akihiko.odaki@gmail.com>

Reviewed-by: Peter Maydell <peter.maydell@linaro.org>

thanks
-- PMM