Skip to content

Commit

Permalink
TLV writer: Remove union UB
Browse files Browse the repository at this point in the history
It is undefined behaviour to use unions for type-punning in C++. Use
raw memcpy calls instead, as a substitute for the unavailable
std::bit_cast.
  • Loading branch information
anqid-g committed Jul 28, 2021
1 parent 68a2b7c commit 403d85b
Showing 1 changed file with 8 additions and 16 deletions.
24 changes: 8 additions & 16 deletions src/lib/core/CHIPTLVWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,26 +211,18 @@ CHIP_ERROR TLVWriter::Put(uint64_t tag, int64_t v, bool preserveSize)
return Put(tag, v);
}

CHIP_ERROR TLVWriter::Put(uint64_t tag, float v)
CHIP_ERROR TLVWriter::Put(uint64_t tag, const float v)
{
union
{
float f;
uint32_t u32;
} cvt;
cvt.f = v;
return WriteElementHead(TLVElementType::FloatingPointNumber32, tag, cvt.u32);
uint32_t u32;
memcpy(&u32, &v, sizeof(u32));
return WriteElementHead(TLVElementType::FloatingPointNumber32, tag, u32);
}

CHIP_ERROR TLVWriter::Put(uint64_t tag, double v)
CHIP_ERROR TLVWriter::Put(uint64_t tag, const double v)
{
union
{
double d;
uint64_t u64;
} cvt;
cvt.d = v;
return WriteElementHead(TLVElementType::FloatingPointNumber64, tag, cvt.u64);
uint64_t u64;
memcpy(&u64, &v, sizeof(u64));
return WriteElementHead(TLVElementType::FloatingPointNumber64, tag, u64);
}

CHIP_ERROR TLVWriter::Put(uint64_t tag, ByteSpan data)
Expand Down

0 comments on commit 403d85b

Please sign in to comment.