https://bugs.llvm.org/show_bug.cgi?id=40682

            Bug ID: 40682
           Summary: clang++ miscompilation while generating copy
                    constructors of classes containing 0-length array
           Product: clang
           Version: trunk
          Hardware: PC
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++
          Assignee: unassignedclangb...@nondot.org
          Reporter: joran.biga...@gmail.com
                CC: blitzrak...@gmail.com, dgre...@apple.com,
                    erik.pilking...@gmail.com, llvm-bugs@lists.llvm.org,
                    richard-l...@metafoo.co.uk

The auto-generated copy constructor of a struct containing a 0-length array
followed by exactly 1 trivially copyable field will not copy the later.
This holds for copy assignments.

Here is a simple example:

===================================================

#include <cstdio>

struct NonTrivial {
    int n;

    NonTrivial& operator=(NonTrivial o) {
        this->n = o.n;
        return *this;
    }
};

struct S {
    NonTrivial _a;  // to force clang to generate a copy assignment constructor
    int ok;  // not mandatory, only to show other trivial fields are still
copied
    int _b[0];
    int bugged;  // not copied by the auto-generated copy assignment
constructor, unless directly followed by another non-trivial field
};

int main() {
    S foo;
    foo.ok = 11;
    foo.bugged = 22;

    S bar;
    bar.ok = 9876;
    bar.bugged = 4321;

    bar = foo;

    printf("%d %d ; %d %d\n", foo.ok, foo.bugged, bar.ok, bar.bugged);
    // expected: 11 22 ; 11 22
    //   output: 11 22 ; 11 4321

    return 0;
}

===================================================

-- 
You are receiving this mail because:
You are on the CC list for the bug.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to