diff options
-rw-r--r-- | core/fpdfapi/parser/cpdf_cross_ref_avail_unittest.cpp | 34 | ||||
-rw-r--r-- | core/fpdfapi/parser/cpdf_hint_tables_unittest.cpp | 3 | ||||
-rw-r--r-- | core/fpdfapi/parser/cpdf_parser_unittest.cpp | 27 | ||||
-rw-r--r-- | testing/fuzzers/xfa_codec_fuzzer.h | 4 | ||||
-rw-r--r-- | testing/fx_string_testhelpers.cpp | 20 | ||||
-rw-r--r-- | 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<CPDF_SyntaxParser> MakeParserForBuffer( - const unsigned char* buffer, - size_t buffer_size) { + pdfium::span<const uint8_t> buffer) { return pdfium::MakeUnique<CPDF_SyntaxParser>( - pdfium::MakeRetain<CFX_BufferSeekableReadStream>(buffer, buffer_size)); + pdfium::MakeRetain<CFX_BufferSeekableReadStream>(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<CPDF_CrossRefAvail>( 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<CPDF_CrossRefAvail>( 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<CPDF_CrossRefAvail>( 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<CPDF_CrossRefAvail>( 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<CPDF_CrossRefAvail>( 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<CPDF_CrossRefAvail>( 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<int>(prev_offset), int_buffer, 10) + ">>\n"; const FX_FILESIZE last_crossref_offset = static_cast<FX_FILESIZE>(cur_offset); - auto parser = MakeParserForBuffer( - reinterpret_cast<const unsigned char*>(table.data()), table.size()); + auto parser = MakeParserForBuffer(pdfium::as_bytes(pdfium::make_span(table))); auto cross_ref_avail = pdfium::MakeUnique<CPDF_CrossRefAvail>( 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<FX_FILESIZE>(cur_offset); - auto parser = MakeParserForBuffer( - reinterpret_cast<const unsigned char*>(table.data()), table.size()); + auto parser = MakeParserForBuffer(pdfium::as_bytes(pdfium::make_span(table))); auto cross_ref_avail = pdfium::MakeUnique<CPDF_CrossRefAvail>( 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<const unsigned char*>(table.data()), table.size()); + auto parser = MakeParserForBuffer(pdfium::as_bytes(pdfium::make_span(table))); auto cross_ref_avail = pdfium::MakeUnique<CPDF_CrossRefAvail>( 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<CPDF_CrossRefAvail>( 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<CPDF_CrossRefAvail>( 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<CPDF_CrossRefAvail>( 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<CPDF_LinearizedHeader> MakeHeader( const std::string& inline_data) { CPDF_SyntaxParser parser(pdfium::MakeRetain<CFX_BufferSeekableReadStream>( - reinterpret_cast<const unsigned char*>(inline_data.data()), - inline_data.size())); + pdfium::as_bytes(pdfium::make_span(inline_data)))); std::unique_ptr<CPDF_Dictionary> 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<const uint8_t> buffer, FX_FILESIZE header_offset) { m_pSyntax = CPDF_SyntaxParser::CreateForTesting( - pdfium::MakeRetain<CFX_BufferSeekableReadStream>(buffer, len), + pdfium::MakeRetain<CFX_BufferSeekableReadStream>(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<const uint8_t> 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<CPDF_Object> 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<CCodec_ProgressiveDecoder> decoder = mgr->CreateProgressiveDecoder(); - auto source = pdfium::MakeRetain<CFX_BufferSeekableReadStream>(data, size); + auto source = pdfium::MakeRetain<CFX_BufferSeekableReadStream>( + 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<const uint8_t> 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<size_t>(offset) >= data_.size()) + return false; + + if (static_cast<size_t>(offset) + size > data_.size()) + size = data_.size() - static_cast<size_t>(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<T> pdfium::MakeRetain(Args&&... args); // IFX_SeekableReadStream: - bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override { - if (offset < 0 || static_cast<size_t>(offset) >= data_size_) - return false; - - if (static_cast<size_t>(offset) + size > data_size_) - size = data_size_ - static_cast<size_t>(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<FX_FILESIZE>(data_size_); + return static_cast<FX_FILESIZE>(data_.size()); } private: - CFX_BufferSeekableReadStream(const unsigned char* src, size_t src_size); + explicit CFX_BufferSeekableReadStream(pdfium::span<const uint8_t> data); ~CFX_BufferSeekableReadStream() override; - const unsigned char* data_; - size_t data_size_; + pdfium::span<const uint8_t> data_; }; #endif // TESTING_FX_STRING_TESTHELPERS_H_ |