Преглед изворни кода

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>
kris/onging/CECPQ3_patch15
David Benjamin пре 7 година
committed by Adam Langley
родитељ
комит
2916430ddb
1 измењених фајлова са 11 додато и 4 уклоњено
  1. +11
    -4
      crypto/compiler_test.cc

+ 11
- 4
crypto/compiler_test.cc Прегледај датотеку

@@ -149,11 +149,11 @@ TEST(CompilerTest, IntegerRepresentation) {
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 @@ TEST(CompilerTest, PointerRepresentation) {
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)));
}

Loading…
Откажи
Сачувај