sc/source/ui/docshell/impex.cxx |   37 ++++++++++++++++++++++++++++++++++---
 1 file changed, 34 insertions(+), 3 deletions(-)

New commits:
commit ac5e918bef47a5f7ec4e5c76e94b36753c85e031
Author:     Eike Rathke <er...@redhat.com>
AuthorDate: Tue Feb 15 15:49:45 2022 +0100
Commit:     Xisco Fauli <xiscofa...@libreoffice.org>
CommitDate: Wed Feb 16 09:15:48 2022 +0100

    Resolves: tdf#147421 Do not use OUString::replaceAll() to strip null-bytes
    
    It reallocates and concatenates for each replacement so for
    massive amounts takes ages.
    
    Change-Id: Ibe1673fd4775c5b95833000669c1a24e718fd77c
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/129971
    Reviewed-by: Eike Rathke <er...@redhat.com>
    Tested-by: Jenkins
    (cherry picked from commit 4b0c17609c2cca326bbcc9e8488a327a4a9ea952)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/129938
    Reviewed-by: Xisco Fauli <xiscofa...@libreoffice.org>

diff --git a/sc/source/ui/docshell/impex.cxx b/sc/source/ui/docshell/impex.cxx
index 5d8e755c18f6..1c21a67562e2 100644
--- a/sc/source/ui/docshell/impex.cxx
+++ b/sc/source/ui/docshell/impex.cxx
@@ -1766,10 +1766,41 @@ void ScImportExport::EmbeddedNullTreatment( OUString & 
rStr )
 
     // The normal case is no embedded NULL, check first before de-/allocating
     // ustring stuff.
-    sal_Unicode cNull = 0;
-    if (rStr.indexOf( cNull) >= 0)
+    const sal_Unicode cNull = 0;
+    sal_Int32 i;
+    if ((i = rStr.indexOf( cNull)) >= 0)
     {
-        rStr = rStr.replaceAll( std::u16string_view( &cNull, 1), "");
+        // Do not use OUString::replaceAll(...,""), in case of repeated null
+        // bytes that reallocates for each and for massive amounts takes
+        // ~endless. See tdf#147421 with 3577016 trailing null-bytes.
+        const sal_Int32 nLen = rStr.getLength();
+        OUStringBuffer aBuf( nLen);
+        sal_Int32 s = 0;
+        sal_Unicode const * const p = rStr.getStr();
+        do
+        {
+            // Append good substring.
+            aBuf.append( p + s, i - s);
+            // Skip all cNull.
+            while (++i < nLen && *(p+i) == cNull)
+                ;
+            // Find next cNull after good if characters left, else end.
+            if (i < nLen)
+            {
+                s = i;
+                i = rStr.indexOf( cNull, i);
+            }
+            else
+            {
+                s = nLen;
+            }
+        }
+        while (0 <= i && i < nLen);
+        // Append good trailing substring, if any.
+        if (s < nLen)
+            aBuf.append( p + s, nLen - s);
+
+        rStr = aBuf.makeStringAndClear();
     }
 }
 

Reply via email to