Skip to content

Don't use strncpy() in String::remove() #295

Closed
@bmcdonnell

Description

@bmcdonnell

In WString.cpp, String::remove() should not use strncpy(), since its behavior is undefined when the source and destination overlap.

Sources:

I noticed the issue due to the compiler warning below.

C:\Users\username\AppData\Local\Arduino15\packages\adafruit\hardware\samd\1.6.7\cores\arduino\WString.cpp: In member function 'void String::remove(unsigned int, unsigned int)':
C:\Users\username\AppData\Local\Arduino15\packages\adafruit\hardware\samd\1.6.7\cores\arduino\WString.cpp:701:9: warning: 'char* strncpy(char*, const char*, size_t)' accessing 0 or more bytes at offsets [-1073741824, 1073741823] and [-1073741824, 1073741823] may overlap up to 4294967295 bytes at offset [5368709117, 1073741823] [-Wrestrict]
  701 |  strncpy(writeTo, buffer + index + count,len - index);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions