• Re: UB-free slice

    From Kaz Kylheku@046-301-5902@kylheku.com to comp.lang.c on Tue Jan 6 20:48:51 2026
    From Newsgroup: comp.lang.c

    On 2026-01-05, highcrew <high.crew3868@fastmail.com> wrote:
    In my understanding, if we call buffer_append(&x, &y) for x.bytes and
    y.bytes pointing to overlapping areas of the same array, we get UB by
    the first two parameters of memcpy being restrict-qualified pointers.

    Nope! The standard doesn't provide a /definition/ of memcpy.

    And restrict qualifiers on pointer parameters in a /declaration/ don't
    mean anything (and need not be repeated in the definition).

    memcpy is not required to take overlapping objects for the
    reason that its description says so.

    That situation existed long before there was a restrict, and has
    implications in the absence of restrict. Various ways of implemeting a memcpy-like function will produce various unexpected results when
    objects overlap, even without any undefined behavior taking place. For instance if we copy byte-by-byte, from lowest address to highest, then
    we end up writing into memory that our loop is about to read from,
    corrupting the data.

    In order not to place any restrictions on how a memcpy may be
    implemented, whether with the help of restrict pointers, or assembly
    language or whatever else, the standard makes overlapping inputs
    undefined.
    --
    TXR Programming Language: http://nongnu.org/txr
    Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
    Mastodon: @Kazinator@mstdn.ca
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Kaz Kylheku@046-301-5902@kylheku.com to comp.lang.c on Tue Jan 6 20:50:42 2026
    From Newsgroup: comp.lang.c

    On 2026-01-06, Kaz Kylheku <046-301-5902@kylheku.com> wrote:
    implications in the absence of restrict. Various ways of implemeting a memcpy-like function will produce various unexpected results when
    objects overlap, even without any undefined behavior taking place. For instance if we copy byte-by-byte, from lowest address to highest, then
    we end up writing into memory that our loop is about to read from,
    corrupting the data.

    I mean, of course, when we copy from a lower addressed object to a higher addresed object which overlaps it.
    --
    TXR Programming Language: http://nongnu.org/txr
    Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
    Mastodon: @Kazinator@mstdn.ca
    --- Synchronet 3.21a-Linux NewsLink 1.2