From 9160cec4c8a0559586d90a6dcf9b2a2551a5fbd8 Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Wed, 25 Jul 2018 20:09:58 +0000 Subject: Change CFX_BufferSeekableReadStream to take a span. Change-Id: Ib9e20fdfc637b2ba0358586e23ad72454b0b8ad1 Reviewed-on: https://pdfium-review.googlesource.com/38851 Commit-Queue: Lei Zhang Reviewed-by: Tom Sepez --- .../parser/cpdf_cross_ref_avail_unittest.cpp | 34 +++++++++------------- core/fpdfapi/parser/cpdf_hint_tables_unittest.cpp | 3 +- core/fpdfapi/parser/cpdf_parser_unittest.cpp | 27 +++++++---------- testing/fuzzers/xfa_codec_fuzzer.h | 4 ++- testing/fx_string_testhelpers.cpp | 20 +++++++++++-- testing/fx_string_testhelpers.h | 21 ++++--------- 6 files changed, 50 insertions(+), 59 deletions(-) diff --git a/core/fpdfapi/parser/cpdf_cross_ref_avail_unittest.cpp b/core/fpdfapi/parser/cpdf_cross_ref_avail_unittest.cpp index 4ee6b59ad5..eab7330cf6 100644 --- a/core/fpdfapi/parser/cpdf_cross_ref_avail_unittest.cpp +++ b/core/fpdfapi/parser/cpdf_cross_ref_avail_unittest.cpp @@ -15,10 +15,9 @@ namespace { std::unique_ptr MakeParserForBuffer( - const unsigned char* buffer, - size_t buffer_size) { + pdfium::span buffer) { return pdfium::MakeUnique( - pdfium::MakeRetain(buffer, buffer_size)); + pdfium::MakeRetain(buffer)); } } // namespace @@ -39,7 +38,7 @@ TEST(CPDF_CrossRefAvailTest, CheckCrossRefV4) { "/Info 15 0 R/Size 16>>"; const FX_FILESIZE last_crossref_offset = 0; - auto parser = MakeParserForBuffer(xref_table, FX_ArraySize(xref_table)); + auto parser = MakeParserForBuffer(xref_table); auto cross_ref_avail = pdfium::MakeUnique( parser.get(), last_crossref_offset); @@ -56,7 +55,7 @@ TEST(CPDF_CrossRefAvailTest, CheckCrossRefStream) { "endobj\n"; const FX_FILESIZE last_crossref_offset = 0; - auto parser = MakeParserForBuffer(xref_stream, FX_ArraySize(xref_stream)); + auto parser = MakeParserForBuffer(xref_stream); auto cross_ref_avail = pdfium::MakeUnique( parser.get(), last_crossref_offset); @@ -74,7 +73,7 @@ TEST(CPDF_CrossRefAvailTest, IncorrectStartOffset) { const FX_FILESIZE last_crossref_offset = 70000; - auto parser = MakeParserForBuffer(xref_stream, FX_ArraySize(xref_stream)); + auto parser = MakeParserForBuffer(xref_stream); auto cross_ref_avail = pdfium::MakeUnique( parser.get(), last_crossref_offset); @@ -91,7 +90,7 @@ TEST(CPDF_CrossRefAvailTest, IncorrectPrevOffset) { "endobj\n"; const FX_FILESIZE last_crossref_offset = 0; - auto parser = MakeParserForBuffer(xref_stream, FX_ArraySize(xref_stream)); + auto parser = MakeParserForBuffer(xref_stream); auto cross_ref_avail = pdfium::MakeUnique( parser.get(), last_crossref_offset); EXPECT_EQ(CPDF_DataAvail::DataError, cross_ref_avail->CheckAvail()); @@ -113,7 +112,7 @@ TEST(CPDF_CrossRefAvailTest, IncorrectPrevStreamOffset) { "/Info 15 0 R/Size 16 /XRefStm 70000>>"; const FX_FILESIZE last_crossref_offset = 0; - auto parser = MakeParserForBuffer(xref_table, FX_ArraySize(xref_table)); + auto parser = MakeParserForBuffer(xref_table); auto cross_ref_avail = pdfium::MakeUnique( parser.get(), last_crossref_offset); EXPECT_EQ(CPDF_DataAvail::DataError, cross_ref_avail->CheckAvail()); @@ -125,8 +124,7 @@ TEST(CPDF_CrossRefAvailTest, IncorrectData) { "wfoihoiwfghouiafghwoigahfi"; const FX_FILESIZE last_crossref_offset = 0; - auto parser = - MakeParserForBuffer(incorrect_data, FX_ArraySize(incorrect_data)); + auto parser = MakeParserForBuffer(incorrect_data); auto cross_ref_avail = pdfium::MakeUnique( parser.get(), last_crossref_offset); EXPECT_EQ(CPDF_DataAvail::DataError, cross_ref_avail->CheckAvail()); @@ -174,8 +172,7 @@ TEST(CPDF_CrossRefAvailTest, ThreeCrossRefV4) { FXSYS_itoa(static_cast(prev_offset), int_buffer, 10) + ">>\n"; const FX_FILESIZE last_crossref_offset = static_cast(cur_offset); - auto parser = MakeParserForBuffer( - reinterpret_cast(table.data()), table.size()); + auto parser = MakeParserForBuffer(pdfium::as_bytes(pdfium::make_span(table))); auto cross_ref_avail = pdfium::MakeUnique( parser.get(), last_crossref_offset); EXPECT_EQ(CPDF_DataAvail::DataAvailable, cross_ref_avail->CheckAvail()); @@ -219,8 +216,7 @@ TEST(CPDF_CrossRefAvailTest, ThreeCrossRefV5) { "endobj\n"; const FX_FILESIZE last_crossref_offset = static_cast(cur_offset); - auto parser = MakeParserForBuffer( - reinterpret_cast(table.data()), table.size()); + auto parser = MakeParserForBuffer(pdfium::as_bytes(pdfium::make_span(table))); auto cross_ref_avail = pdfium::MakeUnique( parser.get(), last_crossref_offset); EXPECT_EQ(CPDF_DataAvail::DataAvailable, cross_ref_avail->CheckAvail()); @@ -269,8 +265,7 @@ TEST(CPDF_CrossRefAvailTest, Mixed) { FXSYS_itoa(first_v5_table_offset, int_buffer, 10) + ">>\n"; const FX_FILESIZE last_crossref_offset = last_v4_table_offset; - auto parser = MakeParserForBuffer( - reinterpret_cast(table.data()), table.size()); + auto parser = MakeParserForBuffer(pdfium::as_bytes(pdfium::make_span(table))); auto cross_ref_avail = pdfium::MakeUnique( parser.get(), last_crossref_offset); EXPECT_EQ(CPDF_DataAvail::DataAvailable, cross_ref_avail->CheckAvail()); @@ -284,8 +279,7 @@ TEST(CPDF_CrossRefAvailTest, CrossRefV5IsNotStream) { "endobj\n"; const FX_FILESIZE last_crossref_offset = 0; - auto parser = MakeParserForBuffer(invalid_xref_stream, - FX_ArraySize(invalid_xref_stream)); + auto parser = MakeParserForBuffer(invalid_xref_stream); auto cross_ref_avail = pdfium::MakeUnique( parser.get(), last_crossref_offset); EXPECT_EQ(CPDF_DataAvail::DataError, cross_ref_avail->CheckAvail()); @@ -308,7 +302,7 @@ TEST(CPDF_CrossRefAvailTest, CrossRefV4WithEncryptRef) { "/Info 15 0 R/Size 16>>"; const FX_FILESIZE last_crossref_offset = 0; - auto parser = MakeParserForBuffer(xref_table, FX_ArraySize(xref_table)); + auto parser = MakeParserForBuffer(xref_table); auto cross_ref_avail = pdfium::MakeUnique( parser.get(), last_crossref_offset); EXPECT_EQ(CPDF_DataAvail::DataError, cross_ref_avail->CheckAvail()); @@ -324,7 +318,7 @@ TEST(CPDF_CrossRefAvailTest, CrossRefStreamWithEncryptRef) { "endobj\n"; const FX_FILESIZE last_crossref_offset = 0; - auto parser = MakeParserForBuffer(xref_stream, FX_ArraySize(xref_stream)); + auto parser = MakeParserForBuffer(xref_stream); auto cross_ref_avail = pdfium::MakeUnique( parser.get(), last_crossref_offset); EXPECT_EQ(CPDF_DataAvail::DataError, cross_ref_avail->CheckAvail()); diff --git a/core/fpdfapi/parser/cpdf_hint_tables_unittest.cpp b/core/fpdfapi/parser/cpdf_hint_tables_unittest.cpp index 34c1694c59..dba39f5594 100644 --- a/core/fpdfapi/parser/cpdf_hint_tables_unittest.cpp +++ b/core/fpdfapi/parser/cpdf_hint_tables_unittest.cpp @@ -49,8 +49,7 @@ class TestLinearizedHeader : public CPDF_LinearizedHeader { static std::unique_ptr MakeHeader( const std::string& inline_data) { CPDF_SyntaxParser parser(pdfium::MakeRetain( - reinterpret_cast(inline_data.data()), - inline_data.size())); + pdfium::as_bytes(pdfium::make_span(inline_data)))); std::unique_ptr dict = ToDictionary(parser.GetObjectBody(nullptr)); ASSERT(dict); diff --git a/core/fpdfapi/parser/cpdf_parser_unittest.cpp b/core/fpdfapi/parser/cpdf_parser_unittest.cpp index 2e7e06066b..d5166fbdf0 100644 --- a/core/fpdfapi/parser/cpdf_parser_unittest.cpp +++ b/core/fpdfapi/parser/cpdf_parser_unittest.cpp @@ -47,17 +47,16 @@ class CPDF_TestParser : public CPDF_Parser { } // Setup reading from a buffer and initial states. - bool InitTestFromBufferWithOffset(const unsigned char* buffer, - size_t len, + bool InitTestFromBufferWithOffset(pdfium::span buffer, FX_FILESIZE header_offset) { m_pSyntax = CPDF_SyntaxParser::CreateForTesting( - pdfium::MakeRetain(buffer, len), + pdfium::MakeRetain(buffer), header_offset); return true; } - bool InitTestFromBuffer(const unsigned char* buffer, size_t len) { - return InitTestFromBufferWithOffset(buffer, len, 0 /*header_offset*/); + bool InitTestFromBuffer(pdfium::span buffer) { + return InitTestFromBufferWithOffset(buffer, 0 /*header_offset*/); } private: @@ -109,8 +108,7 @@ TEST(cpdf_parser, LoadCrossRefV4) { "0000000409 00000 n \n" "trail"; // Needed to end cross ref table reading. CPDF_TestParser parser; - ASSERT_TRUE( - parser.InitTestFromBuffer(xref_table, FX_ArraySize(xref_table))); + ASSERT_TRUE(parser.InitTestFromBuffer(xref_table)); ASSERT_TRUE(parser.LoadCrossRefV4(0, false)); const FX_FILESIZE offsets[] = {0, 17, 81, 0, 331, 409}; @@ -140,8 +138,7 @@ TEST(cpdf_parser, LoadCrossRefV4) { "0000025777 00000 n \n" "trail"; // Needed to end cross ref table reading. CPDF_TestParser parser; - ASSERT_TRUE( - parser.InitTestFromBuffer(xref_table, FX_ArraySize(xref_table))); + ASSERT_TRUE(parser.InitTestFromBuffer(xref_table)); ASSERT_TRUE(parser.LoadCrossRefV4(0, false)); const FX_FILESIZE offsets[] = {0, 0, 0, 25325, 0, 0, 0, @@ -179,8 +176,7 @@ TEST(cpdf_parser, LoadCrossRefV4) { "0000025777 00000 n \n" "trail"; // Needed to end cross ref table reading. CPDF_TestParser parser; - ASSERT_TRUE( - parser.InitTestFromBuffer(xref_table, FX_ArraySize(xref_table))); + ASSERT_TRUE(parser.InitTestFromBuffer(xref_table)); ASSERT_TRUE(parser.LoadCrossRefV4(0, false)); const FX_FILESIZE offsets[] = {0, 0, 0, 25325, 0, 0, 0, @@ -217,8 +213,7 @@ TEST(cpdf_parser, LoadCrossRefV4) { "0000000179 00000 n \n" "trail"; // Needed to end cross ref table reading. CPDF_TestParser parser; - ASSERT_TRUE( - parser.InitTestFromBuffer(xref_table, FX_ArraySize(xref_table))); + ASSERT_TRUE(parser.InitTestFromBuffer(xref_table)); ASSERT_TRUE(parser.LoadCrossRefV4(0, false)); const FX_FILESIZE offsets[] = {0, 23, 0, 0, 0, 45, 179}; @@ -264,8 +259,7 @@ TEST(cpdf_parser, ParseStartXRefWithHeaderOffset) { ASSERT_TRUE(pFileAccess->ReadBlock(&data.front() + kTestHeaderOffset, 0, pFileAccess->GetSize())); CPDF_TestParser parser; - parser.InitTestFromBufferWithOffset(&data.front(), data.size(), - kTestHeaderOffset); + parser.InitTestFromBufferWithOffset(data, kTestHeaderOffset); EXPECT_EQ(100940, parser.ParseStartXRef()); std::unique_ptr cross_ref_v5_obj = @@ -286,8 +280,7 @@ TEST(cpdf_parser, ParseLinearizedWithHeaderOffset) { ASSERT_TRUE(pFileAccess->ReadBlock(&data.front() + kTestHeaderOffset, 0, pFileAccess->GetSize())); CPDF_TestParser parser; - parser.InitTestFromBufferWithOffset(&data.front(), data.size(), - kTestHeaderOffset); + parser.InitTestFromBufferWithOffset(data, kTestHeaderOffset); EXPECT_TRUE(parser.ParseLinearizedHeader()); } diff --git a/testing/fuzzers/xfa_codec_fuzzer.h b/testing/fuzzers/xfa_codec_fuzzer.h index bfc069a31d..bc40f653af 100644 --- a/testing/fuzzers/xfa_codec_fuzzer.h +++ b/testing/fuzzers/xfa_codec_fuzzer.h @@ -12,6 +12,7 @@ #include "core/fxge/dib/cfx_dibitmap.h" #include "testing/fx_string_testhelpers.h" #include "third_party/base/ptr_util.h" +#include "third_party/base/span.h" #ifdef PDF_ENABLE_XFA_BMP #include "core/fxcodec/codec/ccodec_bmpmodule.h" @@ -52,7 +53,8 @@ class XFACodecFuzzer { std::unique_ptr decoder = mgr->CreateProgressiveDecoder(); - auto source = pdfium::MakeRetain(data, size); + auto source = pdfium::MakeRetain( + pdfium::make_span(data, size)); FXCODEC_STATUS status = decoder->LoadImageInfo(source, type, nullptr, true); if (status != FXCODEC_STATUS_FRAME_READY) return 0; diff --git a/testing/fx_string_testhelpers.cpp b/testing/fx_string_testhelpers.cpp index e2ce9353fb..e3c443ac4e 100644 --- a/testing/fx_string_testhelpers.cpp +++ b/testing/fx_string_testhelpers.cpp @@ -24,8 +24,22 @@ CFX_InvalidSeekableReadStream::CFX_InvalidSeekableReadStream( CFX_InvalidSeekableReadStream::~CFX_InvalidSeekableReadStream() = default; CFX_BufferSeekableReadStream::CFX_BufferSeekableReadStream( - const unsigned char* src, - size_t src_size) - : data_(src), data_size_(src_size) {} + pdfium::span data) + : data_(data) {} CFX_BufferSeekableReadStream::~CFX_BufferSeekableReadStream() = default; + +bool CFX_BufferSeekableReadStream::ReadBlock(void* buffer, + FX_FILESIZE offset, + size_t size) { + if (offset < 0 || static_cast(offset) >= data_.size()) + return false; + + if (static_cast(offset) + size > data_.size()) + size = data_.size() - static_cast(offset); + if (size == 0) + return false; + + memcpy(buffer, &data_[offset], size); + return true; +} diff --git a/testing/fx_string_testhelpers.h b/testing/fx_string_testhelpers.h index cb59cfbc03..4cddefab0a 100644 --- a/testing/fx_string_testhelpers.h +++ b/testing/fx_string_testhelpers.h @@ -9,6 +9,7 @@ #include "core/fxcrt/cfx_datetime.h" #include "core/fxcrt/fx_stream.h" +#include "third_party/base/span.h" // Output stream operator so GTEST macros work with CFX_DateTime objects. std::ostream& operator<<(std::ostream& os, const CFX_DateTime& dt); @@ -37,29 +38,17 @@ class CFX_BufferSeekableReadStream : public IFX_SeekableReadStream { friend RetainPtr pdfium::MakeRetain(Args&&... args); // IFX_SeekableReadStream: - bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override { - if (offset < 0 || static_cast(offset) >= data_size_) - return false; - - if (static_cast(offset) + size > data_size_) - size = data_size_ - static_cast(offset); - if (size == 0) - return false; - - memcpy(buffer, data_ + offset, size); - return true; - } + bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override; FX_FILESIZE GetSize() override { - return static_cast(data_size_); + return static_cast(data_.size()); } private: - CFX_BufferSeekableReadStream(const unsigned char* src, size_t src_size); + explicit CFX_BufferSeekableReadStream(pdfium::span data); ~CFX_BufferSeekableReadStream() override; - const unsigned char* data_; - size_t data_size_; + pdfium::span data_; }; #endif // TESTING_FX_STRING_TESTHELPERS_H_ -- cgit v1.2.3