sc/source/filter/excel/xiname.cxx |    9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

New commits:
commit 1b3963b93866680621016860276f53ae428ec6ad
Author:     Caolán McNamara <caol...@redhat.com>
AuthorDate: Sun Jan 29 20:59:44 2023 +0000
Commit:     Xisco Fauli <xiscofa...@libreoffice.org>
CommitDate: Tue Jun 27 22:37:57 2023 +0200

    crashtesting: crash in ReadQsiforum-mso-en4-30276.xls
    
    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 <caol...@redhat.com>
    (cherry picked from commit ea093514e4fc3a66f57e07486863c22e32db4245)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/153380
    Reviewed-by: Xisco Fauli <xiscofa...@libreoffice.org>

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 )

Reply via email to