sw/qa/extras/tiledrendering/data/cond-coll-copy.odt |binary
 sw/qa/extras/tiledrendering/tiledrendering.cxx      |   26 ++++++++++++++++++++
 sw/source/core/doc/docfmt.cxx                       |    9 ++++++
 3 files changed, 35 insertions(+)

New commits:
commit 626efef843f673c6ef2b169da5d42b07ea8a42a4
Author:     Miklos Vajna <vmik...@collabora.com>
AuthorDate: Fri Apr 9 16:40:01 2021 +0200
Commit:     Miklos Vajna <vmik...@collabora.com>
CommitDate: Mon Apr 12 09:33:53 2021 +0200

    sw style copy: fix crash when handling a conditional paragraphy style
    
    - "Text body" is normally a conditional style for a full-blown, default
      SwDoc
    
    - SwTransferable::GetData() creates a temporary, stripped down SwDoc,
      which has a "Text body" style, but it's not conditional
    
    - SwDoc::CopyFormatArr() assumes that in case the target already has a
      style with a given name, then either both the source and destination
      styles are conditional, or neither
    
    - in practice this only causes a crash if the style is customized, as we
      skip default styles, probably that's why this was not noticed so far
    
    The Online case invokes this as part of
    lok::Document::getSelectionType(), so it was enough to set the paragraph
    style to Text body and then select some of that text (with a suitable
    document) to hit this.
    
    Based on a patch from Michael Meeks, thanks for that.
    
    (cherry picked from commit ce98bef935dccd79735615a9299b2aa7a1ab0b94)
    
    Change-Id: Ic3c27ec582ae1745469042856254313cbc996ee0

diff --git a/sw/qa/extras/tiledrendering/data/cond-coll-copy.odt 
b/sw/qa/extras/tiledrendering/data/cond-coll-copy.odt
new file mode 100644
index 000000000000..8fa15f177eb7
Binary files /dev/null and 
b/sw/qa/extras/tiledrendering/data/cond-coll-copy.odt differ
diff --git a/sw/qa/extras/tiledrendering/tiledrendering.cxx 
b/sw/qa/extras/tiledrendering/tiledrendering.cxx
index 193d45135c8c..a0aea532d7c1 100644
--- a/sw/qa/extras/tiledrendering/tiledrendering.cxx
+++ b/sw/qa/extras/tiledrendering/tiledrendering.cxx
@@ -18,6 +18,7 @@
 #include <com/sun/star/frame/XStorable.hpp>
 #include <com/sun/star/frame/Desktop.hpp>
 #include <com/sun/star/text/XTextViewCursorSupplier.hpp>
+#include <com/sun/star/datatransfer/XTransferable2.hpp>
 
 #include <test/helper/transferable.hxx>
 #include <LibreOfficeKit/LibreOfficeKitEnums.h>
@@ -45,6 +46,7 @@
 #include <tools/json_writer.hxx>
 #include <unotools/mediadescriptor.hxx>
 #include <comphelper/processfactory.hxx>
+#include <comphelper/propertyvalue.hxx>
 
 #include <drawdoc.hxx>
 #include <ndtxt.hxx>
@@ -152,6 +154,7 @@ public:
     void testBulletDeleteInvalidation();
     void testBulletNoNumInvalidation();
     void testBulletMultiDeleteInvalidation();
+    void testCondCollCopy();
 
     CPPUNIT_TEST_SUITE(SwTiledRenderingTest);
     CPPUNIT_TEST(testRegisterCallback);
@@ -229,6 +232,7 @@ public:
     CPPUNIT_TEST(testBulletDeleteInvalidation);
     CPPUNIT_TEST(testBulletNoNumInvalidation);
     CPPUNIT_TEST(testBulletMultiDeleteInvalidation);
+    CPPUNIT_TEST(testCondCollCopy);
     CPPUNIT_TEST_SUITE_END();
 
 private:
@@ -3042,6 +3046,28 @@ void 
SwTiledRenderingTest::testBulletMultiDeleteInvalidation()
     CPPUNIT_ASSERT(!aFirstTextRect.IsOver(m_aInvalidations));
 }
 
+void SwTiledRenderingTest::testCondCollCopy()
+{
+    // Given a document with a custom Text Body cond style:
+    SwXTextDocument* pXTextDocument = createDoc("cond-coll-copy.odt");
+    uno::Sequence<beans::PropertyValue> aPropertyValues
+        = { comphelper::makePropertyValue("Style", OUString("Text Body")),
+            comphelper::makePropertyValue("FamilyName", 
OUString("ParagraphStyles")) };
+    dispatchCommand(mxComponent, ".uno:StyleApply", aPropertyValues);
+    SwWrtShell* pWrtShell = pXTextDocument->GetDocShell()->GetWrtShell();
+    pWrtShell->SelAll();
+
+    // When getting the text selection, then make sure it doesn't crash:
+    uno::Reference<datatransfer::XTransferable2> 
xTransferable(pXTextDocument->getSelection(),
+                                                               
css::uno::UNO_QUERY);
+    datatransfer::DataFlavor aFlavor;
+    aFlavor.MimeType = "text/plain;charset=utf-16";
+    aFlavor.DataType = cppu::UnoType<OUString>::get();
+    CPPUNIT_ASSERT(xTransferable->isDataFlavorSupported(aFlavor));
+    // Without the accompanying fix in place, this test would have crashed.
+    xTransferable->getTransferData(aFlavor);
+}
+
 CPPUNIT_TEST_SUITE_REGISTRATION(SwTiledRenderingTest);
 
 CPPUNIT_PLUGIN_IMPLEMENT();
diff --git a/sw/source/core/doc/docfmt.cxx b/sw/source/core/doc/docfmt.cxx
index a27aad094ce2..3709956d20be 100644
--- a/sw/source/core/doc/docfmt.cxx
+++ b/sw/source/core/doc/docfmt.cxx
@@ -1337,9 +1337,18 @@ void SwDoc::CopyFormatArr( const SwFormatsBase& 
rSourceArr,
 
 //FEATURE::CONDCOLL
             if( RES_CONDTXTFMTCOLL == pSrc->Which() )
+            {
+                if (pDstColl->Which() != RES_CONDTXTFMTCOLL)
+                {
+                    // Target already had a style with a matching name, but 
it's not a conditional
+                    // style, then don't copy the conditions.
+                    continue;
+                }
+
                 // Copy the conditions, but delete the old ones first!
                 
static_cast<SwConditionTextFormatColl*>(pDstColl)->SetConditions(
                             
static_cast<SwConditionTextFormatColl*>(pSrc)->GetCondColls() );
+            }
 //FEATURE::CONDCOLL
         }
     }
_______________________________________________
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits

Reply via email to