From 7e2a87a23899908971f9a86a00b1e8e65c80f943 Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Thu, 25 Feb 2021 13:19:48 -0500 Subject: [PATCH] Remove unnecessary checking and casting of sizeof when we need uint16_t sizes. (#4997) Compilers generate a constant for sizeof(), which converts cleanly to whatever integer size we need as long as the constant is small enough. --- src/app/server/RendezvousServer.cpp | 4 +--- src/transport/AdminPairingTable.cpp | 7 ++----- src/transport/StorablePeerConnection.cpp | 7 ++----- 3 files changed, 5 insertions(+), 13 deletions(-) diff --git a/src/app/server/RendezvousServer.cpp b/src/app/server/RendezvousServer.cpp index 3c3b32010e22f6..f8b8ff991d14f2 100644 --- a/src/app/server/RendezvousServer.cpp +++ b/src/app/server/RendezvousServer.cpp @@ -74,9 +74,7 @@ void RendezvousServer::OnRendezvousComplete() ChipLogError(AppServer, "Failed to store the connection state")); uint16_t nextKeyId = mRendezvousSession.GetNextKeyId(); - VerifyOrReturn(CanCastTo(sizeof(nextKeyId)), ChipLogError(AppServer, "Cannot cast the KeyID to uint16_t type")); - uint16_t size = static_cast(sizeof(nextKeyId)); - mStorage->SetKeyValue(kStorablePeerConnectionCountKey, &nextKeyId, size); + mStorage->SetKeyValue(kStorablePeerConnectionCountKey, &nextKeyId, sizeof(nextKeyId)); } void RendezvousServer::OnRendezvousStatusUpdate(Status status, CHIP_ERROR err) diff --git a/src/transport/AdminPairingTable.cpp b/src/transport/AdminPairingTable.cpp index b8ee7c71680006..394bb8cf268d5a 100644 --- a/src/transport/AdminPairingTable.cpp +++ b/src/transport/AdminPairingTable.cpp @@ -37,9 +37,7 @@ CHIP_ERROR AdminPairingInfo::StoreIntoKVS(PersistentStorageDelegate & kvs) info.mNodeId = Encoding::LittleEndian::HostSwap64(mNodeId); info.mAdmin = Encoding::LittleEndian::HostSwap16(mAdmin); - VerifyOrReturnError(CanCastTo(sizeof(info)), CHIP_ERROR_INTERNAL); - uint16_t size = static_cast(sizeof(info)); - return kvs.SetKeyValue(key, &info, size); + return kvs.SetKeyValue(key, &info, sizeof(info)); } CHIP_ERROR AdminPairingInfo::FetchFromKVS(PersistentStorageDelegate & kvs) @@ -49,8 +47,7 @@ CHIP_ERROR AdminPairingInfo::FetchFromKVS(PersistentStorageDelegate & kvs) StorableAdminPairingInfo info; - VerifyOrReturnError(CanCastTo(sizeof(info)), CHIP_ERROR_INTERNAL); - uint16_t size = static_cast(sizeof(info)); + uint16_t size = sizeof(info); ReturnErrorOnFailure(kvs.GetKeyValue(key, &info, size)); mNodeId = Encoding::LittleEndian::HostSwap64(info.mNodeId); diff --git a/src/transport/StorablePeerConnection.cpp b/src/transport/StorablePeerConnection.cpp index edb70b74f394b2..f5ba0a733d7f04 100644 --- a/src/transport/StorablePeerConnection.cpp +++ b/src/transport/StorablePeerConnection.cpp @@ -34,9 +34,7 @@ CHIP_ERROR StorablePeerConnection::StoreIntoKVS(PersistentStorageDelegate & kvs) char key[KeySize()]; ReturnErrorOnFailure(GenerateKey(mKeyId, key, sizeof(key))); - VerifyOrReturnError(CanCastTo(sizeof(mSession)), CHIP_ERROR_INTERNAL); - uint16_t size = static_cast(sizeof(mSession)); - return kvs.SetKeyValue(key, &mSession, size); + return kvs.SetKeyValue(key, &mSession, sizeof(mSession)); } CHIP_ERROR StorablePeerConnection::FetchFromKVS(PersistentStorageDelegate & kvs, uint16_t keyId) @@ -44,8 +42,7 @@ CHIP_ERROR StorablePeerConnection::FetchFromKVS(PersistentStorageDelegate & kvs, char key[KeySize()]; ReturnErrorOnFailure(GenerateKey(keyId, key, sizeof(key))); - VerifyOrReturnError(CanCastTo(sizeof(mSession)), CHIP_ERROR_INTERNAL); - uint16_t size = static_cast(sizeof(mSession)); + uint16_t size = sizeof(mSession); return kvs.GetKeyValue(key, &mSession, size); }