patch: avoid ubsan errors in implicit copy constructors

Nick Lewycky nlewycky at google.com
Mon Sep 9 15:03:39 PDT 2013


The attached patch disables the bool and enum sanitizers when emitting the
implicitly-defined copy constructor.

To start with an example:
  struct X { X(); X(const X&); };
  struct Y { X x; bool b; };
if you don't initialize Y::b then try to copy an object of Y type, ubsan
will complain. Technically, with the standard as written, ubsan is correct.
However, this is a useful thing to do -- you may have a discriminator which
decides which elements are not interesting and therefore never initialize
or read them. Secondly, it's a departure from the rules in C, making
well-defined C code have undefined behaviour in C++ (structs are never trap
values, see C99 6.2.6.1p6). Thirdly, it's checked incompletely right now --
if you make subtle changes (f.e. add an "int i;" member to Y) ubsan will
stop complaining. The semantic I'm implementing is as if the implicit copy
constructor is copying the value representation (the bytes) not the object
representation (the meaning of those bytes).

Nick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130909/ff60e107/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ubsan-bool-cvr-1.patch
Type: application/octet-stream
Size: 4198 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130909/ff60e107/attachment.obj>


More information about the cfe-commits mailing list