reinterpret_cast-based type-punning is almost always UB

This commit is contained in:
jadebenn
2024-11-23 02:29:11 -08:00
parent 8eb3488812
commit 7740bbbaab
5 changed files with 14 additions and 11 deletions

View File

@@ -159,8 +159,7 @@ void RakNet::BitStream::Write<AMFIntValue&>(AMFIntValue& value) {
// Writes an AMFDoubleValue to BitStream
template<>
void RakNet::BitStream::Write<AMFDoubleValue&>(AMFDoubleValue& value) {
double d = value.GetValue();
WriteAMFU64(*this, *reinterpret_cast<uint64_t*>(&d));
WriteAMFU64(*this, std::bit_cast<uint64_t>(value.GetValue()));
}
// Writes an AMFStringValue to BitStream

View File

@@ -146,7 +146,7 @@ void WriteSd0Magic(char* input, uint32_t chunkSize) {
input[2] = '0';
input[3] = 0x01;
input[4] = 0xFF;
*reinterpret_cast<uint32_t*>(input + 5) = chunkSize; // Write the integer to the character array
std::memcpy(&input[5], &chunkSize, sizeof(uint32_t)); // Write the integer to the character array
}
bool CheckSd0Magic(std::istream& streamToCheck) {

View File

@@ -53,7 +53,7 @@ bool static _IsSuffixChar(const uint8_t c) {
bool GeneralUtils::details::_NextUTF8Char(std::string_view& slice, uint32_t& out) {
const size_t rem = slice.length();
if (slice.empty()) return false;
const uint8_t* bytes = reinterpret_cast<const uint8_t*>(&slice.front());
const auto* bytes = &slice.front();
if (rem > 0) {
const uint8_t first = bytes[0];
if (first < 0x80) { // 1 byte character