Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix casting in TLVReader::Get to be safe. #3099

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3,393 changes: 3,393 additions & 0 deletions src/lib/Makefile.in

Large diffs are not rendered by default.

15 changes: 8 additions & 7 deletions src/lib/core/CHIPTLVReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <core/CHIPEncoding.h>
#include <core/CHIPTLV.h>
#include <support/CodeUtils.h>
#include <support/SafeInt.h>
#include <system/SystemPacketBuffer.h>

namespace chip {
Expand Down Expand Up @@ -370,7 +371,7 @@ CHIP_ERROR TLVReader::Get(int8_t & v)
{
uint64_t v64 = 0;
CHIP_ERROR err = Get(v64);
v = v64;
v = CastToSigned(static_cast<uint8_t>(v64));
return err;
}

Expand All @@ -391,7 +392,7 @@ CHIP_ERROR TLVReader::Get(int16_t & v)
{
uint64_t v64 = 0;
CHIP_ERROR err = Get(v64);
v = v64;
v = CastToSigned(static_cast<uint16_t>(v64));
return err;
}

Expand All @@ -412,7 +413,7 @@ CHIP_ERROR TLVReader::Get(int32_t & v)
{
uint64_t v64 = 0;
CHIP_ERROR err = Get(v64);
v = v64;
v = CastToSigned(static_cast<uint32_t>(v64));
return err;
}

Expand All @@ -433,7 +434,7 @@ CHIP_ERROR TLVReader::Get(int64_t & v)
{
uint64_t v64 = 0;
CHIP_ERROR err = Get(v64);
v = v64;
v = CastToSigned(v64);
return err;
}

Expand Down Expand Up @@ -520,13 +521,13 @@ CHIP_ERROR TLVReader::Get(uint64_t & v)
switch (ElementType())
{
case kTLVElementType_Int8:
v = static_cast<int64_t>(static_cast<int8_t>(mElemLenOrVal));
v = static_cast<uint64_t>(static_cast<int64_t>(CastToSigned(static_cast<uint8_t>(mElemLenOrVal))));
break;
case kTLVElementType_Int16:
v = static_cast<int64_t>(static_cast<int16_t>(mElemLenOrVal));
v = static_cast<uint64_t>(static_cast<int64_t>(CastToSigned(static_cast<uint16_t>(mElemLenOrVal))));
break;
case kTLVElementType_Int32:
v = static_cast<int64_t>(static_cast<int32_t>(mElemLenOrVal));
v = static_cast<uint64_t>(static_cast<int64_t>(CastToSigned(static_cast<uint32_t>(mElemLenOrVal))));
break;
case kTLVElementType_Int64:
case kTLVElementType_UInt8:
Expand Down
Loading