From 4e847e36fe013bcfddf71c79221887a308b9fadb Mon Sep 17 00:00:00 2001 From: npm Date: Wed, 23 Nov 2016 14:47:35 -0800 Subject: More unique_ptrs in CXFA_FMParse::ParseForeachExpression and avoid leak Commit a31098417852bdf13e693a6e0913e0706cf94098 accidentally caused a leak. Replaced pointers with unique_ptr to prevent this from happening. BUG=664891 Review-Url: https://codereview.chromium.org/2528543003 --- xfa/fxfa/fm2js/xfa_expression.cpp | 25 +++++++++---------------- xfa/fxfa/fm2js/xfa_expression.h | 5 +++-- xfa/fxfa/fm2js/xfa_fmparse.cpp | 26 ++++++++++---------------- 3 files changed, 22 insertions(+), 34 deletions(-) (limited to 'xfa/fxfa/fm2js') diff --git a/xfa/fxfa/fm2js/xfa_expression.cpp b/xfa/fxfa/fm2js/xfa_expression.cpp index 6895f25438..5734593ff8 100644 --- a/xfa/fxfa/fm2js/xfa_expression.cpp +++ b/xfa/fxfa/fm2js/xfa_expression.cpp @@ -510,21 +510,14 @@ void CXFA_FMForExpression::ToImpliedReturnJS(CFX_WideTextBuf& javascript) { CXFA_FMForeachExpression::CXFA_FMForeachExpression( uint32_t line, const CFX_WideStringC& wsIdentifier, - CFX_ArrayTemplate* pAccessors, + std::vector>&& pAccessors, CXFA_FMExpression* pList) : CXFA_FMLoopExpression(line), m_wsIdentifier(wsIdentifier), - m_pAccessors(pAccessors), + m_pAccessors(std::move(pAccessors)), m_pList(pList) {} -CXFA_FMForeachExpression::~CXFA_FMForeachExpression() { - if (m_pAccessors) { - for (int i = 0; i < m_pAccessors->GetSize(); ++i) - m_pAccessors->GetAt(i); - - delete m_pAccessors; - } -} +CXFA_FMForeachExpression::~CXFA_FMForeachExpression() {} void CXFA_FMForeachExpression::ToJavaScript(CFX_WideTextBuf& javascript) { javascript << FX_WSTRC(L"{\n"); @@ -543,10 +536,10 @@ void CXFA_FMForeachExpression::ToJavaScript(CFX_WideTextBuf& javascript) { javascript << XFA_FM_EXPTypeToString(CONCATFMOBJECT); javascript << FX_WSTRC(L"("); - for (int i = 0; i < m_pAccessors->GetSize(); ++i) { - CXFA_FMSimpleExpression* s = m_pAccessors->GetAt(i); + for (size_t i = 0; i < m_pAccessors.size(); ++i) { + CXFA_FMSimpleExpression* s = m_pAccessors.at(i).get(); s->ToJavaScript(javascript); - if (i + 1 < m_pAccessors->GetSize()) { + if (i + 1 < m_pAccessors.size()) { javascript << FX_WSTRC(L", "); } } @@ -594,10 +587,10 @@ void CXFA_FMForeachExpression::ToImpliedReturnJS(CFX_WideTextBuf& javascript) { javascript << FX_WSTRC(L" = "); javascript << XFA_FM_EXPTypeToString(CONCATFMOBJECT); javascript << FX_WSTRC(L"("); - for (int i = 0; i < m_pAccessors->GetSize(); ++i) { - CXFA_FMSimpleExpression* s = m_pAccessors->GetAt(i); + for (size_t i = 0; i < m_pAccessors.size(); ++i) { + CXFA_FMSimpleExpression* s = m_pAccessors.at(i).get(); s->ToJavaScript(javascript); - if (i + 1 < m_pAccessors->GetSize()) { + if (i + 1 < m_pAccessors.size()) { javascript << FX_WSTRC(L", "); } } diff --git a/xfa/fxfa/fm2js/xfa_expression.h b/xfa/fxfa/fm2js/xfa_expression.h index 69337350bf..236b51a6b0 100644 --- a/xfa/fxfa/fm2js/xfa_expression.h +++ b/xfa/fxfa/fm2js/xfa_expression.h @@ -8,6 +8,7 @@ #define XFA_FXFA_FM2JS_XFA_EXPRESSION_H_ #include +#include #include "xfa/fxfa/fm2js/xfa_simpleexpression.h" @@ -197,7 +198,7 @@ class CXFA_FMForeachExpression : public CXFA_FMLoopExpression { CXFA_FMForeachExpression( uint32_t line, const CFX_WideStringC& wsIdentifier, - CFX_ArrayTemplate* pAccessors, + std::vector>&& pAccessors, CXFA_FMExpression* pList); ~CXFA_FMForeachExpression() override; @@ -206,7 +207,7 @@ class CXFA_FMForeachExpression : public CXFA_FMLoopExpression { private: CFX_WideStringC m_wsIdentifier; - CFX_ArrayTemplate* m_pAccessors; + std::vector> m_pAccessors; std::unique_ptr m_pList; }; diff --git a/xfa/fxfa/fm2js/xfa_fmparse.cpp b/xfa/fxfa/fm2js/xfa_fmparse.cpp index a383b2e04a..36663fce2d 100644 --- a/xfa/fxfa/fm2js/xfa_fmparse.cpp +++ b/xfa/fxfa/fm2js/xfa_fmparse.cpp @@ -8,6 +8,9 @@ #include #include +#include + +#include "third_party/base/ptr_util.h" CXFA_FMParse::CXFA_FMParse() : m_pToken(nullptr), m_pErrorInfo(0) {} @@ -989,7 +992,7 @@ CXFA_FMExpression* CXFA_FMParse::ParseForExpression() { CXFA_FMExpression* CXFA_FMParse::ParseForeachExpression() { std::unique_ptr e; CFX_WideStringC wsIdentifier; - std::unique_ptr> pAccessors; + std::vector> pAccessors; std::unique_ptr pList; uint32_t line = m_pToken->m_uLinenum; NextToken(); @@ -1008,17 +1011,13 @@ CXFA_FMExpression* CXFA_FMParse::ParseForeachExpression() { ws_TempString.c_str()); NextToken(); } else { - pAccessors.reset(new CFX_ArrayTemplate()); while (m_pToken->m_type != TOKrparen) { CXFA_FMSimpleExpression* s = ParseSimpleExpression(); - if (s) { - pAccessors->Add(s); - } - if (m_pToken->m_type == TOKcomma) { - NextToken(); - } else { + if (s) + pAccessors.push_back(pdfium::WrapUnique(s)); + if (m_pToken->m_type != TOKcomma) break; - } + NextToken(); } Check(TOKrparen); } @@ -1026,13 +1025,8 @@ CXFA_FMExpression* CXFA_FMParse::ParseForeachExpression() { pList.reset(ParseBlockExpression()); Check(TOKendfor); if (m_pErrorInfo->message.IsEmpty()) { - e.reset(new CXFA_FMForeachExpression( - line, wsIdentifier, pAccessors.release(), pList.release())); - } else { - if (pAccessors) { - for (int i = 0; i < pAccessors->GetSize(); ++i) - delete static_cast(pAccessors->GetAt(i)); - } + e = pdfium::MakeUnique( + line, wsIdentifier, std::move(pAccessors), pList.release()); } return e.release(); } -- cgit v1.2.3