From 746c28772e01675096800d8853aae33f94ed3d55 Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Fri, 7 Apr 2017 16:35:13 -0700 Subject: Create initializer-list ctor for strings. Also use safe arithmetic for two-arg ctor. Change-Id: I5d541d9b2d5fe5b939f4cc8c22cf034f5cb01176 Reviewed-on: https://pdfium-review.googlesource.com/3955 Reviewed-by: Lei Zhang Commit-Queue: Tom Sepez --- core/fxcrt/cfx_bytestring.cpp | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'core/fxcrt/cfx_bytestring.cpp') diff --git a/core/fxcrt/cfx_bytestring.cpp b/core/fxcrt/cfx_bytestring.cpp index cca8ad6be4..ab35e5d0c5 100644 --- a/core/fxcrt/cfx_bytestring.cpp +++ b/core/fxcrt/cfx_bytestring.cpp @@ -13,6 +13,7 @@ #include "core/fxcrt/cfx_string_pool_template.h" #include "core/fxcrt/fx_basic.h" +#include "core/fxcrt/fx_safe_types.h" #include "third_party/base/numerics/safe_math.h" #include "third_party/base/stl_util.h" @@ -124,7 +125,10 @@ CFX_ByteString::CFX_ByteString(const CFX_ByteStringC& stringSrc) { CFX_ByteString::CFX_ByteString(const CFX_ByteStringC& str1, const CFX_ByteStringC& str2) { - int nNewLen = str1.GetLength() + str2.GetLength(); + FX_SAFE_STRSIZE nSafeLen = str1.GetLength(); + nSafeLen += str2.GetLength(); + + FX_STRSIZE nNewLen = nSafeLen.ValueOrDie(); if (nNewLen == 0) return; @@ -133,6 +137,25 @@ CFX_ByteString::CFX_ByteString(const CFX_ByteStringC& str1, m_pData->CopyContentsAt(str1.GetLength(), str2.c_str(), str2.GetLength()); } +CFX_ByteString::CFX_ByteString( + const std::initializer_list& list) { + FX_SAFE_STRSIZE nSafeLen = 0; + for (const auto& item : list) + nSafeLen += item.GetLength(); + + FX_STRSIZE nNewLen = nSafeLen.ValueOrDie(); + if (nNewLen == 0) + return; + + m_pData.Reset(StringData::Create(nNewLen)); + + FX_STRSIZE nOffset = 0; + for (const auto& item : list) { + m_pData->CopyContentsAt(nOffset, item.c_str(), item.GetLength()); + nOffset += item.GetLength(); + } +} + CFX_ByteString::~CFX_ByteString() {} const CFX_ByteString& CFX_ByteString::operator=(const char* pStr) { -- cgit v1.2.3