Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitef24307

Browse files
committed
WString: change strcpy() to memcpy()
It is worth to use memcpy() to avoid extra checks. Furthermore,it fixes possible issues when copied WString has zero byte storedintentionally.copy() methods are protected and always used to copy zero-terminatedstrings, so it is safe to copy termination zero byte from source buffer.
1 parent0428ad8 commitef24307

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

‎cores/arduino/WString.cpp‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ String & String::copy(const char *cstr, unsigned int length)
174174
return *this;
175175
}
176176
len = length;
177-
strcpy(buffer, cstr);
177+
memcpy(buffer, cstr, length +1);
178178
return *this;
179179
}
180180

@@ -185,7 +185,7 @@ String & String::copy(const __FlashStringHelper *pstr, unsigned int length)
185185
return *this;
186186
}
187187
len = length;
188-
strcpy_P(buffer, (PGM_P)pstr);
188+
memcpy_P(buffer, (PGM_P)pstr, length +1);
189189
return *this;
190190
}
191191

@@ -194,7 +194,7 @@ void String::move(String &rhs)
194194
{
195195
if (buffer) {
196196
if (rhs && capacity >= rhs.len) {
197-
strcpy(buffer, rhs.buffer);
197+
memcpy(buffer, rhs.buffer, rhs.len +1);
198198
len = rhs.len;
199199
rhs.len =0;
200200
return;
@@ -266,8 +266,9 @@ unsigned char String::concat(const char *cstr, unsigned int length)
266266
if (!cstr)return0;
267267
if (length ==0)return1;
268268
if (!reserve(newlen))return0;
269-
strcpy(buffer + len, cstr);
269+
memcpy(buffer + len, cstr, length);
270270
len = newlen;
271+
buffer[len] =0;
271272
return1;
272273
}
273274

@@ -341,7 +342,7 @@ unsigned char String::concat(const __FlashStringHelper * str)
341342
if (length ==0)return1;
342343
unsignedint newlen = len + length;
343344
if (!reserve(newlen))return0;
344-
strcpy_P(buffer + len, (constchar *) str);
345+
memcpy_P(buffer + len, (constchar *) str, length +1);
345346
len = newlen;
346347
return1;
347348
}
@@ -653,6 +654,7 @@ void String::replace(const String& find, const String& replace)
653654
}
654655
}elseif (diff <0) {
655656
char *writeTo = buffer;
657+
char *end = buffer + len;
656658
while ((foundAt =strstr(readFrom, find.buffer)) !=NULL) {
657659
unsignedint n = foundAt - readFrom;
658660
memcpy(writeTo, readFrom, n);
@@ -662,7 +664,7 @@ void String::replace(const String& find, const String& replace)
662664
readFrom = foundAt + find.len;
663665
len += diff;
664666
}
665-
strcpy(writeTo, readFrom);
667+
memcpy(writeTo, readFrom, end - readFrom +1);
666668
}else {
667669
unsignedint size = len;// compute size needed for result
668670
while ((foundAt =strstr(readFrom, find.buffer)) !=NULL) {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp