String#utf8() allocates new CString
authorutatane.tea@gmail.com <utatane.tea@gmail.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 4 Sep 2017 01:38:09 +0000 (01:38 +0000)
committerutatane.tea@gmail.com <utatane.tea@gmail.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 4 Sep 2017 01:38:09 +0000 (01:38 +0000)
https://bugs.webkit.org/show_bug.cgi?id=176302

Reviewed by Sam Weinig.

Source/WebCore:

Several places uses String#utf8() like an accessor. In reality, it allocates new CString per call.
It is very costly. Furthermore, some places uses this incorrectly. For example,
`std::vector<char>(srtpAuth.utf8().data(), srtpAuth.utf8().data() + srtpAuth.utf8().length());`
is incorrect since each time strpAuth.utf8() allocates different CString.

This patch calls utf8() first and use it.

* Modules/websockets/WebSocketChannel.cpp:
(WebCore::WebSocketChannel::startClosingHandshake):
* platform/wpe/PlatformPasteboardWPE.cpp:
(WebCore::PlatformPasteboard::write):

Source/WebKit:

* NetworkProcess/webrtc/NetworkRTCSocket.cpp:
(WebKit::NetworkRTCSocket::sendTo):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@221562 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/Modules/websockets/WebSocketChannel.cpp
Source/WebCore/platform/wpe/PlatformPasteboardWPE.cpp
Source/WebKit/ChangeLog
Source/WebKit/NetworkProcess/webrtc/NetworkRTCSocket.cpp

index a0be3c9..4377ae7 100644 (file)
@@ -1,5 +1,24 @@
 2017-09-03  Yusuke Suzuki  <utatane.tea@gmail.com>
 
 2017-09-03  Yusuke Suzuki  <utatane.tea@gmail.com>
 
+        String#utf8() allocates new CString
+        https://bugs.webkit.org/show_bug.cgi?id=176302
+
+        Reviewed by Sam Weinig.
+
+        Several places uses String#utf8() like an accessor. In reality, it allocates new CString per call.
+        It is very costly. Furthermore, some places uses this incorrectly. For example,
+        `std::vector<char>(srtpAuth.utf8().data(), srtpAuth.utf8().data() + srtpAuth.utf8().length());`
+        is incorrect since each time strpAuth.utf8() allocates different CString.
+
+        This patch calls utf8() first and use it.
+
+        * Modules/websockets/WebSocketChannel.cpp:
+        (WebCore::WebSocketChannel::startClosingHandshake):
+        * platform/wpe/PlatformPasteboardWPE.cpp:
+        (WebCore::PlatformPasteboard::write):
+
+2017-09-03  Yusuke Suzuki  <utatane.tea@gmail.com>
+
         [SOUP] Use fastMalloced SoupBuffer in ResourcehandleSoup
         https://bugs.webkit.org/show_bug.cgi?id=176311
 
         [SOUP] Use fastMalloced SoupBuffer in ResourcehandleSoup
         https://bugs.webkit.org/show_bug.cgi?id=176311
 
index 0cc9e2b..39f3100 100644 (file)
@@ -492,7 +492,8 @@ void WebSocketChannel::startClosingHandshake(int code, const String& reason)
         unsigned char lowByte = code;
         buf.append(static_cast<char>(highByte));
         buf.append(static_cast<char>(lowByte));
         unsigned char lowByte = code;
         buf.append(static_cast<char>(highByte));
         buf.append(static_cast<char>(lowByte));
-        buf.append(reason.utf8().data(), reason.utf8().length());
+        auto reasonUTF8 = reason.utf8();
+        buf.append(reasonUTF8.data(), reasonUTF8.length());
     }
     enqueueRawFrame(WebSocketFrame::OpCodeClose, buf.data(), buf.size());
     Ref<WebSocketChannel> protectedThis(*this); // An attempt to send closing handshake may fail, which will get the channel closed and dereferenced.
     }
     enqueueRawFrame(WebSocketFrame::OpCodeClose, buf.data(), buf.size());
     Ref<WebSocketChannel> protectedThis(*this); // An attempt to send closing handshake may fail, which will get the channel closed and dereferenced.
index 5ffb8c3..449c367 100644 (file)
@@ -103,8 +103,11 @@ void PlatformPasteboard::write(const String& type, const String& string)
     struct wpe_pasteboard_string_pair pairs[] = {
         { { nullptr, 0 }, { nullptr, 0 } },
     };
     struct wpe_pasteboard_string_pair pairs[] = {
         { { nullptr, 0 }, { nullptr, 0 } },
     };
-    wpe_pasteboard_string_initialize(&pairs[0].type, type.utf8().data(), type.utf8().length());
-    wpe_pasteboard_string_initialize(&pairs[0].string, string.utf8().data(), string.utf8().length());
+
+    auto typeUTF8 = type.utf8();
+    auto stringUTF8 = string.utf8();
+    wpe_pasteboard_string_initialize(&pairs[0].type, typeUTF8.data(), typeUTF8.length());
+    wpe_pasteboard_string_initialize(&pairs[0].string, stringUTF8.data(), stringUTF8.length());
     struct wpe_pasteboard_string_map map = { pairs, 1 };
 
     wpe_pasteboard_write(m_pasteboard, &map);
     struct wpe_pasteboard_string_map map = { pairs, 1 };
 
     wpe_pasteboard_write(m_pasteboard, &map);
index acca1f3..ec2548d 100644 (file)
@@ -1,3 +1,13 @@
+2017-09-03  Yusuke Suzuki  <utatane.tea@gmail.com>
+
+        String#utf8() allocates new CString
+        https://bugs.webkit.org/show_bug.cgi?id=176302
+
+        Reviewed by Sam Weinig.
+
+        * NetworkProcess/webrtc/NetworkRTCSocket.cpp:
+        (WebKit::NetworkRTCSocket::sendTo):
+
 2017-09-03  Tim Horton  <timothy_horton@apple.com>
 
         Move shared part of the Remote Layer Tree code out of a Mac-specific directory
 2017-09-03  Tim Horton  <timothy_horton@apple.com>
 
         Move shared part of the Remote Layer Tree code out of a Mac-specific directory
index fcd1249..e3c99bb 100644 (file)
@@ -51,9 +51,10 @@ void NetworkRTCSocket::sendTo(const IPC::DataReference& data, const RTCNetwork::
     options.packet_time_params.rtp_sendtime_extension_id = rtpSendtimeExtensionID;
     options.packet_time_params.srtp_packet_index = srtpPacketIndex;
     options.dscp = static_cast<rtc::DiffServCodePoint>(dscp);
     options.packet_time_params.rtp_sendtime_extension_id = rtpSendtimeExtensionID;
     options.packet_time_params.srtp_packet_index = srtpPacketIndex;
     options.dscp = static_cast<rtc::DiffServCodePoint>(dscp);
-    if (srtpAuth.utf8().length()) {
-        options.packet_time_params.srtp_auth_key = std::vector<char>(srtpAuth.utf8().data(), srtpAuth.utf8().data() + srtpAuth.utf8().length());
-        options.packet_time_params.srtp_auth_tag_len = srtpAuth.utf8().length();
+    auto srtpAuthUTF8 = srtpAuth.utf8();
+    if (srtpAuthUTF8.length()) {
+        options.packet_time_params.srtp_auth_key = std::vector<char>(srtpAuthUTF8.data(), srtpAuthUTF8.data() + srtpAuthUTF8.length());
+        options.packet_time_params.srtp_auth_tag_len = srtpAuthUTF8.length();
     } else
         options.packet_time_params.srtp_auth_tag_len = -1;
     
     } else
         options.packet_time_params.srtp_auth_tag_len = -1;