public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/108846] New: std::copy, std::copy_n on potentially overlapping subobjects
@ 2023-02-18 21:22 arthur.j.odwyer at gmail dot com
  2023-02-18 21:55 ` [Bug libstdc++/108846] " pinskia at gcc dot gnu.org
                   ` (27 more replies)
  0 siblings, 28 replies; 29+ messages in thread
From: arthur.j.odwyer at gmail dot com @ 2023-02-18 21:22 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108846

            Bug ID: 108846
           Summary: std::copy, std::copy_n on potentially overlapping
                    subobjects
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: arthur.j.odwyer at gmail dot com
  Target Milestone: ---

// https://gcc.godbolt.org/z/EfdG4nzv9
#include <algorithm>
#include <cassert>
struct B { 
    B(int i, short j) : i(i), j(j) {}
    int i; 
    short j; /* 2 byte padding */ 
};
struct D : B { 
    D(int i, short j, short x) : B(i, j), x(x) {}    
    short x;
};
int main() {
    D ddst(1, 2, 3);
    D dsrc(4, 5, 6);
    B *dst = &ddst;
    B *src = &dsrc;
    std::copy_n(src, 1, dst);  // should do `*dst = *src`
    assert(ddst.x == 3);  // FAILS
}

Similarly if you use `std::copy(src, src+1, dst)`.
The problem here is that `std::copy` and `std::copy_n` (and presumably some
other algorithms too, like `std::move`) believe that if a type is trivially
copyable, then it's safe to use `memcpy` or `memmove` on it. But in fact that's
safe only if either
- the type's "sizeof" is equal to its "data size, without trailing padding"; or
- you happen to have external information proving that the object being copied
is not a potentially overlapping subobject.

I think it's safe for `std::copy` and `std::copy_n` to use memcpy/memmove if
they are given a destination range of 2-or-more elements; but if it's just a
single object, then (by the letter of the law) they must assume the destination
object might be a potentially overlapping subobject, and not memcpy into it.

(Full disclosure: I will be THRILLED if you close this as "not a bug," because
that will be ammunition to go to LWG and say "look, vendors think this behavior
is fine, we should actually standardize this behavior and remove the
expectation that STL algorithms can ever handle potentially overlapping
subobjects.")

^ permalink raw reply	[flat|nested] 29+ messages in thread

end of thread, other threads:[~2024-03-18 14:13 UTC | newest]

Thread overview: 29+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-18 21:22 [Bug libstdc++/108846] New: std::copy, std::copy_n on potentially overlapping subobjects arthur.j.odwyer at gmail dot com
2023-02-18 21:55 ` [Bug libstdc++/108846] " pinskia at gcc dot gnu.org
2023-02-18 21:58 ` pinskia at gcc dot gnu.org
2023-02-18 22:00 ` pinskia at gcc dot gnu.org
2023-02-20  8:38 ` de34 at live dot cn
2023-02-20 23:42 ` dangelog at gmail dot com
2023-02-21  1:58 ` de34 at live dot cn
2023-02-21  7:21 ` [Bug libstdc++/108846] std::copy, std::copy_n and std::copy_backward " pinskia at gcc dot gnu.org
2023-02-23 17:20 ` redi at gcc dot gnu.org
2023-02-23 17:25 ` redi at gcc dot gnu.org
2023-02-23 17:27 ` redi at gcc dot gnu.org
2023-02-23 17:29 ` arthur.j.odwyer at gmail dot com
2023-02-24 19:31 ` dangelog at gmail dot com
2023-02-25  9:06 ` redi at gcc dot gnu.org
2023-02-25  9:07 ` redi at gcc dot gnu.org
2023-02-25 11:40 ` dangelog at gmail dot com
2023-02-25 14:13 ` redi at gcc dot gnu.org
2023-02-25 14:16 ` redi at gcc dot gnu.org
2023-02-25 14:19 ` redi at gcc dot gnu.org
2023-02-25 14:29 ` redi at gcc dot gnu.org
2023-02-28  9:50 ` cvs-commit at gcc dot gnu.org
2023-03-02 10:49 ` dangelog at gmail dot com
2023-03-02 11:19 ` redi at gcc dot gnu.org
2023-04-20 13:50 ` redi at gcc dot gnu.org
2023-04-20 14:12 ` arthur.j.odwyer at gmail dot com
2023-04-20 17:10 ` redi at gcc dot gnu.org
2023-04-20 17:11 ` redi at gcc dot gnu.org
2024-03-18 14:05 ` cvs-commit at gcc dot gnu.org
2024-03-18 14:13 ` redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).