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 ++++++++++-------- 4 files changed, 42 insertions(+), 32 deletions(-) (limited to 'core') 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) { -- cgit v1.2.3