From c1b36669db55a17ab7049bd2ea68db27653c097c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Bostr=C3=B6m?= Date: Thu, 23 May 2019 17:34:59 +0000 Subject: [PATCH] Revert "Delete STACK_ARRAY macro, and use of alloca" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 74b373f04a69b279e45b0792d86c594cb33d22c1. Reason for revert: This breaks chromium, blocking webrtc from rolling. ... In file included from ../../third_party/webrtc\rtc_base/strings/string_builder.h:23: ../../third_party/webrtc\rtc_base/string_utils.h(54,28): error: implicit conversion loses integer precision: 'std::__1::basic_string, std::__1::allocator >::size_type' (aka 'unsigned long long') to 'int' [-Werror,-Wshorten-64-to-32] ws.size()); See https://logs.chromium.org/logs/chromium/buildbucket/cr-buildbucket.appspot.com/8912652299012991936/+/steps/compile__with_patch_/0/stdout Original change's description: > Delete STACK_ARRAY macro, and use of alloca > > Refactor the few uses of STACK_ARRAY to avoid an extra copy > on the stack. > > Bug: webrtc:6424 > Change-Id: I5c8f3c0381523db0ead31207d949df9a286c76ba > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/137806 > Commit-Queue: Niels Moller > Reviewed-by: Karl Wiberg > Cr-Commit-Position: refs/heads/master@{#28038} TBR=kwiberg@webrtc.org,nisse@webrtc.org Change-Id: I223fceab60855dde363cc9ce8375e8f5cca43c02 No-Presubmit: true No-Tree-Checks: true No-Try: true Bug: webrtc:6424 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/138209 Reviewed-by: Henrik Boström Commit-Queue: Henrik Boström Cr-Commit-Position: refs/heads/master@{#28043} --- rtc_base/string_encode.cc | 41 ++++++++++++++++++++++++--------------- rtc_base/string_utils.h | 31 +++++++++++++++++++++-------- 2 files changed, 48 insertions(+), 24 deletions(-) diff --git a/rtc_base/string_encode.cc b/rtc_base/string_encode.cc index da31ac08b3..7b2eda7051 100644 --- a/rtc_base/string_encode.cc +++ b/rtc_base/string_encode.cc @@ -14,6 +14,7 @@ #include "rtc_base/arraysize.h" #include "rtc_base/checks.h" +#include "rtc_base/string_utils.h" namespace rtc { @@ -44,22 +45,25 @@ bool hex_decode(char ch, unsigned char* val) { return true; } -size_t hex_encode_output_length(size_t srclen, char delimiter) { - return delimiter && srclen > 0 ? (srclen * 3 - 1) : (srclen * 2); -} - -// hex_encode shows the hex representation of binary data in ascii, with -// |delimiter| between bytes, or none if |delimiter| == 0. -void hex_encode_with_delimiter(char* buffer, - const char* csource, - size_t srclen, - char delimiter) { - RTC_DCHECK(buffer); +// hex_encode, but separate each byte representation with a delimiter. +// |delimiter| == 0 means no delimiter +// If the buffer is too short, we return 0 +size_t hex_encode_with_delimiter(char* buffer, + size_t buflen, + const char* csource, + size_t srclen, + char delimiter) { + RTC_DCHECK(buffer); // TODO(kwiberg): estimate output size + if (buflen == 0) + return 0; // Init and check bounds. const unsigned char* bsource = reinterpret_cast(csource); size_t srcpos = 0, bufpos = 0; + size_t needed = delimiter ? (srclen * 3) : (srclen * 2 + 1); + if (buflen < needed) + return 0; while (srcpos < srclen) { unsigned char ch = bsource[srcpos++]; @@ -73,6 +77,10 @@ void hex_encode_with_delimiter(char* buffer, ++bufpos; } } + + // Null terminate. + buffer[bufpos] = '\0'; + return bufpos; } } // namespace @@ -88,11 +96,12 @@ std::string hex_encode(const char* source, size_t srclen) { std::string hex_encode_with_delimiter(const char* source, size_t srclen, char delimiter) { - std::string s(hex_encode_output_length(srclen, delimiter), 0); - // TODO(nisse): When we can use C++17, switch the below hack with begin to - // just s.data(). - hex_encode_with_delimiter(&*s.begin(), source, srclen, delimiter); - return s; + const size_t kBufferSize = srclen * 3; + char* buffer = STACK_ARRAY(char, kBufferSize); + size_t length = + hex_encode_with_delimiter(buffer, kBufferSize, source, srclen, delimiter); + RTC_DCHECK(srclen == 0 || length > 0); + return std::string(buffer, length); } size_t hex_decode(char* cbuffer, diff --git a/rtc_base/string_utils.h b/rtc_base/string_utils.h index 5b5d33c148..bc332846a4 100644 --- a/rtc_base/string_utils.h +++ b/rtc_base/string_utils.h @@ -21,15 +21,31 @@ #include #include +#define alloca _alloca #endif // WEBRTC_WIN #if defined(WEBRTC_POSIX) +#ifdef BSD #include +#else // BSD +#include +#endif // !BSD #include #endif // WEBRTC_POSIX #include +/////////////////////////////////////////////////////////////////////////////// +// Generic string/memory utilities +/////////////////////////////////////////////////////////////////////////////// + +#define STACK_ARRAY(TYPE, LEN) \ + static_cast(::alloca((LEN) * sizeof(TYPE))) + +/////////////////////////////////////////////////////////////////////////////// +// Traits simplifies porting string functions to be CTYPE-agnostic +/////////////////////////////////////////////////////////////////////////////// + namespace rtc { const size_t SIZE_UNKNOWN = static_cast(-1); @@ -49,10 +65,9 @@ size_t strcpyn(char* buffer, inline std::wstring ToUtf16(const char* utf8, size_t len) { int len16 = ::MultiByteToWideChar(CP_UTF8, 0, utf8, static_cast(len), nullptr, 0); - std::wstring ws(len16, 0); - ::MultiByteToWideChar(CP_UTF8, 0, utf8, static_cast(len), &*ws.begin(), - ws.size()); - return ws; + wchar_t* ws = STACK_ARRAY(wchar_t, len16); + ::MultiByteToWideChar(CP_UTF8, 0, utf8, static_cast(len), ws, len16); + return std::wstring(ws, len16); } inline std::wstring ToUtf16(const std::string& str) { @@ -62,10 +77,10 @@ inline std::wstring ToUtf16(const std::string& str) { inline std::string ToUtf8(const wchar_t* wide, size_t len) { int len8 = ::WideCharToMultiByte(CP_UTF8, 0, wide, static_cast(len), nullptr, 0, nullptr, nullptr); - std::string ns(len8, 0); - ::WideCharToMultiByte(CP_UTF8, 0, wide, static_cast(len), &*ns.begin(), - ns.size(), nullptr, nullptr); - return ns; + char* ns = STACK_ARRAY(char, len8); + ::WideCharToMultiByte(CP_UTF8, 0, wide, static_cast(len), ns, len8, + nullptr, nullptr); + return std::string(ns, len8); } inline std::string ToUtf8(const wchar_t* wide) {