From 161f992a81e400eeebba49387f174d836750d624 Mon Sep 17 00:00:00 2001 From: Ryan Harrison Date: Wed, 7 Feb 2018 21:01:44 +0000 Subject: Handle removed fonts correctly in GetFontByCodePage The existing code has a couple of issues that need to be addressed. First it assumes that for a hash, there will be an entry in the map and blindly calls the [] operator and takes the address of the result. If there isn't an entry for the hash then this will cause a crash. This has been converted to a call to find and returning nullptr, which is the fail result, if it cannot find an entry for the hash. The other issue is that it assumed that the first entry in the vector would be a valid pointer. When removing fonts from the vector, RemoveFont, first nulls out entries. Once all of the entries have been removed from a vector on subsequent calls to RemoveFont, then the vector is removed from the map. Thus the first entry in the vector might not be the correct value to return. This has been changed to a linear scan of the vector for a valid pointer. BUG=chromium:648177 Change-Id: Ife758636545f0d10fb726c243e3e0a5b7c1d1138 Reviewed-on: https://pdfium-review.googlesource.com/25930 Commit-Queue: Ryan Harrison Reviewed-by: dsinclair --- xfa/fgas/font/cfgas_fontmgr.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/xfa/fgas/font/cfgas_fontmgr.cpp b/xfa/fgas/font/cfgas_fontmgr.cpp index 1731079bdd..99120a0d51 100644 --- a/xfa/fgas/font/cfgas_fontmgr.cpp +++ b/xfa/fgas/font/cfgas_fontmgr.cpp @@ -951,9 +951,14 @@ RetainPtr CFGAS_FontMgr::GetFontByCodePage( ByteString bsHash = ByteString::Format("%d, %d", wCodePage, dwFontStyles); bsHash += FX_UTF8Encode(WideStringView(pszFontFamily)); uint32_t dwHash = FX_HashCode_GetA(bsHash.AsStringView(), false); - std::vector>* pFontArray = &m_Hash2Fonts[dwHash]; - if (!pFontArray->empty()) - return (*pFontArray)[0]; + auto* pFontVector = &m_Hash2Fonts[dwHash]; + if (!pFontVector->empty()) { + for (auto iter = pFontVector->begin(); iter != pFontVector->end(); ++iter) { + if (*iter != nullptr) + return *iter; + } + return nullptr; + } #if _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ const FX_FONTDESCRIPTOR* pFD = @@ -989,7 +994,7 @@ RetainPtr CFGAS_FontMgr::GetFontByCodePage( return nullptr; pFont->SetLogicalFontStyle(dwFontStyles); - pFontArray->push_back(pFont); + pFontVector->push_back(pFont); return pFont; } -- cgit v1.2.3