diff options
author | Noel Grandin <noelgrandin@gmail.com> | 2018-07-29 15:20:46 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2018-07-30 10:18:13 +0200 |
commit | dead246b1955a99b66b0a69e8da3db1a61f3ab88 (patch) | |
tree | 194875572af5700a0c174f978ae164ccca874bae /sal/rtl/strbuf.cxx | |
parent | bdb0775a262cb66e8924303c98bf5180a7fb02ee (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/rtl/strbuf.cxx')
-rw-r--r-- | sal/rtl/strbuf.cxx | 18 |
1 files changed, 13 insertions, 5 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 ); } } |