[Libreoffice-commits] core.git: sal/rtl

Libreoffice Gerrit user logerrit at kemper.freedesktop.org
Mon Jul 30 08:18:34 UTC 2018


 sal/rtl/strbuf.cxx  |   18 +++++++++++++-----
 sal/rtl/strimp.hxx  |    7 ++++++-
 sal/rtl/strtmpl.cxx |    2 +-
 sal/rtl/ustrbuf.cxx |   19 +++++++++++++------
 4 files changed, 33 insertions(+), 13 deletions(-)

New commits:
commit dead246b1955a99b66b0a69e8da3db1a61f3ab88
Author:     Noel Grandin <noelgrandin at gmail.com>
AuthorDate: Sun Jul 29 15:20:46 2018 +0200
Commit:     Noel Grandin <noel.grandin at collabora.co.uk>
CommitDate: Mon Jul 30 10:18:13 2018 +0200

    avoid writing StringBuffer twice
    
    Change-Id: Ib0a8914cc1967f89a2cd3b062e7a0b52de7a972c
    Reviewed-on: https://gerrit.libreoffice.org/58281
    Tested-by: Jenkins
    Reviewed-by: Noel Grandin <noel.grandin at collabora.co.uk>

diff --git a/sal/rtl/strbuf.cxx b/sal/rtl/strbuf.cxx
index 194e9b159af5..2b7d8e96a8d1 100644
--- a/sal/rtl/strbuf.cxx
+++ b/sal/rtl/strbuf.cxx
@@ -21,6 +21,7 @@
 
 #include <osl/interlck.h>
 #include <rtl/strbuf.hxx>
+#include "strimp.hxx"
 
 /*************************************************************************
  *  rtl_stringbuffer_newFromStr_WithLength
@@ -37,9 +38,13 @@ void SAL_CALL rtl_stringbuffer_newFromStr_WithLength( rtl_String ** newStr,
         return;
     }
 
-    rtl_string_new_WithLength( newStr, count + 16 );
+    // use raw alloc to avoid overwriting the buffer twice
+    if ( *newStr)
+        rtl_string_release( *newStr );
+    *newStr = rtl_string_ImplAlloc( count + 16 );
     (*newStr)->length = count;
     memcpy( (*newStr)->buffer, value, count );
+    memset( (*newStr)->buffer + count, 0, 16 );
 }
 
 /*************************************************************************
@@ -78,16 +83,19 @@ void SAL_CALL rtl_stringbuffer_ensureCapacity
     {
         rtl_String * pTmp = *This;
         rtl_String * pNew = nullptr;
-        *capacity = ((*This)->length + 1) * 2;
+        auto nLength = (*This)->length;
+        *capacity = (nLength + 1) * 2;
         if (minimumCapacity > *capacity)
             /* still lower, set to the minimum capacity */
             *capacity = minimumCapacity;
 
-        rtl_string_new_WithLength(&pNew, *capacity);
-        pNew->length = (*This)->length;
+        // use raw alloc to avoid overwriting the buffer twice
+        pNew = rtl_string_ImplAlloc( *capacity );
+        pNew->length = nLength;
         *This = pNew;
 
-        memcpy( (*This)->buffer, pTmp->buffer, pTmp->length );
+        memcpy( (*This)->buffer, pTmp->buffer, nLength );
+        memset( (*This)->buffer + nLength, 0, *capacity - nLength );
         rtl_string_release( pTmp );
     }
 }
diff --git a/sal/rtl/strimp.hxx b/sal/rtl/strimp.hxx
index a5767fea6917..b3446db1fdb2 100644
--- a/sal/rtl/strimp.hxx
+++ b/sal/rtl/strimp.hxx
@@ -25,8 +25,9 @@
 #include <sys/sdt.h>
 #endif
 
-
 #include <sal/types.h>
+#include <rtl/string.hxx>
+#include <rtl/ustring.hxx>
 
 /* ======================================================================= */
 /* Help functions for String and UString                                   */
@@ -49,6 +50,10 @@ sal_Int16 rtl_ImplGetDigit( sal_Unicode ch, sal_Int16 nRadix );
 
 bool rtl_ImplIsWhitespace( sal_Unicode c );
 
+rtl_uString* rtl_uString_ImplAlloc( sal_Int32 nLen );
+
+rtl_String* rtl_string_ImplAlloc( sal_Int32 nLen );
+
 extern "C" {
 
 typedef void *(*rtl_allocateStringFn)(sal_Size size);
diff --git a/sal/rtl/strtmpl.cxx b/sal/rtl/strtmpl.cxx
index 341a84d01052..e1ab1dce278c 100644
--- a/sal/rtl/strtmpl.cxx
+++ b/sal/rtl/strtmpl.cxx
@@ -1134,7 +1134,7 @@ sal_uInt64 SAL_CALL IMPL_RTL_STRNAME( toUInt64 )( const IMPL_RTL_STRCODE* pStr,
 /* Internal String-Class help functions                                    */
 /* ======================================================================= */
 
-static IMPL_RTL_STRINGDATA* IMPL_RTL_STRINGNAME( ImplAlloc )( sal_Int32 nLen )
+IMPL_RTL_STRINGDATA* IMPL_RTL_STRINGNAME( ImplAlloc )( sal_Int32 nLen )
 {
     IMPL_RTL_STRINGDATA * pData
         = (sal::static_int_cast< sal_uInt32 >(nLen)
diff --git a/sal/rtl/ustrbuf.cxx b/sal/rtl/ustrbuf.cxx
index 4ebfbbbc994c..df8250e71a1d 100644
--- a/sal/rtl/ustrbuf.cxx
+++ b/sal/rtl/ustrbuf.cxx
@@ -41,9 +41,13 @@ void SAL_CALL rtl_uStringbuffer_newFromStr_WithLength( rtl_uString ** newStr,
         return;
     }
 
-    rtl_uString_new_WithLength( newStr, count + 16 );
+    // use raw alloc to avoid overwriting the buffer twice
+    if ( *newStr)
+        rtl_uString_release( *newStr );
+    *newStr = rtl_uString_ImplAlloc( count + 16 );
     (*newStr)->length = count;
-    memcpy( (*newStr)->buffer, value, count * sizeof(sal_Unicode));
+    memcpy( (*newStr)->buffer, value, count * sizeof(sal_Unicode) );
+    memset( (*newStr)->buffer + count, 0, 16 * sizeof(sal_Unicode) );
     RTL_LOG_STRING_NEW( *newStr );
 }
 
@@ -103,16 +107,19 @@ void SAL_CALL rtl_uStringbuffer_ensureCapacity
     {
         rtl_uString * pTmp = *This;
         rtl_uString * pNew = nullptr;
-        *capacity = ((*This)->length + 1) * 2;
+        auto nLength = (*This)->length;
+        *capacity = (nLength + 1) * 2;
         if (minimumCapacity > *capacity)
             /* still lower, set to the minimum capacity */
             *capacity = minimumCapacity;
 
-        rtl_uString_new_WithLength(&pNew, *capacity);
-        pNew->length = (*This)->length;
+        // use raw alloc to avoid overwriting the buffer twice
+        pNew = rtl_uString_ImplAlloc( *capacity );
+        pNew->length = nLength;
         *This = pNew;
 
-        memcpy( (*This)->buffer, pTmp->buffer, pTmp->length * sizeof(sal_Unicode) );
+        memcpy( (*This)->buffer, pTmp->buffer, nLength * sizeof(sal_Unicode) );
+        memset( (*This)->buffer + nLength, 0, (*capacity - nLength) * sizeof(sal_Unicode) );
 
         RTL_LOG_STRING_NEW( pTmp ); // with accurate contents
         rtl_uString_release( pTmp );


More information about the Libreoffice-commits mailing list