From a6adac83c6aadbacf2a338d2e55148f7d3417762 Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Thu, 2 Nov 2017 19:27:08 +0000 Subject: Remove some C-style const char* casts. Change-Id: I4785dd277b9da072ee3c55e2aaeb688bbf02852e Reviewed-on: https://pdfium-review.googlesource.com/17391 Reviewed-by: dsinclair Commit-Queue: Lei Zhang --- .../fpdfapi/parser/cpdf_simple_parser_unittest.cpp | 12 +++++---- core/fpdfapi/parser/cpdf_syntax_parser.cpp | 7 ++--- .../parser/fpdf_parser_decode_embeddertest.cpp | 30 ++++++++++++---------- core/fpdfdoc/cpdf_filespec_unittest.cpp | 25 ++++++++++-------- fpdfsdk/fpdf_flatten.cpp | 3 +-- fpdfsdk/fpdfview.cpp | 5 ++-- fxjs/cjs_globaldata.cpp | 4 +-- testing/test_support.h | 29 +++++++++++---------- 8 files changed, 62 insertions(+), 53 deletions(-) diff --git a/core/fpdfapi/parser/cpdf_simple_parser_unittest.cpp b/core/fpdfapi/parser/cpdf_simple_parser_unittest.cpp index a2f43386e7..e8d3b7142a 100644 --- a/core/fpdfapi/parser/cpdf_simple_parser_unittest.cpp +++ b/core/fpdfapi/parser/cpdf_simple_parser_unittest.cpp @@ -11,7 +11,7 @@ #include "testing/test_support.h" TEST(SimpleParserTest, GetWord) { - pdfium::StrFuncTestData test_data[] = { + static const pdfium::StrFuncTestData test_data[] = { // Empty src string. STR_IN_OUT_CASE("", ""), // Content with whitespaces only. @@ -51,15 +51,17 @@ TEST(SimpleParserTest, GetWord) { const pdfium::StrFuncTestData& data = test_data[i]; CPDF_SimpleParser parser(data.input, data.input_size); ByteStringView word = parser.GetWord(); - EXPECT_EQ(std::string(reinterpret_cast(data.expected), - data.expected_size), - std::string(word.unterminated_c_str(), word.GetLength())) + EXPECT_EQ(data.expected_size, word.GetLength()) << " for case " << i; + if (data.expected_size != word.GetLength()) + continue; + EXPECT_EQ( + 0, memcmp(data.expected, word.unterminated_c_str(), data.expected_size)) << " for case " << i; } } TEST(SimpleParserTest, FindTagParamFromStart) { - struct FindTagTestStruct { + static const struct FindTagTestStruct { const unsigned char* input; unsigned int input_size; const char* token; diff --git a/core/fpdfapi/parser/cpdf_syntax_parser.cpp b/core/fpdfapi/parser/cpdf_syntax_parser.cpp index f0ea5ac5b0..ddf420b34a 100644 --- a/core/fpdfapi/parser/cpdf_syntax_parser.cpp +++ b/core/fpdfapi/parser/cpdf_syntax_parser.cpp @@ -337,9 +337,10 @@ void CPDF_SyntaxParser::ToNextWord() { ByteString CPDF_SyntaxParser::GetNextWord(bool* bIsNumber) { const CPDF_ReadValidator::Session read_session(GetValidator().Get()); GetNextWordInternal(bIsNumber); - return GetValidator()->has_read_problems() - ? ByteString() - : ByteString((const char*)m_WordBuffer, m_WordSize); + ByteString ret; + if (!GetValidator()->has_read_problems()) + ret = ByteString(m_WordBuffer, m_WordSize); + return ret; } ByteString CPDF_SyntaxParser::PeekNextWord(bool* bIsNumber) { diff --git a/core/fpdfapi/parser/fpdf_parser_decode_embeddertest.cpp b/core/fpdfapi/parser/fpdf_parser_decode_embeddertest.cpp index bf2fd6d10f..e95a4cc595 100644 --- a/core/fpdfapi/parser/fpdf_parser_decode_embeddertest.cpp +++ b/core/fpdfapi/parser/fpdf_parser_decode_embeddertest.cpp @@ -11,13 +11,13 @@ #include "testing/gtest/include/gtest/gtest.h" #include "testing/test_support.h" -class FPDFParserDecodeEmbeddertest : public EmbedderTest {}; +using FPDFParserDecodeEmbeddertest = EmbedderTest; // NOTE: python's zlib.compress() and zlib.decompress() may be useful for // external validation of the FlateEncode/FlateDecode test cases. TEST_F(FPDFParserDecodeEmbeddertest, FlateEncode) { - pdfium::StrFuncTestData flate_encode_cases[] = { + static const pdfium::StrFuncTestData flate_encode_cases[] = { STR_IN_OUT_CASE("", "\x78\x9c\x03\x00\x00\x00\x00\x01"), STR_IN_OUT_CASE(" ", "\x78\x9c\x53\x00\x00\x00\x21\x00\x21"), STR_IN_OUT_CASE("123", "\x78\x9c\x33\x34\x32\x06\x00\01\x2d\x00\x97"), @@ -37,18 +37,20 @@ TEST_F(FPDFParserDecodeEmbeddertest, FlateEncode) { for (size_t i = 0; i < FX_ArraySize(flate_encode_cases); ++i) { const pdfium::StrFuncTestData& data = flate_encode_cases[i]; unsigned char* buf = nullptr; - unsigned int buf_size; + uint32_t buf_size; EXPECT_TRUE(FlateEncode(data.input, data.input_size, &buf, &buf_size)); ASSERT_TRUE(buf); - EXPECT_EQ(std::string((const char*)data.expected, data.expected_size), - std::string((const char*)buf, buf_size)) + EXPECT_EQ(data.expected_size, buf_size) << " for case " << i; + if (data.expected_size != buf_size) + continue; + EXPECT_EQ(0, memcmp(data.expected, buf, data.expected_size)) << " for case " << i; FX_Free(buf); } } TEST_F(FPDFParserDecodeEmbeddertest, FlateDecode) { - pdfium::DecodeTestData flate_decode_cases[] = { + static const pdfium::DecodeTestData flate_decode_cases[] = { STR_IN_OUT_CASE("", "", 0), STR_IN_OUT_CASE("preposterous nonsense", "", 2), STR_IN_OUT_CASE("\x78\x9c\x03\x00\x00\x00\x00\x01", "", 8), @@ -71,16 +73,18 @@ TEST_F(FPDFParserDecodeEmbeddertest, FlateDecode) { for (size_t i = 0; i < FX_ArraySize(flate_decode_cases); ++i) { const pdfium::DecodeTestData& data = flate_decode_cases[i]; - unsigned char* result = nullptr; - unsigned int result_size; + unsigned char* buf = nullptr; + uint32_t buf_size; EXPECT_EQ(data.processed_size, - FlateDecode(data.input, data.input_size, &result, &result_size)) + FlateDecode(data.input, data.input_size, &buf, &buf_size)) << " for case " << i; - ASSERT_TRUE(result); - EXPECT_EQ(std::string((const char*)data.expected, data.expected_size), - std::string((const char*)result, result_size)) + ASSERT_TRUE(buf); + EXPECT_EQ(data.expected_size, buf_size) << " for case " << i; + if (data.expected_size != buf_size) + continue; + EXPECT_EQ(0, memcmp(data.expected, buf, data.expected_size)) << " for case " << i; - FX_Free(result); + FX_Free(buf); } } diff --git a/core/fpdfdoc/cpdf_filespec_unittest.cpp b/core/fpdfdoc/cpdf_filespec_unittest.cpp index 5537473e2c..73fc8a4ae8 100644 --- a/core/fpdfdoc/cpdf_filespec_unittest.cpp +++ b/core/fpdfdoc/cpdf_filespec_unittest.cpp @@ -17,7 +17,7 @@ #include "third_party/base/ptr_util.h" TEST(cpdf_filespec, EncodeDecodeFileName) { - std::vector test_data = { + static const std::vector test_data = { // Empty src string. {L"", L""}, // only file name. @@ -59,7 +59,7 @@ TEST(cpdf_filespec, EncodeDecodeFileName) { TEST(cpdf_filespec, GetFileName) { { // String object. - pdfium::NullTermWstrFuncTestData test_data = { + static const pdfium::NullTermWstrFuncTestData test_data = { #if _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ L"/C/docs/test.pdf", L"C:\\docs\\test.pdf" @@ -77,7 +77,7 @@ TEST(cpdf_filespec, GetFileName) { } { // Dictionary object. - pdfium::NullTermWstrFuncTestData test_data[5] = { + static const pdfium::NullTermWstrFuncTestData test_data[] = { #if _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ {L"/C/docs/test.pdf", L"C:\\docs\\test.pdf"}, {L"/D/docs/test.pdf", L"D:\\docs\\test.pdf"}, @@ -99,11 +99,13 @@ TEST(cpdf_filespec, GetFileName) { #endif }; // Keyword fields in reverse order of precedence to retrieve the file name. - const char* const keywords[5] = {"Unix", "Mac", "DOS", "F", "UF"}; + const char* const keywords[] = {"Unix", "Mac", "DOS", "F", "UF"}; + static_assert(FX_ArraySize(test_data) == FX_ArraySize(keywords), + "size mismatch"); auto dict_obj = pdfium::MakeUnique(); CPDF_FileSpec file_spec(dict_obj.get()); EXPECT_TRUE(file_spec.GetFileName().IsEmpty()); - for (int i = 0; i < 5; ++i) { + for (size_t i = 0; i < FX_ArraySize(keywords); ++i) { dict_obj->SetNewFor(keywords[i], test_data[i].input); EXPECT_STREQ(test_data[i].expected, file_spec.GetFileName().c_str()); } @@ -122,7 +124,7 @@ TEST(cpdf_filespec, GetFileName) { } TEST(cpdf_filespec, SetFileName) { - pdfium::NullTermWstrFuncTestData test_data = { + static const pdfium::NullTermWstrFuncTestData test_data = { #if _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ L"C:\\docs\\test.pdf", L"/C/docs/test.pdf" @@ -180,9 +182,10 @@ TEST(cpdf_filespec, GetFileStream) { dict_obj->SetNewFor("EF"); CPDF_FileSpec file_spec(dict_obj.get()); - const char* const keys[5] = {"Unix", "Mac", "DOS", "F", "UF"}; const wchar_t file_name[] = L"test.pdf"; - const char* const stream[5] = {"test1", "test2", "test3", "test4", "test5"}; + const char* const keys[] = {"Unix", "Mac", "DOS", "F", "UF"}; + const char* const streams[] = {"test1", "test2", "test3", "test4", "test5"}; + static_assert(FX_ArraySize(keys) == FX_ArraySize(streams), "size mismatch"); CPDF_Dictionary* file_dict = file_spec.GetObj()->AsDictionary()->GetDictFor("EF"); @@ -193,15 +196,15 @@ TEST(cpdf_filespec, GetFileStream) { // Set the file stream. auto pDict = pdfium::MakeUnique(); - size_t buf_len = strlen(stream[i]) + 1; + size_t buf_len = strlen(streams[i]) + 1; std::unique_ptr buf(FX_Alloc(uint8_t, buf_len)); - memcpy(buf.get(), stream[i], buf_len); + memcpy(buf.get(), streams[i], buf_len); file_dict->SetNewFor(keys[i], std::move(buf), buf_len, std::move(pDict)); // Check that the file content stream is as expected. EXPECT_STREQ( - stream[i], + streams[i], file_spec.GetFileStream()->GetUnicodeText().UTF8Encode().c_str()); if (i == 2) { diff --git a/fpdfsdk/fpdf_flatten.cpp b/fpdfsdk/fpdf_flatten.cpp index a276e3a368..35d3617627 100644 --- a/fpdfsdk/fpdf_flatten.cpp +++ b/fpdfsdk/fpdf_flatten.cpp @@ -200,8 +200,7 @@ void SetPageContents(const ByteString& key, auto pAcc = pdfium::MakeRetain(pContentsStream); pAcc->LoadAllData(); ByteString sStream = "q\n"; - ByteString sBody = - ByteString((const char*)pAcc->GetData(), pAcc->GetSize()); + ByteString sBody = ByteString(pAcc->GetData(), pAcc->GetSize()); sStream = sStream + sBody + "\nQ"; pContentsStream->SetDataAndRemoveFilter(sStream.raw_str(), sStream.GetLength()); diff --git a/fpdfsdk/fpdfview.cpp b/fpdfsdk/fpdfview.cpp index dc2cfef126..2736b8dd6a 100644 --- a/fpdfsdk/fpdfview.cpp +++ b/fpdfsdk/fpdfview.cpp @@ -571,9 +571,8 @@ FPDF_EXPORT FPDF_DOCUMENT FPDF_CALLCONV FPDF_LoadDocument(FPDF_STRING file_path, FPDF_BYTESTRING password) { // NOTE: the creation of the file needs to be by the embedder on the // other side of this API. - return LoadDocumentImpl( - IFX_SeekableReadStream::CreateFromFilename((const char*)file_path), - password); + return LoadDocumentImpl(IFX_SeekableReadStream::CreateFromFilename(file_path), + password); } FPDF_EXPORT int FPDF_CALLCONV FPDF_GetFormType(FPDF_DOCUMENT document) { diff --git a/fxjs/cjs_globaldata.cpp b/fxjs/cjs_globaldata.cpp index fe7d94b9a4..3f962734db 100644 --- a/fxjs/cjs_globaldata.cpp +++ b/fxjs/cjs_globaldata.cpp @@ -337,8 +337,8 @@ void CJS_GlobalData::SaveGlobalPersisitentVariables() { CRYPT_ArcFourCryptBlock(sFile.GetBuffer(), sFile.GetSize(), JS_RC4KEY, sizeof(JS_RC4KEY)); - WriteFileBuffer(m_sFilePath.c_str(), (const char*)sFile.GetBuffer(), - sFile.GetSize()); + WriteFileBuffer(m_sFilePath.c_str(), + reinterpret_cast(sFile.GetBuffer()), sFile.GetSize()); } void CJS_GlobalData::LoadFileBuffer(const wchar_t* sFilePath, diff --git a/testing/test_support.h b/testing/test_support.h index 7525c10b36..ec4b4aeacb 100644 --- a/testing/test_support.h +++ b/testing/test_support.h @@ -19,33 +19,34 @@ namespace pdfium { -#define STR_IN_TEST_CASE(input_literal, ...) \ - { \ - (const unsigned char*) input_literal, sizeof(input_literal) - 1, \ - __VA_ARGS__ \ +#define STR_IN_TEST_CASE(input_literal, ...) \ + { \ + reinterpret_cast(input_literal), \ + sizeof(input_literal) - 1, __VA_ARGS__ \ } -#define STR_IN_OUT_CASE(input_literal, expected_literal, ...) \ - { \ - (const unsigned char*) input_literal, sizeof(input_literal) - 1, \ - (const unsigned char*)expected_literal, sizeof(expected_literal) - 1, \ - __VA_ARGS__ \ +#define STR_IN_OUT_CASE(input_literal, expected_literal, ...) \ + { \ + reinterpret_cast(input_literal), \ + sizeof(input_literal) - 1, \ + reinterpret_cast(expected_literal), \ + sizeof(expected_literal) - 1, __VA_ARGS__ \ } struct StrFuncTestData { const unsigned char* input; - unsigned int input_size; + uint32_t input_size; const unsigned char* expected; - unsigned int expected_size; + uint32_t expected_size; }; struct DecodeTestData { const unsigned char* input; - unsigned int input_size; + uint32_t input_size; const unsigned char* expected; - unsigned int expected_size; + uint32_t expected_size; // The size of input string being processed. - unsigned int processed_size; + uint32_t processed_size; }; struct NullTermWstrFuncTestData { -- cgit v1.2.3