diff options
author | Lei Zhang <thestig@chromium.org> | 2018-08-01 08:51:57 +0000 |
---|---|---|
committer | Chromium commit bot <commit-bot@chromium.org> | 2018-08-01 08:51:57 +0000 |
commit | 3a40bed9d6e573f2c280bf7581249cb2d9163771 (patch) | |
tree | 6916f4fa1896cdba3a683652af1649516173015c | |
parent | 3aec5705db19e0a832660329b05b736ead426022 (diff) | |
download | pdfium-3a40bed9d6e573f2c280bf7581249cb2d9163771.tar.xz |
Add a ScopedLocale for use within CXFA_LocaleValue.
Add more checks for |m_pLocaleMgr| before dereferencing it.
Change-Id: I39ab44d652364f5530266d2b724fa6703d1b51f1
Reviewed-on: https://pdfium-review.googlesource.com/39114
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Ryan Harrison <rharrison@chromium.org>
-rw-r--r-- | xfa/fxfa/parser/cxfa_localevalue.cpp | 55 |
1 files changed, 35 insertions, 20 deletions
diff --git a/xfa/fxfa/parser/cxfa_localevalue.cpp b/xfa/fxfa/parser/cxfa_localevalue.cpp index c201f88ef2..ef6c685adf 100644 --- a/xfa/fxfa/parser/cxfa_localevalue.cpp +++ b/xfa/fxfa/parser/cxfa_localevalue.cpp @@ -62,6 +62,30 @@ bool ValueSplitDateTime(const WideString& wsDateTime, return true; } +class ScopedLocale { + public: + ScopedLocale(CXFA_LocaleMgr* pLocaleMgr, LocaleIface* pNewLocale) + : m_pLocaleMgr(pLocaleMgr), + m_pNewLocale(pNewLocale), + m_pOrigLocale(pNewLocale ? m_pLocaleMgr->GetDefLocale() : nullptr) { + if (m_pNewLocale) + m_pLocaleMgr->SetDefLocale(pNewLocale); + } + + ~ScopedLocale() { + if (m_pNewLocale) + m_pLocaleMgr->SetDefLocale(m_pOrigLocale); + } + + ScopedLocale(const ScopedLocale& that) = delete; + ScopedLocale& operator=(const ScopedLocale& that) = delete; + + private: + UnownedPtr<CXFA_LocaleMgr> const m_pLocaleMgr; + LocaleIface* const m_pNewLocale; + LocaleIface* const m_pOrigLocale; +}; + } // namespace CXFA_LocaleValue::CXFA_LocaleValue() = default; @@ -103,9 +127,7 @@ bool CXFA_LocaleValue::ValidateValue(const WideString& wsValue, return false; WideString wsOutput; - LocaleIface* locale = m_pLocaleMgr->GetDefLocale(); - if (pLocale) - m_pLocaleMgr->SetDefLocale(pLocale); + ScopedLocale scoped_locale(m_pLocaleMgr.Get(), pLocale); auto pFormat = pdfium::MakeUnique<CFGAS_FormatString>(m_pLocaleMgr.Get()); std::vector<WideString> wsPatterns; @@ -179,9 +201,6 @@ bool CXFA_LocaleValue::ValidateValue(const WideString& wsValue, } if (bRet && pMatchFormat) *pMatchFormat = wsPatterns[i - 1]; - if (pLocale) - m_pLocaleMgr->SetDefLocale(locale); - return bRet; } @@ -318,9 +337,10 @@ bool CXFA_LocaleValue::FormatSinglePattern(WideString& wsResult, const WideString& wsFormat, LocaleIface* pLocale, XFA_VALUEPICTURE eValueType) const { - LocaleIface* locale = m_pLocaleMgr->GetDefLocale(); - if (pLocale) - m_pLocaleMgr->SetDefLocale(pLocale); + if (!m_pLocaleMgr) + return false; + + ScopedLocale scoped_locale(m_pLocaleMgr.Get(), pLocale); wsResult.clear(); bool bRet = false; @@ -362,8 +382,6 @@ bool CXFA_LocaleValue::FormatSinglePattern(WideString& wsResult, eValueType != XFA_VALUEPICTURE_Display)) { wsResult = m_wsValue; } - if (pLocale) - m_pLocaleMgr->SetDefLocale(locale); return bRet; } @@ -585,17 +603,17 @@ bool CXFA_LocaleValue::ValidateCanonicalTime(const WideString& wsTime) { bool CXFA_LocaleValue::ParsePatternValue(const WideString& wsValue, const WideString& wsPattern, LocaleIface* pLocale) { - LocaleIface* locale = m_pLocaleMgr->GetDefLocale(); - if (pLocale) - m_pLocaleMgr->SetDefLocale(pLocale); + if (!m_pLocaleMgr) + return false; + + ScopedLocale scoped_locale(m_pLocaleMgr.Get(), pLocale); auto pFormat = pdfium::MakeUnique<CFGAS_FormatString>(m_pLocaleMgr.Get()); std::vector<WideString> wsPatterns; pFormat->SplitFormatString(wsPattern, &wsPatterns); bool bRet = false; - int32_t iCount = pdfium::CollectionSize<int32_t>(wsPatterns); - for (int32_t i = 0; i < iCount && !bRet; i++) { - WideString wsFormat = wsPatterns[i]; + for (size_t i = 0; !bRet && i < wsPatterns.size(); i++) { + const WideString& wsFormat = wsPatterns[i]; switch (ValueCategory(pFormat->GetCategory(wsFormat), m_dwType)) { case FX_LOCALECATEGORY_Null: bRet = pFormat->ParseNull(wsValue, wsFormat); @@ -653,9 +671,6 @@ bool CXFA_LocaleValue::ParsePatternValue(const WideString& wsValue, if (!bRet) m_wsValue = wsValue; - if (pLocale) - m_pLocaleMgr->SetDefLocale(locale); - return bRet; } |