Test that nullptr has the obvious memory representation.
Apparently C does not promise this, only that casting zero to a pointer
gives NULL. No compiler will be insane enough to violate this, but it's
an easy assumption to document.
Change-Id: Ie255d42af655a4be07bcaf48ca90584a85c6aefd
Reviewed-on: https://boringssl-review.googlesource.com/18584
Commit-Queue: David Benjamin <davidben@google.com>
Commit-Queue: Adam Langley <agl@google.com>
Reviewed-by: Adam Langley <agl@google.com>
diff --git a/crypto/compiler_test.cc b/crypto/compiler_test.cc
index 2836276..29375a5 100644
--- a/crypto/compiler_test.cc
+++ b/crypto/compiler_test.cc
@@ -149,11 +149,11 @@
CheckRepresentation(static_cast<uint64_t>(0));
}
-// Converting pointers to integers and doing arithmetic on those values are both
-// defined. Converting those values back into pointers is undefined, but, for
-// aliasing checks, we require that the implementation-defined result of that
-// computation commutes with pointer arithmetic.
TEST(CompilerTest, PointerRepresentation) {
+ // Converting pointers to integers and doing arithmetic on those values are
+ // both defined. Converting those values back into pointers is undefined,
+ // but, for aliasing checks, we require that the implementation-defined
+ // result of that computation commutes with pointer arithmetic.
char chars[256];
for (size_t i = 0; i < sizeof(chars); i++) {
EXPECT_EQ(reinterpret_cast<uintptr_t>(chars) + i,
@@ -165,4 +165,11 @@
EXPECT_EQ(reinterpret_cast<uintptr_t>(ints) + i * sizeof(int),
reinterpret_cast<uintptr_t>(ints + i));
}
+
+ // nullptr must be represented by all zeros in memory. This is necessary so
+ // structs may be initialized by memset(0).
+ int *null = nullptr;
+ uint8_t bytes[sizeof(null)] = {0};
+ EXPECT_EQ(Bytes(bytes),
+ Bytes(reinterpret_cast<uint8_t *>(&null), sizeof(null)));
}