Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a48d96e

Browse files
committedAug 18, 2010
Adding some basic error checking to the String class (i.e. checking for a non-null buffer before modifying its contents).
1 parent 5ede40b commit a48d96e

File tree

2 files changed

+35
-25
lines changed

2 files changed

+35
-25
lines changed
 

‎hardware/arduino/cores/arduino/WString.cpp

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,61 +27,71 @@ String::String( const char *value )
2727
if ( value == NULL )
2828
value = "";
2929
getBuffer( _length = strlen( value ) );
30-
strcpy( _buffer, value );
30+
if ( _buffer != NULL )
31+
strcpy( _buffer, value );
3132
}
3233

3334
String::String( const String &value )
3435
{
3536
getBuffer( _length = value._length );
36-
strcpy( _buffer, value._buffer );
37+
if ( _buffer != NULL )
38+
strcpy( _buffer, value._buffer );
3739
}
3840

3941
String::String( const char value )
4042
{
4143
_length = 1;
4244
getBuffer(1);
43-
_buffer[0] = value;
44-
_buffer[1] = 0;
45+
if ( _buffer != NULL ) {
46+
_buffer[0] = value;
47+
_buffer[1] = 0;
48+
}
4549
}
4650

4751
String::String( const unsigned char value )
4852
{
4953
_length = 1;
5054
getBuffer(1);
51-
_buffer[0] = value;
52-
_buffer[1] = 0;
55+
if ( _buffer != NULL) {
56+
_buffer[0] = value;
57+
_buffer[1] = 0;
58+
}
5359
}
5460

5561
String::String( const int value, const int base )
5662
{
5763
char buf[33];
5864
itoa((signed long)value, buf, base);
5965
getBuffer( _length = strlen(buf) );
60-
strcpy( _buffer, buf );
66+
if ( _buffer != NULL )
67+
strcpy( _buffer, buf );
6168
}
6269

6370
String::String( const unsigned int value, const int base )
6471
{
6572
char buf[33];
6673
ultoa((unsigned long)value, buf, base);
6774
getBuffer( _length = strlen(buf) );
68-
strcpy( _buffer, buf );
75+
if ( _buffer != NULL )
76+
strcpy( _buffer, buf );
6977
}
7078

7179
String::String( const long value, const int base )
7280
{
7381
char buf[33];
7482
ltoa(value, buf, base);
7583
getBuffer( _length = strlen(buf) );
76-
strcpy( _buffer, buf );
84+
if ( _buffer != NULL )
85+
strcpy( _buffer, buf );
7786
}
7887

7988
String::String( const unsigned long value, const int base )
8089
{
8190
char buf[33];
8291
ultoa(value, buf, 10);
8392
getBuffer( _length = strlen(buf) );
84-
strcpy( _buffer, buf );
93+
if ( _buffer != NULL )
94+
strcpy( _buffer, buf );
8595
}
8696

8797
char String::charAt( unsigned int loc ) const
@@ -91,6 +101,7 @@ char String::charAt( unsigned int loc ) const
91101

92102
void String::setCharAt( unsigned int loc, const char aChar )
93103
{
104+
if(_buffer == NULL) return;
94105
if(_length > loc) {
95106
_buffer[loc] = aChar;
96107
}
@@ -116,8 +127,11 @@ const String & String::operator=( const String &rhs )
116127
free(_buffer);
117128
getBuffer( rhs._length );
118129
}
119-
_length = rhs._length;
120-
strcpy( _buffer, rhs._buffer );
130+
131+
if ( _buffer != NULL ) {
132+
_length = rhs._length;
133+
strcpy( _buffer, rhs._buffer );
134+
}
121135
return *this;
122136
}
123137

@@ -138,10 +152,12 @@ const String & String::operator+=( const String &other )
138152
{
139153
char *temp = _buffer;
140154
getBuffer( _length );
141-
strcpy( _buffer, temp );
155+
if ( _buffer != NULL )
156+
strcpy( _buffer, temp );
142157
free(temp);
143158
}
144-
strcat( _buffer, other._buffer );
159+
if ( _buffer != NULL )
160+
strcat( _buffer, other._buffer );
145161
return *this;
146162
}
147163

@@ -213,6 +229,7 @@ boolean String::equalsIgnoreCase( const String &s2 ) const
213229

214230
String String::replace( char findChar, char replaceChar )
215231
{
232+
if ( _buffer == NULL ) return *this;
216233
String theReturn = _buffer;
217234
char* temp = theReturn._buffer;
218235
while( (temp = strchr( temp, findChar )) != 0 )
@@ -223,6 +240,7 @@ String String::replace( char findChar, char replaceChar )
223240

224241
String String::replace( const String& match, const String& replace )
225242
{
243+
if ( _buffer == NULL ) return *this;
226244
String temp = _buffer, newString;
227245

228246
int loc;
@@ -376,6 +394,7 @@ String String::toUpperCase() const
376394

377395
String String::trim() const
378396
{
397+
if ( _buffer == NULL ) return *this;
379398
String temp = _buffer;
380399
unsigned int i,j;
381400

‎hardware/arduino/cores/arduino/WString.h

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class String
5252
char operator []( unsigned int index ) const;
5353
char& operator []( unsigned int index );
5454
//operator const char *() const { return _buffer; }
55-
55+
5656
// general methods
5757
char charAt( unsigned int index ) const;
5858
int compareTo( const String &anotherString ) const;
@@ -89,7 +89,6 @@ class String
8989
unsigned int _length; // the String length (not counting the '\0')
9090

9191
void getBuffer(unsigned int maxStrLen);
92-
void doubleBuffer( );
9392

9493
private:
9594

@@ -100,15 +99,7 @@ inline void String::getBuffer(unsigned int maxStrLen)
10099
{
101100
_capacity = maxStrLen;
102101
_buffer = (char *) malloc(_capacity + 1);
103-
}
104-
105-
// double the buffer size
106-
inline void String::doubleBuffer( )
107-
{
108-
char *temp = _buffer;
109-
getBuffer( ++_capacity * 2 );
110-
strcpy( _buffer, temp );
111-
free(temp);
102+
if (_buffer == NULL) _length = _capacity = 0;
112103
}
113104

114105
inline String operator+( String lhs, const String &rhs )

0 commit comments

Comments
 (0)
Please sign in to comment.