Skip to content

Commit

Permalink
Remove unnecessary checking and casting of sizeof when we need uint16…
Browse files Browse the repository at this point in the history
…_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.
  • Loading branch information
bzbarsky-apple authored Feb 25, 2021
1 parent 5ca7308 commit 7e2a87a
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 13 deletions.
4 changes: 1 addition & 3 deletions src/app/server/RendezvousServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ void RendezvousServer::OnRendezvousComplete()
ChipLogError(AppServer, "Failed to store the connection state"));

uint16_t nextKeyId = mRendezvousSession.GetNextKeyId();
VerifyOrReturn(CanCastTo<uint16_t>(sizeof(nextKeyId)), ChipLogError(AppServer, "Cannot cast the KeyID to uint16_t type"));
uint16_t size = static_cast<uint16_t>(sizeof(nextKeyId));
mStorage->SetKeyValue(kStorablePeerConnectionCountKey, &nextKeyId, size);
mStorage->SetKeyValue(kStorablePeerConnectionCountKey, &nextKeyId, sizeof(nextKeyId));
}

void RendezvousServer::OnRendezvousStatusUpdate(Status status, CHIP_ERROR err)
Expand Down
7 changes: 2 additions & 5 deletions src/transport/AdminPairingTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ CHIP_ERROR AdminPairingInfo::StoreIntoKVS(PersistentStorageDelegate & kvs)
info.mNodeId = Encoding::LittleEndian::HostSwap64(mNodeId);
info.mAdmin = Encoding::LittleEndian::HostSwap16(mAdmin);

VerifyOrReturnError(CanCastTo<uint16_t>(sizeof(info)), CHIP_ERROR_INTERNAL);
uint16_t size = static_cast<uint16_t>(sizeof(info));
return kvs.SetKeyValue(key, &info, size);
return kvs.SetKeyValue(key, &info, sizeof(info));
}

CHIP_ERROR AdminPairingInfo::FetchFromKVS(PersistentStorageDelegate & kvs)
Expand All @@ -49,8 +47,7 @@ CHIP_ERROR AdminPairingInfo::FetchFromKVS(PersistentStorageDelegate & kvs)

StorableAdminPairingInfo info;

VerifyOrReturnError(CanCastTo<uint16_t>(sizeof(info)), CHIP_ERROR_INTERNAL);
uint16_t size = static_cast<uint16_t>(sizeof(info));
uint16_t size = sizeof(info);
ReturnErrorOnFailure(kvs.GetKeyValue(key, &info, size));

mNodeId = Encoding::LittleEndian::HostSwap64(info.mNodeId);
Expand Down
7 changes: 2 additions & 5 deletions src/transport/StorablePeerConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,15 @@ CHIP_ERROR StorablePeerConnection::StoreIntoKVS(PersistentStorageDelegate & kvs)
char key[KeySize()];
ReturnErrorOnFailure(GenerateKey(mKeyId, key, sizeof(key)));

VerifyOrReturnError(CanCastTo<uint16_t>(sizeof(mSession)), CHIP_ERROR_INTERNAL);
uint16_t size = static_cast<uint16_t>(sizeof(mSession));
return kvs.SetKeyValue(key, &mSession, size);
return kvs.SetKeyValue(key, &mSession, sizeof(mSession));
}

CHIP_ERROR StorablePeerConnection::FetchFromKVS(PersistentStorageDelegate & kvs, uint16_t keyId)
{
char key[KeySize()];
ReturnErrorOnFailure(GenerateKey(keyId, key, sizeof(key)));

VerifyOrReturnError(CanCastTo<uint16_t>(sizeof(mSession)), CHIP_ERROR_INTERNAL);
uint16_t size = static_cast<uint16_t>(sizeof(mSession));
uint16_t size = sizeof(mSession);
return kvs.GetKeyValue(key, &mSession, size);
}

Expand Down

0 comments on commit 7e2a87a

Please sign in to comment.