mirror of
https://github.com/arduino/Arduino.git
synced 2024-12-04 15:24:12 +01:00
Adding some basic error checking to the String class (i.e. checking for a non-null buffer before modifying its contents).
This commit is contained in:
parent
5ede40be13
commit
a48d96ee49
@ -27,12 +27,14 @@ String::String( const char *value )
|
|||||||
if ( value == NULL )
|
if ( value == NULL )
|
||||||
value = "";
|
value = "";
|
||||||
getBuffer( _length = strlen( value ) );
|
getBuffer( _length = strlen( value ) );
|
||||||
|
if ( _buffer != NULL )
|
||||||
strcpy( _buffer, value );
|
strcpy( _buffer, value );
|
||||||
}
|
}
|
||||||
|
|
||||||
String::String( const String &value )
|
String::String( const String &value )
|
||||||
{
|
{
|
||||||
getBuffer( _length = value._length );
|
getBuffer( _length = value._length );
|
||||||
|
if ( _buffer != NULL )
|
||||||
strcpy( _buffer, value._buffer );
|
strcpy( _buffer, value._buffer );
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,23 +42,28 @@ String::String( const char value )
|
|||||||
{
|
{
|
||||||
_length = 1;
|
_length = 1;
|
||||||
getBuffer(1);
|
getBuffer(1);
|
||||||
|
if ( _buffer != NULL ) {
|
||||||
_buffer[0] = value;
|
_buffer[0] = value;
|
||||||
_buffer[1] = 0;
|
_buffer[1] = 0;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
String::String( const unsigned char value )
|
String::String( const unsigned char value )
|
||||||
{
|
{
|
||||||
_length = 1;
|
_length = 1;
|
||||||
getBuffer(1);
|
getBuffer(1);
|
||||||
|
if ( _buffer != NULL) {
|
||||||
_buffer[0] = value;
|
_buffer[0] = value;
|
||||||
_buffer[1] = 0;
|
_buffer[1] = 0;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
String::String( const int value, const int base )
|
String::String( const int value, const int base )
|
||||||
{
|
{
|
||||||
char buf[33];
|
char buf[33];
|
||||||
itoa((signed long)value, buf, base);
|
itoa((signed long)value, buf, base);
|
||||||
getBuffer( _length = strlen(buf) );
|
getBuffer( _length = strlen(buf) );
|
||||||
|
if ( _buffer != NULL )
|
||||||
strcpy( _buffer, buf );
|
strcpy( _buffer, buf );
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,6 +72,7 @@ String::String( const unsigned int value, const int base )
|
|||||||
char buf[33];
|
char buf[33];
|
||||||
ultoa((unsigned long)value, buf, base);
|
ultoa((unsigned long)value, buf, base);
|
||||||
getBuffer( _length = strlen(buf) );
|
getBuffer( _length = strlen(buf) );
|
||||||
|
if ( _buffer != NULL )
|
||||||
strcpy( _buffer, buf );
|
strcpy( _buffer, buf );
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,6 +81,7 @@ String::String( const long value, const int base )
|
|||||||
char buf[33];
|
char buf[33];
|
||||||
ltoa(value, buf, base);
|
ltoa(value, buf, base);
|
||||||
getBuffer( _length = strlen(buf) );
|
getBuffer( _length = strlen(buf) );
|
||||||
|
if ( _buffer != NULL )
|
||||||
strcpy( _buffer, buf );
|
strcpy( _buffer, buf );
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,6 +90,7 @@ String::String( const unsigned long value, const int base )
|
|||||||
char buf[33];
|
char buf[33];
|
||||||
ultoa(value, buf, 10);
|
ultoa(value, buf, 10);
|
||||||
getBuffer( _length = strlen(buf) );
|
getBuffer( _length = strlen(buf) );
|
||||||
|
if ( _buffer != NULL )
|
||||||
strcpy( _buffer, buf );
|
strcpy( _buffer, buf );
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,6 +101,7 @@ char String::charAt( unsigned int loc ) const
|
|||||||
|
|
||||||
void String::setCharAt( unsigned int loc, const char aChar )
|
void String::setCharAt( unsigned int loc, const char aChar )
|
||||||
{
|
{
|
||||||
|
if(_buffer == NULL) return;
|
||||||
if(_length > loc) {
|
if(_length > loc) {
|
||||||
_buffer[loc] = aChar;
|
_buffer[loc] = aChar;
|
||||||
}
|
}
|
||||||
@ -116,8 +127,11 @@ const String & String::operator=( const String &rhs )
|
|||||||
free(_buffer);
|
free(_buffer);
|
||||||
getBuffer( rhs._length );
|
getBuffer( rhs._length );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( _buffer != NULL ) {
|
||||||
_length = rhs._length;
|
_length = rhs._length;
|
||||||
strcpy( _buffer, rhs._buffer );
|
strcpy( _buffer, rhs._buffer );
|
||||||
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,9 +152,11 @@ const String & String::operator+=( const String &other )
|
|||||||
{
|
{
|
||||||
char *temp = _buffer;
|
char *temp = _buffer;
|
||||||
getBuffer( _length );
|
getBuffer( _length );
|
||||||
|
if ( _buffer != NULL )
|
||||||
strcpy( _buffer, temp );
|
strcpy( _buffer, temp );
|
||||||
free(temp);
|
free(temp);
|
||||||
}
|
}
|
||||||
|
if ( _buffer != NULL )
|
||||||
strcat( _buffer, other._buffer );
|
strcat( _buffer, other._buffer );
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@ -213,6 +229,7 @@ boolean String::equalsIgnoreCase( const String &s2 ) const
|
|||||||
|
|
||||||
String String::replace( char findChar, char replaceChar )
|
String String::replace( char findChar, char replaceChar )
|
||||||
{
|
{
|
||||||
|
if ( _buffer == NULL ) return *this;
|
||||||
String theReturn = _buffer;
|
String theReturn = _buffer;
|
||||||
char* temp = theReturn._buffer;
|
char* temp = theReturn._buffer;
|
||||||
while( (temp = strchr( temp, findChar )) != 0 )
|
while( (temp = strchr( temp, findChar )) != 0 )
|
||||||
@ -223,6 +240,7 @@ String String::replace( char findChar, char replaceChar )
|
|||||||
|
|
||||||
String String::replace( const String& match, const String& replace )
|
String String::replace( const String& match, const String& replace )
|
||||||
{
|
{
|
||||||
|
if ( _buffer == NULL ) return *this;
|
||||||
String temp = _buffer, newString;
|
String temp = _buffer, newString;
|
||||||
|
|
||||||
int loc;
|
int loc;
|
||||||
@ -376,6 +394,7 @@ String String::toUpperCase() const
|
|||||||
|
|
||||||
String String::trim() const
|
String String::trim() const
|
||||||
{
|
{
|
||||||
|
if ( _buffer == NULL ) return *this;
|
||||||
String temp = _buffer;
|
String temp = _buffer;
|
||||||
unsigned int i,j;
|
unsigned int i,j;
|
||||||
|
|
||||||
|
@ -89,7 +89,6 @@ class String
|
|||||||
unsigned int _length; // the String length (not counting the '\0')
|
unsigned int _length; // the String length (not counting the '\0')
|
||||||
|
|
||||||
void getBuffer(unsigned int maxStrLen);
|
void getBuffer(unsigned int maxStrLen);
|
||||||
void doubleBuffer( );
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
@ -100,15 +99,7 @@ inline void String::getBuffer(unsigned int maxStrLen)
|
|||||||
{
|
{
|
||||||
_capacity = maxStrLen;
|
_capacity = maxStrLen;
|
||||||
_buffer = (char *) malloc(_capacity + 1);
|
_buffer = (char *) malloc(_capacity + 1);
|
||||||
}
|
if (_buffer == NULL) _length = _capacity = 0;
|
||||||
|
|
||||||
// double the buffer size
|
|
||||||
inline void String::doubleBuffer( )
|
|
||||||
{
|
|
||||||
char *temp = _buffer;
|
|
||||||
getBuffer( ++_capacity * 2 );
|
|
||||||
strcpy( _buffer, temp );
|
|
||||||
free(temp);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline String operator+( String lhs, const String &rhs )
|
inline String operator+( String lhs, const String &rhs )
|
||||||
|
Loading…
Reference in New Issue
Block a user