From ea093514e4fc3a66f57e07486863c22e32db4245 Mon Sep 17 00:00:00 2001 From: Caolán McNamara Date: Sun, 29 Jan 2023 20:59:44 +0000 Subject: crashtesting: crash in ReadQsiforum-mso-en4-30276.xls MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit on duplicate names where during import an existing ScRangeData using the name is deleted on inserting a new one, leaving a XclImpName behind that still references the deleted ScRangeData. Reverse the order of lookup of XclImpName so we find the duplicate that references the valid ScRangeData first. likely also forums/xls/forum-mso-en4-69844.xls forums/xls/forum-mso-en4-69589.xls forums/xls/forum-mso-en4-69308.xls see also: commit 657b3c889ae107d9ccaaab569929a3a1abde3200 Date: Sat Jan 21 00:08:29 2012 -0500 fdo#44831: Named range should overwrite existing name. When inserting a new named range, it should overwrite any existing name if one exists. Change-Id: I275663cacc34a2b85080c038dc5a199563f3547c Reviewed-on: https://gerrit.libreoffice.org/c/core/+/146310 Tested-by: Jenkins Reviewed-by: Caolán McNamara --- sc/source/filter/excel/xiname.cxx | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'sc') diff --git a/sc/source/filter/excel/xiname.cxx b/sc/source/filter/excel/xiname.cxx index d498dfba492c..cd9e92dfaa93 100644 --- a/sc/source/filter/excel/xiname.cxx +++ b/sc/source/filter/excel/xiname.cxx @@ -291,8 +291,15 @@ const XclImpName* XclImpNameManager::FindName( std::u16string_view rXclName, SCT { const XclImpName* pGlobalName = nullptr; // a found global name const XclImpName* pLocalName = nullptr; // a found local name - for( const auto& rxName : maNameList ) + // If a duplicate name is seen by ScRangeName::insert then the existing + // name is erased and the new one inserted, so in the case of duplicates + // the last one seen is valid and the others invalid. So do this lookup in + // reverse in order to return the XclImpName* that references the valid + // entry (see tdf#44831 for the insert behavior and 'forum-mso-en4-30276.xls' + // for an example of this problem) + for (auto itName = maNameList.rbegin(); itName != maNameList.rend(); ++itName) { + const auto& rxName = *itName; if( rxName->GetXclName() == rXclName ) { if( rxName->GetScTab() == nScTab ) -- cgit