Make CPDF_StreamContentParser own its stream parser. Convert an unowned reference to an unique_ptr<>, since no other object needs such a reference, at the cost of making a heap vs. stack allocation. Change-Id: I81fa29f3f1047506da0dde6f46907817ac9cc79b Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/79772 Reviewed-by: Lei Zhang <thestig@chromium.org> Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfapi/page/cpdf_streamcontentparser.cpp b/core/fpdfapi/page/cpdf_streamcontentparser.cpp index 153aa04..2e7fbc6 100644 --- a/core/fpdfapi/page/cpdf_streamcontentparser.cpp +++ b/core/fpdfapi/page/cpdf_streamcontentparser.cpp
@@ -1500,32 +1500,33 @@ pDataStart); uint32_t init_obj_count = m_pObjectHolder->GetPageObjectCount(); - CPDF_StreamParser syntax(pdfium::make_span(pDataStart, size_left), - m_pDocument->GetByteStringPool()); - AutoNuller<UnownedPtr<CPDF_StreamParser>> auto_clearer(&m_pSyntax); - m_pSyntax = &syntax; + AutoNuller<std::unique_ptr<CPDF_StreamParser>> auto_clearer(&m_pSyntax); + m_pSyntax = std::make_unique<CPDF_StreamParser>( + pdfium::make_span(pDataStart, size_left), + m_pDocument->GetByteStringPool()); + while (1) { uint32_t cost = m_pObjectHolder->GetPageObjectCount() - init_obj_count; if (max_cost && cost >= max_cost) { break; } - switch (syntax.ParseNextElement()) { + switch (m_pSyntax->ParseNextElement()) { case CPDF_StreamParser::EndOfData: return m_pSyntax->GetPos(); case CPDF_StreamParser::Keyword: - OnOperator(syntax.GetWord()); + OnOperator(m_pSyntax->GetWord()); ClearAllParams(); break; case CPDF_StreamParser::Number: - AddNumberParam(syntax.GetWord()); + AddNumberParam(m_pSyntax->GetWord()); break; case CPDF_StreamParser::Name: { - auto word = syntax.GetWord(); + auto word = m_pSyntax->GetWord(); AddNameParam(word.Last(word.GetLength() - 1)); break; } default: - AddObjectParam(syntax.GetObject()); + AddObjectParam(m_pSyntax->GetObject()); } } return m_pSyntax->GetPos();
diff --git a/core/fpdfapi/page/cpdf_streamcontentparser.h b/core/fpdfapi/page/cpdf_streamcontentparser.h index 87aef15..66231aa9 100644 --- a/core/fpdfapi/page/cpdf_streamcontentparser.h +++ b/core/fpdfapi/page/cpdf_streamcontentparser.h
@@ -214,7 +214,7 @@ const CFX_FloatRect m_BBox; uint32_t m_ParamStartPos = 0; uint32_t m_ParamCount = 0; - UnownedPtr<CPDF_StreamParser> m_pSyntax; + std::unique_ptr<CPDF_StreamParser> m_pSyntax; std::unique_ptr<CPDF_AllStates> m_pCurStates; std::stack<std::unique_ptr<CPDF_ContentMarks>> m_ContentMarksStack; std::vector<std::unique_ptr<CPDF_TextObject>> m_ClipTextList;