Skip to content

String ::remove, ::trim have overlapping strncpy/memcpy, undefined results per standard specs #20

Closed
@earlephilhower

Description

@earlephilhower

The ESP8266 Arduino port just discovered the following line in String::remove where strncpy is used with overlapping source and destination addresses (it's moving things backwards in the allocated buffer). See link for the valgrind warnings reported by our CI system.

char *writeTo = buffer + index;
len = len - count;
strncpy(writeTo, buffer + index + count,len - index);

str*cpy is undefined when you overlap source and destination

While it's true that on the AVR, an 8-bit MCU, str*cpy will be implemented in a byte-wise manner, this code has and will probably be copied to 16- and 32-bit systems where they may use optimizations which give strange results occasionally due to the overlap. A simple replacement of the strcpy with a memmove in this line clears this error and provides desired behavior on all systems.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions