Page MenuHomeFreeBSD

memcpy.3: remove BUGS section allowing overlapping strings
ClosedPublic

Authored by emaste on Jul 15 2021, 2:12 PM.
Tags
None
Referenced Files
F96002700: D31192.diff
Mon, Sep 23, 7:39 AM
F95842533: D31192.diff
Sun, Sep 22, 7:48 PM
Unknown Object (File)
Wed, Sep 18, 12:14 PM
Unknown Object (File)
Wed, Sep 18, 7:07 AM
Unknown Object (File)
Tue, Sep 17, 8:52 PM
Unknown Object (File)
Tue, Sep 17, 4:19 PM
Unknown Object (File)
Wed, Sep 11, 2:55 PM
Unknown Object (File)
Tue, Sep 10, 2:19 AM
Subscribers

Details

Summary

The removed text claimed that memcpy is implemented using bcopy and thus strings may overlap. That was not actually true (although the implementation may have behaved as if so).

In any case it is undefined behavior according to the C standard, and this man page already claimed that src and dst may not overlap. Just remove the extra text that permits overlapping.

Diff Detail

Lint
Lint Skipped
Unit
Tests Skipped

Event Timeline

emaste created this revision.

I do not object, but we need to coordinate due to D31180

This revision is now accepted and ready to land.Jul 15 2021, 2:34 PM

Mentioning how it is implemented is definitely a manpage bug, but ultimately the implementation allows for overlapping strings and this can't be changed because userspace expects it. There is a famous glibc bugreport where memcpy was patched to no longer allow overlapping stores and that broke tons of stuff. This had to get worse since then.

I think a note that overlapping stores are supported due to overall breakage is prudent, but I don't know how to nicely word it.

one such bug report: https://sourceware.org/bugzilla/show_bug.cgi?id=12518

glibc man page: http://manpages.ubuntu.com/manpages/xenial/man3/memcpy.3.html

Failure to observe the requirement that the memory areas  do  not  overlap  has  been  the
source of real bugs.  (POSIX and the C standards are explicit that employing memcpy() with
overlapping areas produces undefined behavior.)  Most notably, in glibc 2.13 a performance
optimization  of memcpy() on some platforms (including x86-64) included changing the order
in which bytes were copied from src to dest.

This change revealed breakages in a number of applications  that  performed  copying  with
overlapping  areas.   Under the previous implementation, the order in which the bytes were
copied had fortuitously hidden the bug, which was revealed  when  the  copying  order  was
reversed.   In  glibc 2.14, a versioned symbol was added so that old binaries (i.e., those
linked against glibc versions earlier than 2.14) employed a memcpy()  implementation  that
safely   handles   the   overlapping  buffers  case  (by  providing  an  "older"  memcpy()
implementation that was aliased to memmove(3)).