Replaced dangerous uses of sizeof()
This patch replaces several uses of sizeof with calls to std::array's
size method.
Bug: 153655228
Test: Compiled with changed static asserts
Test: Treehugger
Change-Id: I4cf87f2d7fffe0d7c81e0652688bedd4d5c9c5c2
diff --git a/libnativeloader/public_libraries.cpp b/libnativeloader/public_libraries.cpp
index 9026546..258dbf6 100644
--- a/libnativeloader/public_libraries.cpp
+++ b/libnativeloader/public_libraries.cpp
@@ -226,7 +226,7 @@
}
static std::string InitArtPublicLibraries() {
- CHECK_GT((int)sizeof(kArtApexPublicLibraries), 0);
+ CHECK_GT((int)kArtApexPublicLibraries.size(), 0);
std::string list = android::base::Join(kArtApexPublicLibraries, ":");
std::string additional_libs = additional_public_libraries();
diff --git a/runtime/oat.cc b/runtime/oat.cc
index 17c797a..723bf49 100644
--- a/runtime/oat.cc
+++ b/runtime/oat.cc
@@ -80,9 +80,9 @@
quick_to_interpreter_bridge_offset_(0) {
// Don't want asserts in header as they would be checked in each file that includes it. But the
// fields are private, so we check inside a method.
- static_assert(sizeof(magic_) == sizeof(kOatMagic),
+ static_assert(decltype(magic_)().size() == kOatMagic.size(),
"Oat magic and magic_ have different lengths.");
- static_assert(sizeof(version_) == sizeof(kOatVersion),
+ static_assert(decltype(version_)().size() == kOatVersion.size(),
"Oat version and version_ have different lengths.");
magic_ = kOatMagic;
@@ -112,13 +112,13 @@
std::string OatHeader::GetValidationErrorMessage() const {
if (magic_ != kOatMagic) {
- static_assert(sizeof(kOatMagic) == 4, "kOatMagic has unexpected length");
+ static_assert(kOatMagic.size() == 4, "kOatMagic has unexpected length");
return StringPrintf("Invalid oat magic, expected 0x%02x%02x%02x%02x, got 0x%02x%02x%02x%02x.",
kOatMagic[0], kOatMagic[1], kOatMagic[2], kOatMagic[3],
magic_[0], magic_[1], magic_[2], magic_[3]);
}
if (version_ != kOatVersion) {
- static_assert(sizeof(kOatVersion) == 4, "kOatVersion has unexpected length");
+ static_assert(kOatVersion.size() == 4, "kOatVersion has unexpected length");
return StringPrintf("Invalid oat version, expected 0x%02x%02x%02x%02x, got 0x%02x%02x%02x%02x.",
kOatVersion[0], kOatVersion[1], kOatVersion[2], kOatVersion[3],
version_[0], version_[1], version_[2], version_[3]);