From 2fdc6960b47eb50221c6c1a84ff5bcc3570ec03e Mon Sep 17 00:00:00 2001 From: Bjarne Stroustrup Date: Sun, 8 Dec 2019 14:54:19 -0500 Subject: [PATCH] Update CppCoreGuidelines.md Adding rule to resolve #1424 --- CppCoreGuidelines.md | 46 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index 2932d55..dc78eeb 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -1,6 +1,6 @@ # C++ Core Guidelines -June 17, 2019 +December 8, 2019 Editors: @@ -19243,7 +19243,7 @@ Container rule summary: * [SL.con.1: Prefer using STL `array` or `vector` instead of a C array](#Rsl-arrays) * [SL.con.2: Prefer using STL `vector` by default unless you have a reason to use a different container](#Rsl-vector) * [SL.con.3: Avoid bounds errors](#Rsl-bounds) -* ??? +* [SL.con.4: don't use `memset` or `memcpy` for arguments that are not non-trivially-copyable](#Rsl-copy) ### SL.con.1: Prefer using STL `array` or `vector` instead of a C array @@ -19395,6 +19395,48 @@ If code is using an unmodified standard library, then there are still workaround This rule is part of the [bounds profile](#SS-bounds). + +### SL.con.4: don't use `memset` or `memcpy` for arguments that are not non-trivially-copyable + +##### Reason + +Doing so messes the semantics of the objects (e.g., by overwriting a `vptr`). + +##### Note + +Similarly for (w)memset, (w)memcpy, (w)memmove, and (w)memcmp + +##### Example + + struct base { + virtual void update() = 0; + }; + + struct derived : public base { + void update() override {} + }; + + + void f (derived& a, derived& b) // goodbye v-tables + { + memset(&a, 0, sizeof(derived)); + memcpy(&a, &b, sizeof(derived)); + memcmp(&a, &b, sizeof(derived)); + } + +Insted, define proper default initialization, copy, and comparison functions + + void g(derived& a, derived& b) + { + a = {}; // default initialize + b = a; // copy + if (a == b) do_something(a,b); + } + +##### Enforcement + +* Flag the use of those functions for types theat are not trivially copyable + **TODO Notes**: * Impact on the standard library will require close coordination with WG21, if only to ensure compatibility even if never standardized.