From 53894390dba2ec571bab75157ebe60d11171ed07 Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Mon, 9 Apr 2018 18:30:24 +0000 Subject: Use pdfium::span<> in CFX_BitStream, CPDF_SimpleParser. Get bounds checks in parsers automatically when using spans. Change-Id: I71fbe7b838435d455376db2f89817d807a9cdcfd Reviewed-on: https://pdfium-review.googlesource.com/29830 Commit-Queue: Tom Sepez Reviewed-by: dsinclair --- core/fpdfapi/parser/cpdf_hint_tables.cpp | 3 +- core/fpdfapi/parser/cpdf_simple_parser.cpp | 39 +++++++++++----------- core/fpdfapi/parser/cpdf_simple_parser.h | 5 +-- .../fpdfapi/parser/cpdf_simple_parser_unittest.cpp | 3 +- core/fpdfapi/parser/cpdf_stream_acc.cpp | 16 +++------ core/fpdfapi/parser/cpdf_stream_acc.h | 11 +++--- 6 files changed, 36 insertions(+), 41 deletions(-) (limited to 'core/fpdfapi/parser') diff --git a/core/fpdfapi/parser/cpdf_hint_tables.cpp b/core/fpdfapi/parser/cpdf_hint_tables.cpp index 381b160233..123e6594c1 100644 --- a/core/fpdfapi/parser/cpdf_hint_tables.cpp +++ b/core/fpdfapi/parser/cpdf_hint_tables.cpp @@ -19,6 +19,7 @@ #include "core/fxcrt/cfx_bitstream.h" #include "core/fxcrt/fx_safe_types.h" #include "third_party/base/numerics/safe_conversions.h" +#include "third_party/base/span.h" namespace { @@ -488,7 +489,7 @@ bool CPDF_HintTables::LoadHintStream(CPDF_Stream* pHintStream) { return false; } - CFX_BitStream bs(pAcc->GetData(), size); + CFX_BitStream bs(pdfium::make_span(pAcc->GetData(), size)); return ReadPageHintTable(&bs) && ReadSharedObjHintTable(&bs, shared_hint_table_offset); } diff --git a/core/fpdfapi/parser/cpdf_simple_parser.cpp b/core/fpdfapi/parser/cpdf_simple_parser.cpp index 45ea0d4528..ff6e2cf94d 100644 --- a/core/fpdfapi/parser/cpdf_simple_parser.cpp +++ b/core/fpdfapi/parser/cpdf_simple_parser.cpp @@ -8,7 +8,8 @@ #include "core/fpdfapi/parser/fpdf_parser_utility.h" -CPDF_SimpleParser::CPDF_SimpleParser(const ByteStringView& str) : data_(str) {} +CPDF_SimpleParser::CPDF_SimpleParser(pdfium::span input) + : data_(input) {} CPDF_SimpleParser::~CPDF_SimpleParser() = default; @@ -17,12 +18,12 @@ ByteStringView CPDF_SimpleParser::GetWord() { // Skip whitespace and comment lines. while (1) { - if (data_.GetLength() <= cur_pos_) + if (data_.size() <= cur_pos_) return ByteStringView(); ch = data_[cur_pos_++]; while (PDFCharIsWhitespace(ch)) { - if (data_.GetLength() <= cur_pos_) + if (data_.size() <= cur_pos_) return ByteStringView(); ch = data_[cur_pos_++]; } @@ -31,7 +32,7 @@ ByteStringView CPDF_SimpleParser::GetWord() { break; while (1) { - if (data_.GetLength() <= cur_pos_) + if (data_.size() <= cur_pos_) return ByteStringView(); ch = data_[cur_pos_++]; @@ -46,7 +47,7 @@ ByteStringView CPDF_SimpleParser::GetWord() { // Find names if (ch == '/') { while (1) { - if (data_.GetLength() <= cur_pos_) + if (data_.size() <= cur_pos_) break; ch = data_[cur_pos_++]; @@ -56,29 +57,29 @@ ByteStringView CPDF_SimpleParser::GetWord() { break; } } - return data_.Mid(start_pos, dwSize); + return data_.subspan(start_pos, dwSize); } dwSize = 1; if (ch == '<') { - if (data_.GetLength() <= cur_pos_) - return data_.Mid(start_pos, dwSize); + if (data_.size() <= cur_pos_) + return data_.subspan(start_pos, dwSize); ch = data_[cur_pos_++]; if (ch == '<') { dwSize = 2; } else { - while (cur_pos_ < data_.GetLength() && data_[cur_pos_] != '>') + while (cur_pos_ < data_.size() && data_[cur_pos_] != '>') cur_pos_++; - if (cur_pos_ < data_.GetLength()) + if (cur_pos_ < data_.size()) cur_pos_++; dwSize = cur_pos_ - start_pos; } } else if (ch == '>') { - if (data_.GetLength() <= cur_pos_) - return data_.Mid(start_pos, dwSize); + if (data_.size() <= cur_pos_) + return data_.subspan(start_pos, dwSize); ch = data_[cur_pos_++]; if (ch == '>') @@ -87,7 +88,7 @@ ByteStringView CPDF_SimpleParser::GetWord() { cur_pos_--; } else if (ch == '(') { int level = 1; - while (cur_pos_ < data_.GetLength()) { + while (cur_pos_ < data_.size()) { if (data_[cur_pos_] == ')') { level--; if (level == 0) @@ -95,28 +96,28 @@ ByteStringView CPDF_SimpleParser::GetWord() { } if (data_[cur_pos_] == '\\') { - if (data_.GetLength() <= cur_pos_) + if (data_.size() <= cur_pos_) break; cur_pos_++; } else if (data_[cur_pos_] == '(') { level++; } - if (data_.GetLength() <= cur_pos_) + if (data_.size() <= cur_pos_) break; cur_pos_++; } - if (cur_pos_ < data_.GetLength()) + if (cur_pos_ < data_.size()) cur_pos_++; dwSize = cur_pos_ - start_pos; } - return data_.Mid(start_pos, dwSize); + return data_.subspan(start_pos, dwSize); } dwSize = 1; - while (cur_pos_ < data_.GetLength()) { + while (cur_pos_ < data_.size()) { ch = data_[cur_pos_++]; if (PDFCharIsDelimiter(ch) || PDFCharIsWhitespace(ch)) { @@ -125,5 +126,5 @@ ByteStringView CPDF_SimpleParser::GetWord() { } dwSize++; } - return data_.Mid(start_pos, dwSize); + return data_.subspan(start_pos, dwSize); } diff --git a/core/fpdfapi/parser/cpdf_simple_parser.h b/core/fpdfapi/parser/cpdf_simple_parser.h index 8a07323a69..c8dae23ba6 100644 --- a/core/fpdfapi/parser/cpdf_simple_parser.h +++ b/core/fpdfapi/parser/cpdf_simple_parser.h @@ -11,10 +11,11 @@ #include "core/fxcrt/fx_string.h" #include "core/fxcrt/fx_system.h" +#include "third_party/base/span.h" class CPDF_SimpleParser { public: - explicit CPDF_SimpleParser(const ByteStringView& str); + explicit CPDF_SimpleParser(pdfium::span input); ~CPDF_SimpleParser(); ByteStringView GetWord(); @@ -23,7 +24,7 @@ class CPDF_SimpleParser { uint32_t GetCurPos() const { return cur_pos_; } private: - const ByteStringView data_; + const pdfium::span data_; uint32_t cur_pos_ = 0; }; diff --git a/core/fpdfapi/parser/cpdf_simple_parser_unittest.cpp b/core/fpdfapi/parser/cpdf_simple_parser_unittest.cpp index 2fb50f1ff3..254d084bb0 100644 --- a/core/fpdfapi/parser/cpdf_simple_parser_unittest.cpp +++ b/core/fpdfapi/parser/cpdf_simple_parser_unittest.cpp @@ -9,6 +9,7 @@ #include "core/fpdfapi/parser/fpdf_parser_utility.h" #include "testing/gtest/include/gtest/gtest.h" #include "testing/test_support.h" +#include "third_party/base/span.h" TEST(SimpleParserTest, GetWord) { static const pdfium::StrFuncTestData test_data[] = { @@ -49,7 +50,7 @@ TEST(SimpleParserTest, GetWord) { }; for (size_t i = 0; i < FX_ArraySize(test_data); ++i) { const pdfium::StrFuncTestData& data = test_data[i]; - CPDF_SimpleParser parser(ByteStringView(data.input, data.input_size)); + CPDF_SimpleParser parser(pdfium::make_span(data.input, data.input_size)); ByteStringView word = parser.GetWord(); EXPECT_EQ(data.expected_size, word.GetLength()) << " for case " << i; if (data.expected_size != word.GetLength()) diff --git a/core/fpdfapi/parser/cpdf_stream_acc.cpp b/core/fpdfapi/parser/cpdf_stream_acc.cpp index d115b48226..9cce894fff 100644 --- a/core/fpdfapi/parser/cpdf_stream_acc.cpp +++ b/core/fpdfapi/parser/cpdf_stream_acc.cpp @@ -68,12 +68,10 @@ CPDF_Dictionary* CPDF_StreamAcc::GetDict() const { return m_pStream ? m_pStream->GetDict() : nullptr; } -const uint8_t* CPDF_StreamAcc::GetData() const { - return GetDataHelper(); -} - -uint8_t* CPDF_StreamAcc::GetData() { - return GetDataHelper(); +uint8_t* CPDF_StreamAcc::GetData() const { + if (m_bNewBuf) + return m_pData; + return m_pStream ? m_pStream->GetRawData() : nullptr; } uint32_t CPDF_StreamAcc::GetSize() const { @@ -93,9 +91,3 @@ std::unique_ptr CPDF_StreamAcc::DetachData() { memcpy(p.get(), m_pData, m_dwSize); return p; } - -uint8_t* CPDF_StreamAcc::GetDataHelper() const { - if (m_bNewBuf) - return m_pData; - return m_pStream ? m_pStream->GetRawData() : nullptr; -} diff --git a/core/fpdfapi/parser/cpdf_stream_acc.h b/core/fpdfapi/parser/cpdf_stream_acc.h index ac5253a68b..89a454a0f0 100644 --- a/core/fpdfapi/parser/cpdf_stream_acc.h +++ b/core/fpdfapi/parser/cpdf_stream_acc.h @@ -14,6 +14,7 @@ #include "core/fxcrt/fx_string.h" #include "core/fxcrt/fx_system.h" #include "core/fxcrt/retain_ptr.h" +#include "third_party/base/span.h" class CPDF_StreamAcc : public Retainable { public: @@ -30,11 +31,11 @@ class CPDF_StreamAcc : public Retainable { const CPDF_Stream* GetStream() const { return m_pStream.Get(); } CPDF_Dictionary* GetDict() const; - ByteStringView GetDataView() { return ByteStringView(GetData(), GetSize()); } - - const uint8_t* GetData() const; - uint8_t* GetData(); + uint8_t* GetData() const; uint32_t GetSize() const; + pdfium::span GetSpan() const { + return pdfium::make_span(GetData(), GetSize()); + } const ByteString& GetImageDecoder() const { return m_ImageDecoder; } const CPDF_Dictionary* GetImageParam() const { return m_pImageParam; } std::unique_ptr DetachData(); @@ -44,8 +45,6 @@ class CPDF_StreamAcc : public Retainable { ~CPDF_StreamAcc() override; private: - uint8_t* GetDataHelper() const; - uint8_t* m_pData = nullptr; uint32_t m_dwSize = 0; bool m_bNewBuf = false; -- cgit v1.2.3