From 497a1925ecc0550630ba44f29d2db7378e7c9e4e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jan=20Dj=C3=A4rv?= Date: Sat, 29 Jan 2011 12:30:11 +0100 Subject: [PATCH] Handle nul-character in copy/paste (Bug#7934). * nsselect.m (ns_string_from_pasteboard): Get length of string and use make_string instead of build_string. (ns_string_to_pasteboard_internal): Use initWithBytesNoCopy instead of stringWithUTF8String (Bug#7934). --- src/ChangeLog | 7 +++++++ src/nsselect.m | 20 +++++++++++++++----- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index f8686529221..340ca37aae4 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,10 @@ +2011-01-29 Jan Djärv + + * nsselect.m (ns_string_from_pasteboard): Get length of string + and use make_string instead of build_string (Bug#7934). + (ns_string_to_pasteboard_internal): Use initWithBytesNoCopy + instead of stringWithUTF8String (Bug#7934). + 2011-01-29 Anders Lindgren (tiny change) * nsfont.m (nsfont_open): Ensure that fonts with inexact diff --git a/src/nsselect.m b/src/nsselect.m index d9e9e7d524c..299a24e4a8c 100644 --- a/src/nsselect.m +++ b/src/nsselect.m @@ -153,8 +153,10 @@ ns_string_to_pasteboard_internal (id pb, Lisp_Object str, NSString *gtype) CHECK_STRING (str); utfStr = SDATA (str); - nsStr = [NSString stringWithUTF8String: utfStr]; - + nsStr = [[NSString alloc] initWithBytesNoCopy: utfStr + length: SBYTES (str) + encoding: NSUTF8StringEncoding + freeWhenDone: NO]; if (gtype == nil) { [pb declareTypes: ns_send_types owner: nil]; @@ -166,6 +168,7 @@ ns_string_to_pasteboard_internal (id pb, Lisp_Object str, NSString *gtype) { [pb setString: nsStr forType: gtype]; } + [nsStr release]; } } @@ -303,6 +306,7 @@ ns_string_from_pasteboard (id pb) { NSString *type, *str; const char *utfStr; + int length; type = [pb availableTypeFromArray: ns_return_types]; if (type == nil) @@ -344,17 +348,23 @@ ns_string_from_pasteboard (id pb) options: NSLiteralSearch range: NSMakeRange (0, [mstr length])]; utfStr = [mstr UTF8String]; - if (!utfStr) - utfStr = [mstr cString]; + length = [mstr lengthOfBytesUsingEncoding: NSUTF8StringEncoding]; + + if (!utfStr) + { + utfStr = [mstr cString]; + length = strlen (utfStr); + } } NS_HANDLER { message1 ("ns_string_from_pasteboard: UTF8String failed\n"); utfStr = [str lossyCString]; + length = strlen (utfStr); } NS_ENDHANDLER - return build_string (utfStr); + return make_string (utfStr, length); } -- 2.39.2