summaryrefslogtreecommitdiff
path: root/sw
diff options
context:
space:
mode:
authorMike Kaganski <mike.kaganski@collabora.com>2023-12-18 17:44:41 +0300
committerXisco Fauli <xiscofauli@libreoffice.org>2023-12-19 09:37:43 +0100
commit64e371c6db51a2be53e0436b762fd96e84f49e04 (patch)
treec133be476df3b430e9a95ca0ec5b018504368c2c /sw
parente6bdd44e314907535db181d5f65342c1bfa7e3da (diff)
tdf#158703: return updated position from FnAddNonBrkSpace
The paragraph text may become shorter after the function succeeds, because it may remove arbitrary number of preceding spaces; since the position may then be used to access data in the string in the caller, we need to update it, to avoid use of old position (which may point beyond the string, and produce a crash; or it may point to a wrong position in it). Change-Id: Ib1b4b63cbd7150e0f69c97032e3410db7dadd4dc Reviewed-on: https://gerrit.libreoffice.org/c/core/+/160924 Tested-by: Jenkins Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com> (cherry picked from commit 1325d66a7f8657aaf9951a0664fbf14cca86905b) Reviewed-on: https://gerrit.libreoffice.org/c/core/+/160934 Reviewed-by: Xisco Fauli <xiscofauli@libreoffice.org>
Diffstat (limited to 'sw')
-rw-r--r--sw/qa/extras/uiwriter/data/tdf158703.fodt14
-rw-r--r--sw/qa/extras/uiwriter/uiwriter8.cxx15
-rw-r--r--sw/source/core/edit/autofmt.cxx7
3 files changed, 33 insertions, 3 deletions
diff --git a/sw/qa/extras/uiwriter/data/tdf158703.fodt b/sw/qa/extras/uiwriter/data/tdf158703.fodt
new file mode 100644
index 000000000000..5fc03d015f7e
--- /dev/null
+++ b/sw/qa/extras/uiwriter/data/tdf158703.fodt
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<office:document xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" office:version="1.3" office:mimetype="application/vnd.oasis.opendocument.text">
+ <office:automatic-styles>
+ <style:style style:name="P1" style:family="paragraph">
+ <style:text-properties fo:language="fr" fo:country="FR"/>
+ </style:style>
+ </office:automatic-styles>
+ <office:body>
+ <office:text>
+ <text:p text:style-name="P1">Foo <text:s text:c="5"/></text:p>
+ </office:text>
+ </office:body>
+</office:document> \ No newline at end of file
diff --git a/sw/qa/extras/uiwriter/uiwriter8.cxx b/sw/qa/extras/uiwriter/uiwriter8.cxx
index 641797b2c7b0..9aaafa1e6967 100644
--- a/sw/qa/extras/uiwriter/uiwriter8.cxx
+++ b/sw/qa/extras/uiwriter/uiwriter8.cxx
@@ -2844,6 +2844,21 @@ CPPUNIT_TEST_FIXTURE(SwUiWriterTest8, testTdf158459)
CPPUNIT_ASSERT_EQUAL(OUString("abdf"), pTextNode->GetText());
}
+CPPUNIT_TEST_FIXTURE(SwUiWriterTest8, testTdf158703)
+{
+ // Given a document with French text, consisting of a word and several spaces:
+ createSwDoc("tdf158703.fodt");
+ SwXTextDocument* pTextDoc = dynamic_cast<SwXTextDocument*>(mxComponent.get());
+ CPPUNIT_ASSERT(pTextDoc);
+
+ dispatchCommand(mxComponent, ".uno:GoToEndOfDoc", {});
+
+ // Typing ":" after the spaces should start auto-correction, which is expected to
+ // remove the spaces, and insert an NBSP instead. It must not crash.
+ emulateTyping(*pTextDoc, u":");
+ CPPUNIT_ASSERT_EQUAL(u"Foo\u00A0:"_ustr, getParagraph(1)->getString());
+}
+
CPPUNIT_PLUGIN_IMPLEMENT();
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sw/source/core/edit/autofmt.cxx b/sw/source/core/edit/autofmt.cxx
index b569ca389ed8..6fafe1310d72 100644
--- a/sw/source/core/edit/autofmt.cxx
+++ b/sw/source/core/edit/autofmt.cxx
@@ -2113,9 +2113,9 @@ void SwAutoFormat::AutoCorrect(TextFrameIndex nPos)
: LANGUAGE_SYSTEM;
SetRedlineText( STR_AUTOFMTREDL_NON_BREAK_SPACE );
- if (pATst->FnAddNonBrkSpace(aACorrDoc, *pText, sal_Int32(nPos), eLang, bNbspRunNext))
+ if (sal_Int32 nUpdatedPos = pATst->FnAddNonBrkSpace(aACorrDoc, *pText, sal_Int32(nPos), eLang, bNbspRunNext); nUpdatedPos >= 0)
{
- --nPos;
+ nPos = TextFrameIndex(nUpdatedPos);
break;
}
}
@@ -2233,7 +2233,8 @@ void SwAutoFormat::AutoCorrect(TextFrameIndex nPos)
if ( m_aFlags.bAddNonBrkSpace && nPos < TextFrameIndex(pText->getLength()) )
{
SetRedlineText( STR_AUTOFMTREDL_NON_BREAK_SPACE );
- pATst->FnAddNonBrkSpace(aACorrDoc, *pText, sal_Int32(nPos), eLang, bNbspRunNext);
+ if (sal_Int32 nUpdatedPos = pATst->FnAddNonBrkSpace(aACorrDoc, *pText, sal_Int32(nPos), eLang, bNbspRunNext); nUpdatedPos >= 0)
+ nPos = TextFrameIndex(nUpdatedPos);
}
if( ( m_aFlags.bChgOrdinalNumber &&