From 63f4021447231003f1c362b0f492756644aa3165 Mon Sep 17 00:00:00 2001 From: "David A. Mellis" Date: Sat, 11 Dec 2010 15:22:07 -0500 Subject: [PATCH] Changing String append to use realloc(); thanks to Paul Stoffregen. http://code.google.com/p/arduino/issues/detail?id=332 --- hardware/arduino/cores/arduino/WString.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/hardware/arduino/cores/arduino/WString.cpp b/hardware/arduino/cores/arduino/WString.cpp index 4de929616..db5a441dc 100644 --- a/hardware/arduino/cores/arduino/WString.cpp +++ b/hardware/arduino/cores/arduino/WString.cpp @@ -150,14 +150,16 @@ const String & String::operator+=( const String &other ) _length += other._length; if ( _length > _capacity ) { - char *temp = _buffer; - getBuffer( _length ); - if ( _buffer != NULL ) - strcpy( _buffer, temp ); - free(temp); + char *temp = (char *)realloc(_buffer, _length + 1); + if ( temp != NULL ) { + _buffer = temp; + _capacity = _length; + } else { + _length -= other._length; + return *this; + } } - if ( _buffer != NULL ) - strcat( _buffer, other._buffer ); + strcat( _buffer, other._buffer ); return *this; }