Skip to content

Commit

Permalink
Add compile time verification of assumptions we're currently making i…
Browse files Browse the repository at this point in the history
…mplicitly/tacitly
  • Loading branch information
practicalswift committed Feb 12, 2019
1 parent 1bc149d commit 1ea39ed
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Makefile.am
Expand Up @@ -102,6 +102,7 @@ endif
BITCOIN_CORE_H = \ BITCOIN_CORE_H = \
addrdb.h \ addrdb.h \
addrman.h \ addrman.h \
assumptions.h \
attributes.h \ attributes.h \
banman.h \ banman.h \
base58.h \ base58.h \
Expand Down
29 changes: 29 additions & 0 deletions src/assumptions.h
@@ -0,0 +1,29 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2019 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

// Compile-time verification of assumptions we make.

#ifndef BITCOIN_ASSUMPTIONS_H
#define BITCOIN_ASSUMPTIONS_H

#include <climits>
#include <limits>

// Assumption: We assume the floating-point types to fulfill the requirements of IEC 559 (IEEE 754) standard.
// Example(s): Floating-point division by zero in ConnectBlock, CreateTransaction and EstimateMedianVal.
static_assert(std::numeric_limits<float>::is_iec559, "IEEE 754 float assumed");
static_assert(std::numeric_limits<double>::is_iec559, "IEEE 754 double assumed");

// Assumption: We assume floating-point widths.
// Example(s): Type punning in serialization code (ser_{float,double}_to_uint{32,64}).
static_assert(sizeof(float) * CHAR_BIT == 32 && CHAR_BIT == 8, "32-bit float assumed");
static_assert(sizeof(double) * CHAR_BIT == 64 && CHAR_BIT == 8, "64-bit double assumed");

// Assumption: We assume integer widths.
// Example(s): GetSizeOfCompactSize and WriteCompactSize in the serialization code.
static_assert(sizeof(short) * CHAR_BIT == 16 && CHAR_BIT == 8, "16-bit short assumed");
static_assert(sizeof(int) * CHAR_BIT == 32 && CHAR_BIT == 8, "32-bit int assumed");

#endif // BITCOIN_ASSUMPTIONS_H
1 change: 1 addition & 0 deletions src/util/system.h
Expand Up @@ -14,6 +14,7 @@
#include <config/bitcoin-config.h> #include <config/bitcoin-config.h>
#endif #endif


#include <assumptions.h>
#include <attributes.h> #include <attributes.h>
#include <compat.h> #include <compat.h>
#include <fs.h> #include <fs.h>
Expand Down

0 comments on commit 1ea39ed

Please sign in to comment.