Rebased ref, commits from common ancestor:
commit 376f0143d875ef39550f916f209a3a923342c269
Author:     Michael Stahl <michael.st...@allotropia.de>
AuthorDate: Thu Apr 14 18:33:21 2022 +0200
Commit:     Samuel Mehrbrodt <samuel.mehrbr...@allotropia.de>
CommitDate: Tue Apr 19 14:16:51 2022 +0200

    sw: check if cursor position is valid before inserting file
    
    SwXTextCursor::insertDocumentFromURL() must not insert the file inside
    of an input field.
    
    If the CH_TXT_ATR_INPUTFIELD* become separated to different text nodes,
    things are going to break.
    
    Change-Id: Ia170e63f6c6d8a8fdd18f0b91e2b333e660ed924

diff --git a/sw/qa/extras/uiwriter/uiwriter.cxx 
b/sw/qa/extras/uiwriter/uiwriter.cxx
index d11cf4e2d3f8..7f81222766bf 100755
--- a/sw/qa/extras/uiwriter/uiwriter.cxx
+++ b/sw/qa/extras/uiwriter/uiwriter.cxx
@@ -47,6 +47,7 @@
 #include <AnnotationWin.hxx>
 #include "com/sun/star/text/XDefaultNumberingProvider.hpp"
 #include "com/sun/star/awt/FontUnderline.hpp"
+#include <com/sun/star/document/XDocumentInsertable.hpp>
 
 #include <svx/svdpage.hxx>
 #include <svx/svdview.hxx>
@@ -129,6 +130,7 @@ public:
     void testDOCXAutoTextGallery();
     void testWatermarkDOCX();
     void testTdf67238();
+    void testInsertFileInInputFieldException();
     void testFdo75110();
     void testFdo75898();
     void testFdo74981();
@@ -278,6 +280,7 @@ public:
     CPPUNIT_TEST(testDOCXAutoTextGallery);
     CPPUNIT_TEST(testWatermarkDOCX);
     CPPUNIT_TEST(testTdf67238);
+    CPPUNIT_TEST(testInsertFileInInputFieldException);
     CPPUNIT_TEST(testFdo75110);
     CPPUNIT_TEST(testFdo75898);
     CPPUNIT_TEST(testFdo74981);
@@ -559,6 +562,24 @@ void SwUiWriterTest::testBookmarkCopy()
     }
 }
 
+void SwUiWriterTest::testInsertFileInInputFieldException()
+{
+    createDoc();
+    uno::Reference<text::XTextDocument> const xTextDoc(mxComponent, 
uno::UNO_QUERY);
+    uno::Reference<text::XText> const xBody(xTextDoc->getText());
+    uno::Reference<lang::XMultiServiceFactory> const xFactory(mxComponent, 
uno::UNO_QUERY);
+    uno::Reference<text::XTextCursor> const xCursor(xBody->createTextCursor());
+    uno::Reference<document::XDocumentInsertable> const xInsertable(xCursor, 
uno::UNO_QUERY);
+    uno::Reference<text::XTextContent> const xContent(
+        xFactory->createInstance("com.sun.star.text.textfield.Input"), 
uno::UNO_QUERY);
+    xBody->insertTextContent(xCursor, xContent, false);
+    xCursor->goLeft(1, false);
+    // try to insert some random file
+    OUString const url(m_directories.getURLFromSrc(DATA_DIRECTORY) + 
"fdo75110.odt");
+    // inserting even asserts in debug builds - document model goes invalid 
with input field split across 2 nodes
+    CPPUNIT_ASSERT_THROW(xInsertable->insertDocumentFromURL(url, {}), 
uno::RuntimeException);
+}
+
 void SwUiWriterTest::testTdf67238()
 {
     //create a new writer document
diff --git a/sw/source/core/unocore/unocrsrhelper.cxx 
b/sw/source/core/unocore/unocrsrhelper.cxx
index 547598423ab7..4dc38b347e8f 100644
--- a/sw/source/core/unocore/unocrsrhelper.cxx
+++ b/sw/source/core/unocore/unocrsrhelper.cxx
@@ -955,6 +955,16 @@ void resetCursorPropertyValue(const 
SfxItemPropertySimpleEntry& rEntry, SwPaM& r
 void InsertFile(SwUnoCursor* pUnoCursor, const OUString& rURL,
     const uno::Sequence< beans::PropertyValue >& rOptions)
 {
+    if (SwTextNode const*const pTextNode = 
pUnoCursor->GetPoint()->nNode.GetNode().GetTextNode())
+    {
+        // TODO: check meta field here too in case it ever grows a 2nd char
+        if 
(pTextNode->GetTextAttrAt(pUnoCursor->GetPoint()->nContent.GetIndex(),
+                    RES_TXTATR_INPUTFIELD, SwTextNode::PARENT))
+        {
+            throw uno::RuntimeException("cannot insert file inside input 
field");
+        }
+    }
+
     std::unique_ptr<SfxMedium> pMed;
     SwDoc* pDoc = pUnoCursor->GetDoc();
     SwDocShell* pDocSh = pDoc->GetDocShell();

Reply via email to