Skip to content

Commit 2f501fb

Browse files
committed
Merge #15522: Document sizeof(size_t) assumptions and compiler assumptions in assumptions.h
c7a7250 Document assumptions about C++ compiler (practicalswift) c7ea8d3 Add sizeof(size_t) assumptions (practicalswift) Pull request description: Document `sizeof(size_t)` assumptions and compiler assumptions by adding compile-time checks in `assumptions.h`. Tree-SHA512: db46481eecad6a87718ae637a7761d39d32cfe6f95fc8ad2b3a52a3d966c2a05c8f540dd3f362721279816571b04b6cce2de9b3b1d17606d7b197126cd4a8d1f
2 parents 165ea14 + c7a7250 commit 2f501fb

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/compat/assumptions.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@
1717
# error "Bitcoin cannot be compiled without assertions."
1818
#endif
1919

20+
// Assumption: We assume a C++11 (ISO/IEC 14882:2011) compiler (minimum requirement).
21+
// Example(s): We assume the presence of C++11 features everywhere :-)
22+
// Note: MSVC does not report the expected __cplusplus value due to legacy
23+
// reasons.
24+
#if !defined(_MSC_VER)
25+
// ISO Standard C++11 [cpp.predefined]p1:
26+
// "The name __cplusplus is defined to the value 201103L when compiling a C++
27+
// translation unit."
28+
static_assert(__cplusplus >= 201103L, "C++11 standard assumed");
29+
#endif
30+
2031
// Assumption: We assume the floating-point types to fulfill the requirements of
2132
// IEC 559 (IEEE 754) standard.
2233
// Example(s): Floating-point division by zero in ConnectBlock, CreateTransaction
@@ -40,8 +51,13 @@ static_assert(sizeof(double) == 8, "64-bit double assumed");
4051
static_assert(sizeof(short) == 2, "16-bit short assumed");
4152
static_assert(sizeof(int) == 4, "32-bit int assumed");
4253

54+
// Assumption: We assume size_t to be 32-bit or 64-bit.
55+
// Example(s): size_t assumed to be at least 32-bit in ecdsa_signature_parse_der_lax(...).
56+
// size_t assumed to be 32-bit or 64-bit in MallocUsage(...).
57+
static_assert(sizeof(size_t) == 4 || sizeof(size_t) == 8, "size_t assumed to be 32-bit or 64-bit");
58+
static_assert(sizeof(size_t) == sizeof(void*), "Sizes of size_t and void* assumed to be equal");
59+
4360
// Some important things we are NOT assuming (non-exhaustive list):
44-
// * We are NOT assuming a specific value for sizeof(std::size_t).
4561
// * We are NOT assuming a specific value for std::endian::native.
4662
// * We are NOT assuming a specific value for std::locale("").name().
4763
// * We are NOT assuming a specific value for std::numeric_limits<char>::is_signed.

0 commit comments

Comments
 (0)