Bug 673301, crash using non-string data with text/plain drags, r=josh
authorNeil Deakin <neil@mozilla.com>
Thu, 18 Aug 2011 09:22:52 -0400
changeset 75495 6309d93cd072d3d9cc49a3c903e9022b3f64490b
parent 75494 58b7de3f9b79b713d74504e922c4bae8511cc34c
child 75496 e8cb0687737a90e89871b38707153e8f98042a6d
push id2
push userbsmedberg@mozilla.com
push dateFri, 19 Aug 2011 14:38:13 +0000
reviewersjosh
bugs673301
milestone9.0a1
Bug 673301, crash using non-string data with text/plain drags, r=josh
content/base/test/test_copypaste.html
widget/src/cocoa/nsClipboard.mm
widget/tests/Makefile.in
widget/tests/test_bug673301.xul
--- a/content/base/test/test_copypaste.html
+++ b/content/base/test/test_copypaste.html
@@ -48,18 +48,18 @@ function testCopyPaste () {
 
   var clipboard = Components.classes["@mozilla.org/widget/clipboard;1"]
                             .getService(Components.interfaces.nsIClipboard);
 
   var textarea = document.getElementById('input');
 
   function copySelectionToClipboard() {
     documentViewer.copySelection();
-    is(clipboard.hasDataMatchingFlavors(["text/unicode"], 1,1), true);
-    is(clipboard.hasDataMatchingFlavors(["text/html"], 1,1), true);
+    ok(clipboard.hasDataMatchingFlavors(["text/unicode"], 1,1), "check text/unicode");
+    ok(clipboard.hasDataMatchingFlavors(["text/html"], 1,1), "check text/html");
   }
   function copyToClipboard(node) {
     textarea.blur();
     clipboard.emptyClipboard(1);
     var sel = window.getSelection();
     sel.removeAllRanges();
     var r = document.createRange();
     r.selectNode(node);
--- a/widget/src/cocoa/nsClipboard.mm
+++ b/widget/src/cocoa/nsClipboard.mm
@@ -423,18 +423,23 @@ nsClipboard::PasteboardDictFromTransfera
     NSString *pboardType = nil;
 
     if (nsClipboard::IsStringType(flavorStr, &pboardType)) {
       void* data = nsnull;
       PRUint32 dataSize = 0;
       nsCOMPtr<nsISupports> genericDataWrapper;
       rv = aTransferable->GetTransferData(flavorStr, getter_AddRefs(genericDataWrapper), &dataSize);
       nsPrimitiveHelpers::CreateDataFromPrimitive(flavorStr, genericDataWrapper, &data, dataSize);
+
+      NSString* nativeString;
+      if (data)
+        nativeString = [NSString stringWithCharacters:(const unichar*)data length:(dataSize / sizeof(PRUnichar))];
+      else
+        nativeString = [NSString string];
       
-      NSString* nativeString = [NSString stringWithCharacters:(const unichar*)data length:(dataSize / sizeof(PRUnichar))];
       // be nice to Carbon apps, normalize the receiver's contents using Form C.
       nativeString = [nativeString precomposedStringWithCanonicalMapping];
 
       [pasteboardOutputDict setObject:nativeString forKey:pboardType];
       
       nsMemory::Free(data);
     }
     else if (flavorStr.EqualsLiteral(kPNGImageMime) || flavorStr.EqualsLiteral(kJPEGImageMime) ||
--- a/widget/tests/Makefile.in
+++ b/widget/tests/Makefile.in
@@ -105,16 +105,17 @@ ifeq ($(MOZ_WIDGET_TOOLKIT),cocoa)
                test_platform_colors.xul \
                test_standalone_native_menu.xul \
                standalone_native_menu_window.xul \
                test_bug586713.xul \
                bug586713_window.xul \
                test_key_event_counts.xul \
                test_bug596600.xul \
                window_bug596600.xul \
+               test_bug673301.xul \
                $(NULL)
 endif
 
 ifeq ($(MOZ_WIDGET_TOOLKIT),windows)
 _CHROME_FILES  += taskbar_previews.xul \
 		window_state_windows.xul \
 		taskbar_progress.xul \
 		test_chrome_context_menus_win.xul \
new file mode 100644
--- /dev/null
+++ b/widget/tests/test_bug673301.xul
@@ -0,0 +1,30 @@
+<?xml version="1.0"?>
+<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
+<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css"
+                 type="text/css"?>
+
+<window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
+
+<script type="application/javascript" src="chrome://mochikit/content/MochiKit/packed.js"/>
+<script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
+
+<body  xmlns="http://www.w3.org/1999/xhtml">
+<p id="display"></p>
+<div id="content" style="display: none"/>
+</body>
+
+<script type="application/javascript">
+var clipboard = Components.classes["@mozilla.org/widget/clipboard;1"]
+                          .getService(Components.interfaces.nsIClipboard);
+
+var transferable = Components.classes['@mozilla.org/widget/transferable;1']
+                             .createInstance(Components.interfaces.nsITransferable);
+
+transferable.addDataFlavor("text/unicode");
+transferable.setTransferData("text/unicode", document, 4);
+
+clipboard.setData(transferable, null, Components.interfaces.nsIClipboard.kGlobalClipboard);
+
+SimpleTest.ok(true, "Didn't crash setting non-text data for text/unicode type");
+</script>
+</window>