Commit 7223839e authored by Dave Tapuska's avatar Dave Tapuska Committed by Commit Bot

Silence 64 to 32 bit truncation errors on WTF strings.

strlen returns size_t which can be 64 bits yet all of the WTF strings
use 32 storage lengths. Add a helper function string_unsigned which
returns the strlen with a SafeCast to unsigned. Place it in an
implementation file to avoid increase in binary size due to inlining.

BUG=588506

Change-Id: I3b5025c1fee9b1eda9cd017af6bdfc66d808e802
Reviewed-on: https://chromium-review.googlesource.com/1096133Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Reviewed-by: default avatarYuta Kitamura <yutak@chromium.org>
Commit-Queue: Dave Tapuska <dtapuska@chromium.org>
Cr-Commit-Position: refs/heads/master@{#566415}
parent 4b17e6ae
...@@ -125,6 +125,7 @@ jumbo_component("wtf") { ...@@ -125,6 +125,7 @@ jumbo_component("wtf") {
"stack_util.h", "stack_util.h",
"static_constructors.h", "static_constructors.h",
"std_lib_extras.h", "std_lib_extras.h",
"string_extras.cc",
"string_extras.h", "string_extras.h",
"string_hasher.h", "string_hasher.h",
"terminated_array.h", "terminated_array.h",
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/platform/wtf/string_extras.h"
#include "third_party/blink/renderer/platform/wtf/std_lib_extras.h"
namespace WTF {
unsigned strlen_unsigned(const char* string) {
return SafeCast<unsigned>(strlen(string));
}
} // namespace WTF
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
#include <string.h> #include <string.h>
#include "build/build_config.h" #include "build/build_config.h"
#include "third_party/blink/renderer/platform/wtf/wtf_export.h"
#if defined(OS_POSIX) #if defined(OS_POSIX)
#include <strings.h> #include <strings.h>
...@@ -46,4 +47,11 @@ inline int strcasecmp(const char* s1, const char* s2) { ...@@ -46,4 +47,11 @@ inline int strcasecmp(const char* s1, const char* s2) {
#endif // defined(COMPILER_MSVC) #endif // defined(COMPILER_MSVC)
namespace WTF {
// Wrapper around strlen with a SafeCast to unsigned from size_t.
WTF_EXPORT unsigned strlen_unsigned(const char* string);
} // namespace WTF
#endif // THIRD_PARTY_BLINK_RENDERER_PLATFORM_WTF_STRING_EXTRAS_H_ #endif // THIRD_PARTY_BLINK_RENDERER_PLATFORM_WTF_STRING_EXTRAS_H_
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#include "build/build_config.h" #include "build/build_config.h"
#include "third_party/blink/renderer/platform/wtf/allocator.h" #include "third_party/blink/renderer/platform/wtf/allocator.h"
#include "third_party/blink/renderer/platform/wtf/hash_table_deleted_value_type.h" #include "third_party/blink/renderer/platform/wtf/hash_table_deleted_value_type.h"
#include "third_party/blink/renderer/platform/wtf/string_extras.h"
#include "third_party/blink/renderer/platform/wtf/text/cstring.h" #include "third_party/blink/renderer/platform/wtf/text/cstring.h"
#include "third_party/blink/renderer/platform/wtf/text/string_view.h" #include "third_party/blink/renderer/platform/wtf/text/string_view.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h" #include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
...@@ -58,8 +59,9 @@ class WTF_EXPORT AtomicString { ...@@ -58,8 +59,9 @@ class WTF_EXPORT AtomicString {
AtomicString() = default; AtomicString() = default;
AtomicString(const LChar* chars) AtomicString(const LChar* chars)
: AtomicString(chars, : AtomicString(
chars ? strlen(reinterpret_cast<const char*>(chars)) : 0) { chars,
chars ? strlen_unsigned(reinterpret_cast<const char*>(chars)) : 0) {
} }
AtomicString(const char* chars) AtomicString(const char* chars)
: AtomicString(reinterpret_cast<const LChar*>(chars)) {} : AtomicString(reinterpret_cast<const LChar*>(chars)) {}
......
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
#include <string.h> #include <string.h>
#include "third_party/blink/renderer/platform/wtf/allocator.h" #include "third_party/blink/renderer/platform/wtf/allocator.h"
#include "third_party/blink/renderer/platform/wtf/string_extras.h"
#ifndef WTFString_h #ifndef WTFString_h
#include "third_party/blink/renderer/platform/wtf/text/atomic_string.h" #include "third_party/blink/renderer/platform/wtf/text/atomic_string.h"
...@@ -101,7 +102,7 @@ class WTF_EXPORT StringTypeAdapter<char*> { ...@@ -101,7 +102,7 @@ class WTF_EXPORT StringTypeAdapter<char*> {
public: public:
explicit StringTypeAdapter<char*>(char* buffer) explicit StringTypeAdapter<char*>(char* buffer)
: buffer_(buffer), length_(strlen(buffer)) {} : buffer_(buffer), length_(strlen_unsigned(buffer)) {}
unsigned length() const { return length_; } unsigned length() const { return length_; }
bool Is8Bit() const { return true; } bool Is8Bit() const { return true; }
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include "third_party/blink/renderer/platform/wtf/allocator.h" #include "third_party/blink/renderer/platform/wtf/allocator.h"
#include "third_party/blink/renderer/platform/wtf/hash_traits.h" #include "third_party/blink/renderer/platform/wtf/hash_traits.h"
#include "third_party/blink/renderer/platform/wtf/string_extras.h"
#include "third_party/blink/renderer/platform/wtf/string_hasher.h" #include "third_party/blink/renderer/platform/wtf/string_hasher.h"
#include "third_party/blink/renderer/platform/wtf/text/atomic_string.h" #include "third_party/blink/renderer/platform/wtf/text/atomic_string.h"
...@@ -102,7 +103,7 @@ class CaseFoldingHash { ...@@ -102,7 +103,7 @@ class CaseFoldingHash {
static inline unsigned GetHash(const char* data) { static inline unsigned GetHash(const char* data) {
return CaseFoldingHash::GetHash(reinterpret_cast<const LChar*>(data), return CaseFoldingHash::GetHash(reinterpret_cast<const LChar*>(data),
strlen(data)); strlen_unsigned(data));
} }
static inline bool Equal(const StringImpl* a, const StringImpl* b) { static inline bool Equal(const StringImpl* a, const StringImpl* b) {
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include "base/memory/scoped_refptr.h" #include "base/memory/scoped_refptr.h"
#endif #endif
#include <cstring> #include <cstring>
#include "third_party/blink/renderer/platform/wtf/string_extras.h"
#include "third_party/blink/renderer/platform/wtf/text/string_impl.h" #include "third_party/blink/renderer/platform/wtf/text/string_impl.h"
#include "third_party/blink/renderer/platform/wtf/text/unicode.h" #include "third_party/blink/renderer/platform/wtf/text/unicode.h"
...@@ -73,7 +74,8 @@ class WTF_EXPORT StringView { ...@@ -73,7 +74,8 @@ class WTF_EXPORT StringView {
: StringView(reinterpret_cast<const LChar*>(chars), length) {} : StringView(reinterpret_cast<const LChar*>(chars), length) {}
StringView(const LChar* chars) StringView(const LChar* chars)
: StringView(chars, : StringView(chars,
chars ? strlen(reinterpret_cast<const char*>(chars)) : 0) {} chars ? strlen_unsigned(reinterpret_cast<const char*>(chars))
: 0) {}
StringView(const char* chars) StringView(const char* chars)
: StringView(reinterpret_cast<const LChar*>(chars)) {} : StringView(reinterpret_cast<const LChar*>(chars)) {}
......
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
#include "third_party/blink/renderer/platform/wtf/allocator.h" #include "third_party/blink/renderer/platform/wtf/allocator.h"
#include "third_party/blink/renderer/platform/wtf/compiler.h" #include "third_party/blink/renderer/platform/wtf/compiler.h"
#include "third_party/blink/renderer/platform/wtf/hash_table_deleted_value_type.h" #include "third_party/blink/renderer/platform/wtf/hash_table_deleted_value_type.h"
#include "third_party/blink/renderer/platform/wtf/string_extras.h"
#include "third_party/blink/renderer/platform/wtf/text/ascii_fast_path.h" #include "third_party/blink/renderer/platform/wtf/text/ascii_fast_path.h"
#include "third_party/blink/renderer/platform/wtf/text/string_impl.h" #include "third_party/blink/renderer/platform/wtf/text/string_impl.h"
#include "third_party/blink/renderer/platform/wtf/text/string_view.h" #include "third_party/blink/renderer/platform/wtf/text/string_view.h"
...@@ -92,7 +93,7 @@ class WTF_EXPORT String { ...@@ -92,7 +93,7 @@ class WTF_EXPORT String {
String(const LChar* characters) String(const LChar* characters)
: String(reinterpret_cast<const char*>(characters)) {} : String(reinterpret_cast<const char*>(characters)) {}
String(const char* characters) String(const char* characters)
: String(characters, characters ? strlen(characters) : 0) {} : String(characters, characters ? strlen_unsigned(characters) : 0) {}
// Construct a string referencing an existing StringImpl. // Construct a string referencing an existing StringImpl.
String(StringImpl* impl) : impl_(impl) {} String(StringImpl* impl) : impl_(impl) {}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment