cac0ns3c / arduino

Automatically exported from code.google.com/p/arduino
Other
0 stars 0 forks source link

String operator+= should use realloc. #332

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Paul Stoffregen: "This version avoids buffer overflow if more memory can't be 
allocated.  It also leverages the C library realloc() function which is vastly 
better when the user repetitively concatenates stuff onto a string since it can 
just grow the existing block if free space follows."

const String & String::operator+=( const String &other )
{
 _length += other._length;
 if ( _length > _capacity ) {
  char *newbuffer = (char *)realloc(_buffer, _length + 1);
  if (newbuffer) {
    _buffer = newbuffer;
    _capacity = _length;
  } else {
    // out of memory, avoid buffer overflow by leaving string unchanged
    _length -= other._length;
    return *this;
  }
 }
 strcat( _buffer, other._buffer );
 return *this;
}

Original issue reported on code.google.com by dmel...@gmail.com on 18 Aug 2010 at 8:58

GoogleCodeExporter commented 9 years ago

Original comment by dmel...@gmail.com on 25 Aug 2010 at 5:54

GoogleCodeExporter commented 9 years ago

Original comment by dmel...@gmail.com on 5 Oct 2010 at 2:00

GoogleCodeExporter commented 9 years ago
https://github.com/arduino/Arduino/commit/63f4021447231003f1c362b0f492756644aa31
65

Original comment by dmel...@gmail.com on 11 Dec 2010 at 8:25