[llvm-bugs] [Bug 40682] New: clang++ miscompilation while generating copy constructors of classes containing 0-length array

via llvm-bugs llvm-bugs at lists.llvm.org
Sun Feb 10 11:02:42 PST 2019


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: unassignedclangbugs at nondot.org
          Reporter: joran.bigalet at gmail.com
                CC: blitzrakete at gmail.com, dgregor at apple.com,
                    erik.pilkington at gmail.com, llvm-bugs at lists.llvm.org,
                    richard-llvm at 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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20190210/abd238d2/attachment.html>


More information about the llvm-bugs mailing list