Skip to content

Commit 9580190

Browse files
committed
Merge #15391: Add compile time verification of assumptions we're currently making implicitly/tacitly
7cee858 Add compile time verification of assumptions we're currently making implicitly/tacitly (practicalswift) Pull request description: Add compile time verification of assumptions we're currently making implicitly/tacitly. As suggested by @sipa in bitcoin/bitcoin#14239 (comment) and @MarcoFalke in bitcoin/bitcoin#14479 (comment). Tree-SHA512: e68fe51164dbd3eeb76aa8a7e83dfcd3b4d5a66037c0f1822bbbd189bbe3c280e03b3b10af870880ecc09b612e62fb3d9bcd6cf1e16cb7ba818c257db0712ce4
2 parents eca1273 + 7cee858 commit 9580190

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ BITCOIN_CORE_H = \
118118
clientversion.h \
119119
coins.h \
120120
compat.h \
121+
compat/assumptions.h \
121122
compat/byteswap.h \
122123
compat/endian.h \
123124
compat/sanity.h \

src/compat/assumptions.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright (c) 2009-2010 Satoshi Nakamoto
2+
// Copyright (c) 2009-2019 The Bitcoin Core developers
3+
// Distributed under the MIT software license, see the accompanying
4+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
6+
// Compile-time verification of assumptions we make.
7+
8+
#ifndef BITCOIN_COMPAT_ASSUMPTIONS_H
9+
#define BITCOIN_COMPAT_ASSUMPTIONS_H
10+
11+
#include <limits>
12+
13+
// Assumption: We assume that the macro NDEBUG is not defined.
14+
// Example(s): We use assert(...) extensively with the assumption of it never
15+
// being a noop at runtime.
16+
#if defined(NDEBUG)
17+
# error "Bitcoin cannot be compiled without assertions."
18+
#endif
19+
20+
// Assumption: We assume the floating-point types to fulfill the requirements of
21+
// IEC 559 (IEEE 754) standard.
22+
// Example(s): Floating-point division by zero in ConnectBlock, CreateTransaction
23+
// and EstimateMedianVal.
24+
static_assert(std::numeric_limits<float>::is_iec559, "IEEE 754 float assumed");
25+
static_assert(std::numeric_limits<double>::is_iec559, "IEEE 754 double assumed");
26+
27+
// Assumption: We assume eight bits per byte (obviously, but remember: don't
28+
// trust -- verify!).
29+
// Example(s): Everywhere :-)
30+
static_assert(std::numeric_limits<unsigned char>::digits == 8, "8-bit byte assumed");
31+
32+
// Assumption: We assume floating-point widths.
33+
// Example(s): Type punning in serialization code (ser_{float,double}_to_uint{32,64}).
34+
static_assert(sizeof(float) == 4, "32-bit float assumed");
35+
static_assert(sizeof(double) == 8, "64-bit double assumed");
36+
37+
// Assumption: We assume integer widths.
38+
// Example(s): GetSizeOfCompactSize and WriteCompactSize in the serialization
39+
// code.
40+
static_assert(sizeof(short) == 2, "16-bit short assumed");
41+
static_assert(sizeof(int) == 4, "32-bit int assumed");
42+
43+
// Some important things we are NOT assuming (non-exhaustive list):
44+
// * We are NOT assuming a specific value for sizeof(std::size_t).
45+
// * We are NOT assuming a specific value for std::endian::native.
46+
// * We are NOT assuming a specific value for std::locale("").name().
47+
// * We are NOT assuming a specific value for std::numeric_limits<char>::is_signed.
48+
49+
#endif // BITCOIN_COMPAT_ASSUMPTIONS_H

src/util/system.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include <attributes.h>
1818
#include <compat.h>
19+
#include <compat/assumptions.h>
1920
#include <fs.h>
2021
#include <logging.h>
2122
#include <sync.h>

0 commit comments

Comments
 (0)