diff options
author | Mike Kaganski <mike.kaganski@collabora.com> | 2023-12-18 17:44:41 +0300 |
---|---|---|
committer | Mike Kaganski <mike.kaganski@collabora.com> | 2023-12-18 18:45:51 +0100 |
commit | 1325d66a7f8657aaf9951a0664fbf14cca86905b (patch) | |
tree | ced0622b332d931f520776ac82fecc587ba3be0f /editeng | |
parent | d05a74234d5ab6b029618281908552066fe04fa4 (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>
Diffstat (limited to 'editeng')
-rw-r--r-- | editeng/source/misc/svxacorr.cxx | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/editeng/source/misc/svxacorr.cxx b/editeng/source/misc/svxacorr.cxx index f6941d6cc371..ff2c4518aadd 100644 --- a/editeng/source/misc/svxacorr.cxx +++ b/editeng/source/misc/svxacorr.cxx @@ -667,12 +667,12 @@ bool SvxAutoCorrect::FnChgToEnEmDash( } // Add non-breaking space before specific punctuation marks in French text -bool SvxAutoCorrect::FnAddNonBrkSpace( +sal_Int32 SvxAutoCorrect::FnAddNonBrkSpace( SvxAutoCorrDoc& rDoc, std::u16string_view rTxt, sal_Int32 nEndPos, LanguageType eLang, bool& io_bNbspRunNext ) { - bool bRet = false; + sal_Int32 nRet = -1; CharClass& rCC = GetCharClass( eLang ); @@ -706,7 +706,7 @@ bool SvxAutoCorrect::FnAddNonBrkSpace( if (nIndex + nProtocolLen <= rTxt.size()) { if (INetURLObject::CompareProtocolScheme(rTxt.substr(nIndex, nProtocolLen)) != INetProtocol::NotValid) - return false; + return -1; } // Check the presence of "://" in the word @@ -734,7 +734,7 @@ bool SvxAutoCorrect::FnAddNonBrkSpace( if ( bHasSpace ) rDoc.Insert( nPos, OUString(cNonBreakingSpace) ); io_bNbspRunNext = true; - bRet = true; + nRet = nPos; } else if ( chars.indexOf( cPrevChar ) != -1 ) io_bNbspRunNext = true; @@ -748,12 +748,12 @@ bool SvxAutoCorrect::FnAddNonBrkSpace( if ( cPrevChar == ':' && cMaybeSpaceChar == cNonBreakingSpace ) { rDoc.Delete( nEndPos - 2, nEndPos - 1 ); - bRet = true; + nRet = nEndPos - 1; } } } - return bRet; + return nRet; } // URL recognition @@ -1501,10 +1501,14 @@ void SvxAutoCorrect::DoAutoCorrect( SvxAutoCorrDoc& rDoc, const OUString& rTxt, // Hardspaces autocorrection if ( IsAutoCorrFlag( ACFlags::AddNonBrkSpace ) ) { - if ( NeedsHardspaceAutocorr( cChar ) && - FnAddNonBrkSpace( rDoc, rTxt, nInsPos, GetDocLanguage( rDoc, nInsPos ), io_bNbspRunNext ) ) + // WARNING ATTENTION: rTxt is an alias of the text node's OUString + // and its length may change (even become shorter) if FnAddNonBrkSpace succeeds! + sal_Int32 nUpdatedPos = -1; + if (NeedsHardspaceAutocorr(cChar)) + nUpdatedPos = FnAddNonBrkSpace( rDoc, rTxt, nInsPos, GetDocLanguage( rDoc, nInsPos ), io_bNbspRunNext ); + if (nUpdatedPos >= 0) { - ; + nInsPos = nUpdatedPos; } else if ( bIsNextRun && !IsAutoCorrectChar( cChar ) ) { |