sw/qa/extras/uiwriter/data/text-with-formula-one-paragraph.fodt | 32 ++++++++++ sw/qa/extras/uiwriter/uiwriter9.cxx | 12 +++ sw/source/core/layout/anchoredobject.cxx | 11 ++- sw/source/core/text/txtfrm.cxx | 7 +- 4 files changed, 58 insertions(+), 4 deletions(-)
New commits: commit cab85b8cc7806d4f1ac2191b2e4d78a38f75812c Author: Mike Kaganski <mike.kagan...@collabora.com> AuthorDate: Thu Jun 26 20:00:16 2025 +0500 Commit: Xisco Fauli <xiscofa...@libreoffice.org> CommitDate: Fri Jun 27 13:03:50 2025 +0200 tdf#167133: Be prepared that frame's object's anchor may be null ... between the time the object was removed from the model, and the layout update. Change-Id: Ieb2f42c18794090cdc8786d5cffd9ae6e2b8f147 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/187071 Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com> Tested-by: Jenkins Signed-off-by: Xisco Fauli <xiscofa...@libreoffice.org> Reviewed-on: https://gerrit.libreoffice.org/c/core/+/187087 diff --git a/sw/qa/extras/uiwriter/data/text-with-formula-one-paragraph.fodt b/sw/qa/extras/uiwriter/data/text-with-formula-one-paragraph.fodt new file mode 100644 index 000000000000..a422453f0cee --- /dev/null +++ b/sw/qa/extras/uiwriter/data/text-with-formula-one-paragraph.fodt @@ -0,0 +1,32 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<office:document xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" office:version="1.4" office:mimetype="application/vnd.oasis.opendocument.text"> + <office:body> + <office:text> + <text:p>Paragraph one: <draw:frame text:anchor-type="as-char" svg:width="15.54mm" svg:height="5.31mm"> + <draw:object> + <math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> + <semantics> + <mrow> + <mrow> + <msup> + <mi mathvariant="normal">e</mi> + <mrow> + <mi>i</mi> + <mi>π</mi> + </mrow> + </msup> + <mo stretchy="false">+</mo> + <mn>1</mn> + </mrow> + <mo stretchy="false">=</mo> + <mn>0</mn> + </mrow> + <annotation encoding="StarMath 5.0">{func e}^{i %pi} + 1 = 0</annotation> + </semantics> + </math> + </draw:object> + </draw:frame>.</text:p> + </office:text> + </office:body> +</office:document> \ No newline at end of file diff --git a/sw/qa/extras/uiwriter/uiwriter9.cxx b/sw/qa/extras/uiwriter/uiwriter9.cxx index 9d1adeca8b9f..400a4c8cf3b2 100644 --- a/sw/qa/extras/uiwriter/uiwriter9.cxx +++ b/sw/qa/extras/uiwriter/uiwriter9.cxx @@ -579,6 +579,18 @@ CPPUNIT_TEST_FIXTURE(SwUiWriterTest9, testTdf151710) CPPUNIT_ASSERT_EQUAL(sStartDoubleQuote, xTextDocument->getText()->getString()); } +CPPUNIT_TEST_FIXTURE(SwUiWriterTest9, testTdf167133) +{ + // Given a document with a single paragraph, having a formula object + createSwDoc("text-with-formula-one-paragraph.fodt"); + dispatchCommand(mxComponent, u".uno:SelectAll"_ustr, {}); + SwDoc* pDoc = getSwDoc(); + SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell(); + pDoc->getIDocumentContentOperations().ReplaceRange(*pWrtShell->GetCursor(), "", false); + Scheduler::ProcessEventsToIdle(); + // This must not crash! +} + CPPUNIT_TEST_FIXTURE(SwUiWriterTest9, testTdf159054_disableOutlineNumbering) { createSwDoc("tdf159054_disableOutlineNumbering.docx"); diff --git a/sw/source/core/layout/anchoredobject.cxx b/sw/source/core/layout/anchoredobject.cxx index 8f10719cdf9c..f60969613ba7 100644 --- a/sw/source/core/layout/anchoredobject.cxx +++ b/sw/source/core/layout/anchoredobject.cxx @@ -725,9 +725,14 @@ SwTextFrame* SwAnchoredObject::FindAnchorCharFrame() if ((rAnch.GetAnchorId() == RndStdIds::FLY_AT_CHAR) || (rAnch.GetAnchorId() == RndStdIds::FLY_AS_CHAR)) { - SwTextFrame* const pFrame(static_cast<SwTextFrame*>(AnchorFrame())); - TextFrameIndex const nOffset(pFrame->MapModelToViewPos(*rAnch.GetContentAnchor())); - pAnchorCharFrame = &pFrame->GetFrameAtOfst(nOffset); + // When the object was already removed from text, but the layout hasn't been + // updated yet, this can be nullptr: + if (const SwPosition* pContentAnchor = rAnch.GetContentAnchor()) + { + SwTextFrame* const pFrame(static_cast<SwTextFrame*>(AnchorFrame())); + TextFrameIndex const nOffset(pFrame->MapModelToViewPos(*pContentAnchor)); + pAnchorCharFrame = &pFrame->GetFrameAtOfst(nOffset); + } } else if (SwFlyFrame* pFlyFrame = DynCastFlyFrame()) { diff --git a/sw/source/core/text/txtfrm.cxx b/sw/source/core/text/txtfrm.cxx index 9fc7a6605b61..bea6d81e3b44 100644 --- a/sw/source/core/text/txtfrm.cxx +++ b/sw/source/core/text/txtfrm.cxx @@ -1802,8 +1802,13 @@ void SwTextFrame::HideAndShowObjects() sal_Int32 nHiddenStart; sal_Int32 nHiddenEnd; const SwFormatAnchor& rAnchorFormat = pContact->GetAnchorFormat(); + const SwNode* pNode = rAnchorFormat.GetAnchorNode(); + // When the object was already removed from text, but the layout hasn't been + // updated yet, this can be nullptr: + if (!pNode) + continue; SwScriptInfo::GetBoundsOfHiddenRange( - *rAnchorFormat.GetAnchorNode()->GetTextNode(), + *pNode->GetTextNode(), rAnchorFormat.GetAnchorContentOffset(), nHiddenStart, nHiddenEnd); // Under certain conditions if ( nHiddenStart != COMPLETE_STRING && bShouldBeHidden &&