summaryrefslogtreecommitdiff
path: root/sal
diff options
context:
space:
mode:
authorNoel Grandin <noelgrandin@gmail.com>2018-07-29 15:20:46 +0200
committerNoel Grandin <noel.grandin@collabora.co.uk>2018-07-30 10:18:13 +0200
commitdead246b1955a99b66b0a69e8da3db1a61f3ab88 (patch)
tree194875572af5700a0c174f978ae164ccca874bae /sal
parentbdb0775a262cb66e8924303c98bf5180a7fb02ee (diff)
avoid writing StringBuffer twice
Change-Id: Ib0a8914cc1967f89a2cd3b062e7a0b52de7a972c Reviewed-on: https://gerrit.libreoffice.org/58281 Tested-by: Jenkins Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'sal')
-rw-r--r--sal/rtl/strbuf.cxx18
-rw-r--r--sal/rtl/strimp.hxx7
-rw-r--r--sal/rtl/strtmpl.cxx2
-rw-r--r--sal/rtl/ustrbuf.cxx19
4 files changed, 33 insertions, 13 deletions
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 );