Skip to content

Bug in String::copy, out of bounds crash #9110

Open
@TD-er

Description

@TD-er

Basic Infos

  • This issue complies with the issue POLICY doc.
  • I have read the documentation at readthedocs and the issue is not addressed there.
  • I have tested that the issue is present in current master branch (aka latest git).
  • I have searched the issue tracker for a similar issue.
  • If there is a stack dump, I have decoded it.
  • I have filled out all fields below.

Problem Description

See line 291 (and line 280):

String &String::copy(const __FlashStringHelper *pstr, unsigned int length) {
if (!reserve(length)) {
invalidate();
return *this;
}
setLen(length);
memcpy_P(wbuffer(), (PGM_P)pstr, length); // We know wbuffer() cannot ever be in PROGMEM, so memcpy safe here
wbuffer()[length] = 0;
return *this;
}

When allocating an array of N elements, accessing element [N] is out of bounds.

This only happens sometimes at very specific string lengths, as setLen only allocates in multiples of N bytes.

N.B. similar issue for ESP32, so I will also add an issue there.

Suggested fix:

reserve(length + 1);

N.B. This function is likely to be called with strlen() or strlen_P() as argument, which does not include the ending null character.

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