summaryrefslogtreecommitdiff
path: root/mozilla-1631061-2.patch
blob: ae90baa976d4396579f6d50f03ac7d8876fb6c5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
changeset:   567293:4d5e5e9f146e
tag:         tip
parent:      567291:3bdf0c33844f
user:        stransky <stransky@redhat.com>
date:        Fri Jan 29 12:13:15 2021 +0100
files:       widget/gtk/nsClipboardWayland.cpp
description:
Bug 1631061 [Wayland] Clear clipboard content when gtk_clipboard_request_contents() fails, r?jhorak

Differential Revision: https://phabricator.services.mozilla.com/D103461


diff --git a/widget/gtk/nsClipboardWayland.cpp b/widget/gtk/nsClipboardWayland.cpp
--- a/widget/gtk/nsClipboardWayland.cpp
+++ b/widget/gtk/nsClipboardWayland.cpp
@@ -842,7 +842,8 @@ struct FastTrackClipboard {
 
 static void wayland_clipboard_contents_received(
     GtkClipboard* clipboard, GtkSelectionData* selection_data, gpointer data) {
-  LOGCLIP(("wayland_clipboard_contents_received() callback\n"));
+  LOGCLIP(("wayland_clipboard_contents_received() selection_data = %p\n",
+           selection_data));
   FastTrackClipboard* fastTrack = static_cast<FastTrackClipboard*>(data);
   fastTrack->mRetrievalContex->TransferFastTrackClipboard(
       fastTrack->mClipboardRequestNumber, selection_data);
@@ -851,24 +852,34 @@ static void wayland_clipboard_contents_r
 
 void nsRetrievalContextWayland::TransferFastTrackClipboard(
     int aClipboardRequestNumber, GtkSelectionData* aSelectionData) {
-  LOGCLIP(("nsRetrievalContextWayland::TransferFastTrackClipboard()\n"));
+  LOGCLIP(
+      ("nsRetrievalContextWayland::TransferFastTrackClipboard(), "
+       "aSelectionData = %p\n",
+       aSelectionData));
+
+  int dataLength = gtk_selection_data_get_length(aSelectionData);
+  if (dataLength < 0) {
+    LOGCLIP(
+        ("    gtk_clipboard_request_contents() failed to get clipboard "
+         "data!\n"));
+    ReleaseClipboardData(mClipboardData);
+    return;
+  }
 
   if (mClipboardRequestNumber == aClipboardRequestNumber) {
     LOGCLIP(("    request number matches\n"));
-    int dataLength = gtk_selection_data_get_length(aSelectionData);
-    if (!dataLength) {
-      LOGCLIP(
-          ("    gtk_selection_data_get_length() returned 0 data length!\n"));
-      return;
-    }
     LOGCLIP(("    fastracking %d bytes of data.\n", dataLength));
     mClipboardDataLength = dataLength;
-    mClipboardData = reinterpret_cast<char*>(
-        g_malloc(sizeof(char) * (mClipboardDataLength + 1)));
-    memcpy(mClipboardData, gtk_selection_data_get_data(aSelectionData),
-           sizeof(char) * mClipboardDataLength);
-    mClipboardData[mClipboardDataLength] = '\0';
-    LOGCLIP(("    done, mClipboardData = %p\n", mClipboardData));
+    if (dataLength > 0) {
+      mClipboardData = reinterpret_cast<char*>(
+          g_malloc(sizeof(char) * (mClipboardDataLength + 1)));
+      memcpy(mClipboardData, gtk_selection_data_get_data(aSelectionData),
+             sizeof(char) * mClipboardDataLength);
+      mClipboardData[mClipboardDataLength] = '\0';
+      LOGCLIP(("    done, mClipboardData = %p\n", mClipboardData));
+    } else {
+      ReleaseClipboardData(mClipboardData);
+    }
   } else {
     LOGCLIP(("    request number does not match!\n"));
     NS_WARNING("Received obsoleted clipboard data!");
@@ -952,11 +963,10 @@ void nsRetrievalContextWayland::ReleaseC
     const char* aClipboardData) {
   LOGCLIP(("nsRetrievalContextWayland::ReleaseClipboardData [%p]\n",
            aClipboardData));
-
-  NS_ASSERTION(aClipboardData == mClipboardData,
-               "Releasing unknown clipboard data!");
-  g_free((void*)aClipboardData);
-
+  if (aClipboardData != mClipboardData) {
+    NS_WARNING("Wayland clipboard: Releasing unknown clipboard data!");
+  }
+  g_free((void*)mClipboardData);
+  mClipboardDataLength = 0;
   mClipboardData = nullptr;
-  mClipboardDataLength = 0;
 }

bgstack15