Efficient string concatenation

Norbert Thiebaud nthiebaud at gmail.com
Sun Dec 2 20:59:07 PST 2012


Patch 0001

it seems to me that addData via the different addDataHelper, do _not_
add the final binary 0, like rtl_stringbuffer_insert() for instance is
doing

so:
+#ifdef RTL_FAST_STRING
+    template< typename T1, typename T2 >
+    OStringBuffer( const OStringConcat< T1, T2 >& c )
+    {
+        const int l = c.length();
+        rtl_String* buffer = NULL;
+        rtl_string_new_WithLength( &buffer, l );
+        char* end = c.addData( buffer->buffer );
^^^
here the buffer is not 0-terminated...

so

OStringBuffer sBuff
sBuff.append("foo")
printf("%s", sBuff.getStr())  -> ok

OStringBuffer sBuff = "f" + "oo";
printf("%s", sBuff.getStr())  -> segfault or at least buffer overrun

no?

vaguely related... since we are talking about performance... why
*_new_WithLength() in strtmpl.cxx is doing a memset on the whole newly
allocated buffer...
surely the allocated buffer will be filled with something soon
enough... all that is needed is buffer[0] = 0 to make getStr()
C-string-safe.

Norbert


More information about the LibreOffice mailing list