Commit 9dc97afa7190aca5bdf923ed13d723197c083547

Ryan C. Gordon 2021-05-08T12:39:50

cocoa: Report an error if SDL_SetClipboardText() isn't using UTF-8 encoding. Fixes #4110.

diff --git a/src/video/cocoa/SDL_cocoaclipboard.m b/src/video/cocoa/SDL_cocoaclipboard.m
index a091d8f..bfd35f0 100644
--- a/src/video/cocoa/SDL_cocoaclipboard.m
+++ b/src/video/cocoa/SDL_cocoaclipboard.m
@@ -32,10 +32,14 @@ Cocoa_SetClipboardText(_THIS, const char *text)
     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
     NSPasteboard *pasteboard;
     NSString *format = NSPasteboardTypeString;
+    NSString *nsstr = [NSString stringWithUTF8String:text];
+    if (nsstr == nil) {
+        return SDL_SetError("Couldn't create NSString; is your string data in UTF-8 format?");
+    }
 
     pasteboard = [NSPasteboard generalPasteboard];
     data->clipboard_count = [pasteboard declareTypes:[NSArray arrayWithObject:format] owner:nil];
-    [pasteboard setString:[NSString stringWithUTF8String:text] forType:format];
+    [pasteboard setString:nsstr forType:format];
 
     return 0;
 }}
@@ -61,7 +65,7 @@ Cocoa_GetClipboardText(_THIS)
         } else {
             utf8 = [string UTF8String];
         }
-        text = SDL_strdup(utf8);
+        text = SDL_strdup(utf8 ? utf8 : "");
     } else {
         text = SDL_strdup("");
     }