From d524e4bf80314ade4a6aedf77b8b4adaa96bfcee Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Tue, 25 Apr 2017 09:43:52 -0700 Subject: Delete some CFX string ctors. Prevent implicit construction of CFX_WideString from non-wchar_t variables. Similarly prevent implicit construction of CFX_ByteString from non-char variables. Fix up CBC_OnedCodaBarWriter which tries to do the above, and simplify code there using pdfium::ContainsValue(). Same for CPDF_FileSpec. Change-Id: I3db7125a68ef3f64c2f235d38e974767cd083dc3 Reviewed-on: https://pdfium-review.googlesource.com/4478 Commit-Queue: Lei Zhang Reviewed-by: Tom Sepez --- core/fpdfdoc/cpdf_filespec.cpp | 43 +++++++++------------ core/fxcrt/cfx_bytestring.h | 4 ++ core/fxcrt/cfx_widestring.h | 4 ++ fxbarcode/oned/BC_OnedCodaBarWriter.cpp | 68 +++++++++++++-------------------- 4 files changed, 52 insertions(+), 67 deletions(-) diff --git a/core/fpdfdoc/cpdf_filespec.cpp b/core/fpdfdoc/cpdf_filespec.cpp index e5f026e573..086515b9a8 100644 --- a/core/fpdfdoc/cpdf_filespec.cpp +++ b/core/fpdfdoc/cpdf_filespec.cpp @@ -22,9 +22,9 @@ CFX_WideString ChangeSlashToPlatform(const wchar_t* str) { while (*str) { if (*str == '/') { #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ - result += ':'; + result += L':'; #else - result += '\\'; + result += L'\\'; #endif } else { result += *str; @@ -38,7 +38,7 @@ CFX_WideString ChangeSlashToPDF(const wchar_t* str) { CFX_WideString result; while (*str) { if (*str == '\\' || *str == ':') - result += '/'; + result += L'/'; else result += *str; @@ -60,19 +60,19 @@ CFX_WideString CPDF_FileSpec::DecodeFileName(const CFX_WideStringC& filepath) { return ChangeSlashToPlatform(filepath.c_str()); #elif _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ - if (filepath.GetAt(0) != '/') + if (filepath.GetAt(0) != L'/') return ChangeSlashToPlatform(filepath.c_str()); - if (filepath.GetAt(1) == '/') + if (filepath.GetAt(1) == L'/') return ChangeSlashToPlatform(filepath.c_str() + 1); - if (filepath.GetAt(2) == '/') { + if (filepath.GetAt(2) == L'/') { CFX_WideString result; result += filepath.GetAt(1); - result += ':'; + result += L':'; result += ChangeSlashToPlatform(filepath.c_str() + 2); return result; } CFX_WideString result; - result += '\\'; + result += L'\\'; result += ChangeSlashToPlatform(filepath.c_str()); return result; #else @@ -122,33 +122,24 @@ CFX_WideString CPDF_FileSpec::EncodeFileName(const CFX_WideStringC& filepath) { return CFX_WideString(); #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ - if (filepath.GetAt(1) == ':') { - CFX_WideString result; - result = '/'; + if (filepath.GetAt(1) == L':') { + CFX_WideString result(L'/'); result += filepath.GetAt(0); - if (filepath.GetAt(2) != '\\') - result += '/'; + if (filepath.GetAt(2) != L'\\') + result += L'/'; result += ChangeSlashToPDF(filepath.c_str() + 2); return result; } - if (filepath.GetAt(0) == '\\' && filepath.GetAt(1) == '\\') + if (filepath.GetAt(0) == L'\\' && filepath.GetAt(1) == L'\\') return ChangeSlashToPDF(filepath.c_str() + 1); - if (filepath.GetAt(0) == '\\') { - CFX_WideString result; - result = '/'; - result += ChangeSlashToPDF(filepath.c_str()); - return result; - } + if (filepath.GetAt(0) == L'\\') + return L'/' + ChangeSlashToPDF(filepath.c_str()); return ChangeSlashToPDF(filepath.c_str()); #elif _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ - if (filepath.Left(sizeof("Mac") - 1) == L"Mac") { - CFX_WideString result; - result = '/'; - result += ChangeSlashToPDF(filepath.c_str()); - return result; - } + if (filepath.Left(sizeof("Mac") - 1) == L"Mac") + return L'/' + ChangeSlashToPDF(filepath.c_str()); return ChangeSlashToPDF(filepath.c_str()); #else return CFX_WideString(filepath); diff --git a/core/fxcrt/cfx_bytestring.h b/core/fxcrt/cfx_bytestring.h index 0ffe92936a..c4d48954ba 100644 --- a/core/fxcrt/cfx_bytestring.h +++ b/core/fxcrt/cfx_bytestring.h @@ -35,6 +35,10 @@ class CFX_ByteString { // NOLINTNEXTLINE(runtime/explicit) CFX_ByteString(const char* ptr); + // No implicit conversions from wide strings. + // NOLINTNEXTLINE(runtime/explicit) + CFX_ByteString(wchar_t) = delete; + CFX_ByteString(const char* ptr, FX_STRSIZE len); CFX_ByteString(const uint8_t* ptr, FX_STRSIZE len); diff --git a/core/fxcrt/cfx_widestring.h b/core/fxcrt/cfx_widestring.h index fd3c92a15e..977114816d 100644 --- a/core/fxcrt/cfx_widestring.h +++ b/core/fxcrt/cfx_widestring.h @@ -35,6 +35,10 @@ class CFX_WideString { // NOLINTNEXTLINE(runtime/explicit) CFX_WideString(const wchar_t* ptr); + // No implicit conversions from byte strings. + // NOLINTNEXTLINE(runtime/explicit) + CFX_WideString(char) = delete; + CFX_WideString(const wchar_t* ptr, FX_STRSIZE len); explicit CFX_WideString(const CFX_WideStringC& str); diff --git a/fxbarcode/oned/BC_OnedCodaBarWriter.cpp b/fxbarcode/oned/BC_OnedCodaBarWriter.cpp index 029a6ec37c..14ffcdfc35 100644 --- a/fxbarcode/oned/BC_OnedCodaBarWriter.cpp +++ b/fxbarcode/oned/BC_OnedCodaBarWriter.cpp @@ -20,11 +20,13 @@ * limitations under the License. */ +#include "fxbarcode/oned/BC_OnedCodaBarWriter.h" + #include "fxbarcode/BC_Writer.h" #include "fxbarcode/common/BC_CommonBitArray.h" #include "fxbarcode/common/BC_CommonBitMatrix.h" #include "fxbarcode/oned/BC_OneDimWriter.h" -#include "fxbarcode/oned/BC_OnedCodaBarWriter.h" +#include "third_party/base/stl_util.h" namespace { @@ -42,34 +44,31 @@ const char CONTENT_CHARS[] = {'0', '1', '2', '3', '4', '5', '6', '7', } // namespace -CBC_OnedCodaBarWriter::CBC_OnedCodaBarWriter() { - m_chStart = 'A'; - m_chEnd = 'B'; - m_iWideNarrRatio = 2; -} +CBC_OnedCodaBarWriter::CBC_OnedCodaBarWriter() + : m_chStart('A'), m_chEnd('B'), m_iWideNarrRatio(2) {} + CBC_OnedCodaBarWriter::~CBC_OnedCodaBarWriter() {} + bool CBC_OnedCodaBarWriter::SetStartChar(char start) { - for (size_t i = 0; i < FX_ArraySize(START_END_CHARS); ++i) { - if (START_END_CHARS[i] == start) { - m_chStart = start; - return true; - } - } - return false; + if (!pdfium::ContainsValue(START_END_CHARS, start)) + return false; + + m_chStart = start; + return true; } bool CBC_OnedCodaBarWriter::SetEndChar(char end) { - for (size_t i = 0; i < FX_ArraySize(START_END_CHARS); ++i) { - if (START_END_CHARS[i] == end) { - m_chEnd = end; - return true; - } - } - return false; + if (!pdfium::ContainsValue(START_END_CHARS, end)) + return false; + + m_chEnd = end; + return true; } + void CBC_OnedCodaBarWriter::SetDataLength(int32_t length) { m_iDataLenth = length + 2; } + bool CBC_OnedCodaBarWriter::SetTextLocation(BC_TEXT_LOC location) { if (location < BC_TEXT_LOC_NONE || location > BC_TEXT_LOC_BELOWEMBED) { return false; @@ -87,27 +86,14 @@ bool CBC_OnedCodaBarWriter::SetWideNarrowRatio(int8_t ratio) { } bool CBC_OnedCodaBarWriter::FindChar(wchar_t ch, bool isContent) { - if (isContent) { - for (size_t i = 0; i < FX_ArraySize(CONTENT_CHARS); ++i) { - if (ch == (wchar_t)CONTENT_CHARS[i]) { - return true; - } - } - for (size_t j = 0; j < FX_ArraySize(START_END_CHARS); ++j) { - if (ch == (wchar_t)START_END_CHARS[j]) { - return true; - } - } - return false; - } else { - for (size_t i = 0; i < FX_ArraySize(CONTENT_CHARS); ++i) { - if (ch == (wchar_t)CONTENT_CHARS[i]) { - return true; - } - } + if (ch > 0x7F) return false; - } + + char narrow_ch = static_cast(ch); + return pdfium::ContainsValue(CONTENT_CHARS, narrow_ch) || + (isContent && pdfium::ContainsValue(START_END_CHARS, narrow_ch)); } + bool CBC_OnedCodaBarWriter::CheckContentValidity( const CFX_WideStringC& contents) { return std::all_of( @@ -204,8 +190,8 @@ uint8_t* CBC_OnedCodaBarWriter::EncodeImpl(const CFX_ByteString& contents, CFX_WideString CBC_OnedCodaBarWriter::encodedContents( const CFX_WideStringC& contents) { - CFX_WideString strStart(m_chStart); - CFX_WideString strEnd(m_chEnd); + CFX_WideString strStart(static_cast(m_chStart)); + CFX_WideString strEnd(static_cast(m_chEnd)); return strStart + contents + strEnd; } -- cgit v1.2.3