Poison non-POD memset & non-trivially-copyable memcpy/memmove
[deliverable/binutils-gdb.git] / gdb / common / poison.h
CommitLineData
b0b92aeb
PA
1/* Poison symbols at compile time.
2
3 Copyright (C) 2017 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20#ifndef COMMON_POISON_H
21#define COMMON_POISON_H
22
23#include "traits.h"
24
25/* Poison memset of non-POD types. The idea is catching invalid
26 initialization of non-POD structs that is easy to be introduced as
27 side effect of refactoring. For example, say this:
28
29 struct S { VEC(foo_s) *m_data; };
30
31is converted to this at some point:
32
33 struct S {
34 S() { m_data.reserve (10); }
35 std::vector<foo> m_data;
36 };
37
38and old code was initializing S objects like this:
39
40 struct S s;
41 memset (&s, 0, sizeof (S)); // whoops, now wipes vector.
42
43Declaring memset as deleted for non-POD types makes the memset above
44be a compile-time error. */
45
46/* Helper for SFINAE. True if "T *" is memsettable. I.e., if T is
47 either void, or POD. */
48template<typename T>
49struct IsMemsettable
50 : gdb::Or<std::is_void<T>,
51 std::is_pod<T>>
52{};
53
54template <typename T,
55 typename = gdb::Requires<gdb::Not<IsMemsettable<T>>>>
56void *memset (T *s, int c, size_t n) = delete;
57
58/* Similarly, poison memcpy and memmove of non trivially-copyable
59 types, which is undefined. */
60
61/* True if "T *" is relocatable. I.e., copyable with memcpy/memmove.
62 I.e., T is either trivially copyable, or void. */
63template<typename T>
64struct IsRelocatable
65 : gdb::Or<std::is_void<T>,
66 std::is_trivially_copyable<T>>
67{};
68
69/* True if both source and destination are relocatable. */
70
71template <typename D, typename S>
72using BothAreRelocatable
73 = gdb::And<IsRelocatable<D>, IsRelocatable<S>>;
74
75template <typename D, typename S,
76 typename = gdb::Requires<gdb::Not<BothAreRelocatable<D, S>>>>
77void *memcpy (D *dest, const S *src, size_t n) = delete;
78
79template <typename D, typename S,
80 typename = gdb::Requires<gdb::Not<BothAreRelocatable<D, S>>>>
81void *memmove (D *dest, const S *src, size_t n) = delete;
82
83#endif /* COMMON_POISON_H */
This page took 0.026842 seconds and 4 git commands to generate.