sw/qa/core/data/ww8/pass/ofz53457-1.doc |binary
 sw/source/filter/ww8/ww8par5.cxx        |   19 +++++++++++++++++--
 2 files changed, 17 insertions(+), 2 deletions(-)

New commits:
commit f44f5f12605f32d92c2c1689e3e6888337eca2d9
Author:     Caolán McNamara <caol...@redhat.com>
AuthorDate: Thu Nov 17 13:15:27 2022 +0000
Commit:     Caolán McNamara <caol...@redhat.com>
CommitDate: Thu Nov 17 17:12:48 2022 +0100

    ofz#53457 sanity check the form field range
    
    Change-Id: I2a1c82bb0590acf8f2399f2ea4b6b477600c7908
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/142840
    Tested-by: Jenkins
    Reviewed-by: Caolán McNamara <caol...@redhat.com>

diff --git a/sw/qa/core/data/ww8/pass/ofz53457-1.doc 
b/sw/qa/core/data/ww8/pass/ofz53457-1.doc
new file mode 100644
index 000000000000..b847eedbbdef
Binary files /dev/null and b/sw/qa/core/data/ww8/pass/ofz53457-1.doc differ
diff --git a/sw/source/filter/ww8/ww8par5.cxx b/sw/source/filter/ww8/ww8par5.cxx
index 513ee98e0edb..7e7cedd3f4b5 100644
--- a/sw/source/filter/ww8/ww8par5.cxx
+++ b/sw/source/filter/ww8/ww8par5.cxx
@@ -503,6 +503,20 @@ void SwWW8ImplReader::UpdateFields()
     m_rDoc.SetInitDBFields(true);             // Also update fields in the 
database
 }
 
+// Sanity check the PaM to see if it makes sense wrt sw::CalcBreaks
+static bool SanityCheck(const SwPaM& rFieldPam)
+{
+    SwNodeOffset const nEndNode(rFieldPam.End()->GetNodeIndex());
+    SwNodes const& rNodes(rFieldPam.GetPoint()->GetNodes());
+    SwNode *const pFinalNode(rNodes[nEndNode]);
+    if (pFinalNode->IsTextNode())
+    {
+        SwTextNode & rTextNode(*pFinalNode->GetTextNode());
+        return (rTextNode.Len() >= rFieldPam.End()->GetContentIndex());
+    }
+    return true;
+}
+
 sal_uInt16 SwWW8ImplReader::End_Field()
 {
     sal_uInt16 nRet = 0;
@@ -530,10 +544,11 @@ sal_uInt16 SwWW8ImplReader::End_Field()
         if (bUseEnhFields && m_pPaM!=nullptr && m_pPaM->GetPoint()!=nullptr) {
             SwPosition aEndPos = *m_pPaM->GetPoint();
             SwPaM aFieldPam( m_aFieldStack.back().GetPtNode().GetNode(), 
m_aFieldStack.back().GetPtContent(), aEndPos.GetNode(), 
aEndPos.GetContentIndex());
+
             IDocumentMarkAccess* pMarksAccess = m_rDoc.getIDocumentMarkAccess( 
);
-            IFieldmark *pFieldmark = pMarksAccess->makeFieldBookmark(
+            IFieldmark *pFieldmark = SanityCheck(aFieldPam) ? 
pMarksAccess->makeFieldBookmark(
                         aFieldPam, m_aFieldStack.back().GetBookmarkName(), 
ODF_FORMTEXT,
-                        aFieldPam.Start() /*same pos as start!*/ );
+                        aFieldPam.Start() /*same pos as start!*/ ) : nullptr;
             OSL_ENSURE(pFieldmark!=nullptr, "hmmm; why was the bookmark not 
created?");
             if (pFieldmark!=nullptr) {
                 // adapt redline positions to inserted field mark start

Reply via email to