Skip to content

Commit

Permalink
created FromBitsUnchecked memcpy wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
jadebenn committed Nov 25, 2024
1 parent 6fa719c commit 66fa3ff
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
21 changes: 21 additions & 0 deletions dCommon/GeneralUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// C++
#include <charconv>
#include <cstdint>
#include <cstring>
#include <ctime>
#include <functional>
#include <optional>
Expand Down Expand Up @@ -152,6 +153,26 @@ namespace GeneralUtils {
char_pointer_hash
>;

/**
* A convenience wrapper around std::memcpy that creates a new object
* from the value representation of the provided bytes.Use when
* std::bit_cast is not applicable.
* @warning All restrictions of std::memcpy still apply. Accessing
* outside of the source object is undefined behavior.
* @param from The source of the value representation
* @returns A new object with the given value representation
*/
template <typename To, typename From>
[[nodiscard]]
inline To FromBitsUnchecked(const From* from) noexcept
requires (std::is_trivially_copyable_v<To>
&& std::is_trivially_copyable_v<From>)
{
To to{};
std::memcpy(&to, from, sizeof(To));
return to;
}

// Concept constraining to enum types
template <typename T>
concept Enum = std::is_enum_v<T>;
Expand Down
1 change: 1 addition & 0 deletions dCommon/dClient/AssetManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ struct AssetStream : std::istream {
}

operator bool() {
// NEED TO FIX THIS
return reinterpret_cast<AssetMemoryBuffer*>(rdbuf())->m_Success;

Check warning on line 58 in dCommon/dClient/AssetManager.h

View workflow job for this annotation

GitHub Actions / Build & Test (ubuntu-22.04)

dereferencing type-punned pointer might break strict-aliasing rules [-Wstrict-aliasing]

Check warning on line 58 in dCommon/dClient/AssetManager.h

View workflow job for this annotation

GitHub Actions / Build & Test (ubuntu-22.04)

dereferencing type-punned pointer might break strict-aliasing rules [-Wstrict-aliasing]

Check warning on line 58 in dCommon/dClient/AssetManager.h

View workflow job for this annotation

GitHub Actions / Build & Test (ubuntu-22.04)

dereferencing type-punned pointer might break strict-aliasing rules [-Wstrict-aliasing]

Check warning on line 58 in dCommon/dClient/AssetManager.h

View workflow job for this annotation

GitHub Actions / Build & Test (ubuntu-22.04)

dereferencing type-punned pointer might break strict-aliasing rules [-Wstrict-aliasing]

Check warning on line 58 in dCommon/dClient/AssetManager.h

View workflow job for this annotation

GitHub Actions / Build & Test (ubuntu-22.04)

dereferencing type-punned pointer might break strict-aliasing rules [-Wstrict-aliasing]

Check warning on line 58 in dCommon/dClient/AssetManager.h

View workflow job for this annotation

GitHub Actions / Build & Test (ubuntu-22.04)

dereferencing type-punned pointer might break strict-aliasing rules [-Wstrict-aliasing]

Check warning on line 58 in dCommon/dClient/AssetManager.h

View workflow job for this annotation

GitHub Actions / Build & Test (ubuntu-22.04)

dereferencing type-punned pointer might break strict-aliasing rules [-Wstrict-aliasing]

Check warning on line 58 in dCommon/dClient/AssetManager.h

View workflow job for this annotation

GitHub Actions / Build & Test (ubuntu-22.04)

dereferencing type-punned pointer might break strict-aliasing rules [-Wstrict-aliasing]

Check warning on line 58 in dCommon/dClient/AssetManager.h

View workflow job for this annotation

GitHub Actions / Build & Test (ubuntu-22.04)

dereferencing type-punned pointer might break strict-aliasing rules [-Wstrict-aliasing]

Check warning on line 58 in dCommon/dClient/AssetManager.h

View workflow job for this annotation

GitHub Actions / Build & Test (ubuntu-22.04)

dereferencing type-punned pointer might break strict-aliasing rules [-Wstrict-aliasing]
}
};
Expand Down
9 changes: 4 additions & 5 deletions dScripts/02_server/Map/AG/NpcAgCourseStarter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,10 @@ void NpcAgCourseStarter::OnFireEventServerSide(Entity* self, Entity* sender, std
scriptedActivityComponent->RemoveActivityPlayerData(sender->GetObjectID());
} else if (args == "course_finish") {
const time_t endTime = std::time(0);

// Using memcpy since misaligned reads are UB
time_t startTime{};
std::memcpy(&startTime, &data->values[1], sizeof(time_t));
const time_t finish = (endTime - startTime);

// Using FromBitsUnchecked to create new time_t object since misaligned reads are UB
const time_t startTime = GeneralUtils::FromBitsUnchecked<time_t>(&data->values[1]);
const time_t finish = endTime - startTime;

std::memcpy(&data->values[2], &finish, sizeof(float));

Expand Down
5 changes: 2 additions & 3 deletions dWorldServer/WorldServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1396,9 +1396,8 @@ void HandlePacket(Packet* packet) {
}

default:
// Need to use memcpy instead of reinterpret_cast to avoid misaligned reads, which are UB
auto messageId = MessageType::World::INVALID;
std::memcpy(&messageId, &packet->data[3], sizeof(MessageType::World));
// Need to use FromBitsUnchecked (aka memcpy) instead of reinterpret_cast to avoid misaligned reads, which are UB
const auto messageId = GeneralUtils::FromBitsUnchecked<MessageType::World>(&packet->data[3]);

const std::string_view messageIdString = StringifiedEnum::ToString(messageId);
LOG("Unknown world packet received: %4i, %s", messageId, messageIdString.data());
Expand Down

0 comments on commit 66fa3ff

Please sign in to comment.