Skip to content

Build warnings -- [-Wrestrict] #253

Closed
@henrygab

Description

@henrygab

Original Log

expand

/home/runner/.arduino15/packages/adafruit/hardware/samd/1.6.0/cores/arduino/WString.cpp: In member function 'void String::remove(unsigned int, unsigned int)':
/home/runner/.arduino15/packages/adafruit/hardware/samd/1.6.0/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);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

See https://github.com/adafruit/ArduinoCore-samd/runs/965558867

Simplified log w/hex offsets

.../samd/1.6.0/cores/arduino/WString.cpp:701:9:

warning: 'char* strncpy(char*, const char*, size_t)' accessing 0 or more bytes
    at offsets [0xC000_0000, 0x3FFF_FFFF] and [0xC000_0000, 0x3FFF_FFFF]
    may overlap up to 0xFFFF_FFFF bytes at offset [0x1_3FFF_FFFD, 0x3FFF_FFFF]
    [-Wrestrict]

701 |  strncpy(writeTo, buffer + index + count,len - index);
    |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

-Wrestrict information

From https://en.cppreference.com/w/c/language/restrict:

During each execution of a block in which a restricted pointer P is declared (typically each execution of a function body in which P is a function parameter), if some object that is accessible through P (directly or indirectly) is modified, by any means, then all accesses to that object (both reads and writes) in that block must occur through P (directly or indirectly), otherwise the behavior is undefined:

See GCC documentation for an example bug.

Interpretation

strncpy() marks the source and destination as restricted pointers. Therefore, the caller must detect and prevent overlap between the source and destination.

Recommendation

Preferably, rewrite to use strncpy_s() (added in C++11), which handles and reports additional errors (including overlapping src / dest).

Otherwise, write the code that validates that destination and source do not overlap, prior to calling strncpy().

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