Skip to content

Commit

Permalink
fix: debug build from solution and improve NetworkMessage::add function
Browse files Browse the repository at this point in the history
  • Loading branch information
dudantas committed Sep 30, 2024
1 parent 04fa248 commit 0db73b2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
29 changes: 24 additions & 5 deletions src/server/network/message/networkmessage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,41 @@ class NetworkMessage {

template <typename T>
void add(T value, std::source_location location = std::source_location::current()) {
// Check if there is enough space to add the value to the buffer
if (!canAdd(sizeof(T))) {
g_logger().error("Cannot add value of size {}, buffer overflow", sizeof(T));
g_logger().error("Cannot add value of size {}, buffer overflow. Called at line '{}:{}' in '{}'",
sizeof(T), buffer.size(), location.line(), location.column(), location.function_name());
return;
}

g_logger().trace("[{}] called line '{}:{}' in '{}'", __FUNCTION__, location.line(), location.column(), location.function_name());
// Verify if the buffer has enough space to accommodate the value
if (info.position + sizeof(T) > buffer.size()) {
g_logger().error("Buffer overflow detected, current position: {}, value size: {}, buffer size: {}. Called at line '{}:{}' in '{}'",
info.position, sizeof(T), buffer.size(), location.line(), location.column(), location.function_name());
return;
}

// Ensure that T is trivially copyable
static_assert(std::is_trivially_copyable_v<T>, "Type T must be trivially copyable");

// Convert the value to an array of unsigned char using std::bit_cast
auto byteArray = std::bit_cast<std::array<unsigned char, sizeof(T)>>(value);

// Create a span from the byte array
std::span<const unsigned char> byteSpan(byteArray);
// Copy the bytes into the buffer
auto it = std::ranges::copy(byteSpan.begin(), byteSpan.end(), buffer.begin() + info.position);
g_logger().trace("First value copied from sourceSpan: {}, second value copied from sourceSpan: {}", *it.in, *it.out);

// Check if the size of byteSpan can fit into the buffer
if (byteSpan.size() > (buffer.size() - info.position)) {
g_logger().error("Buffer overflow during span copy. Source span size: {}, buffer available space: {}",
byteSpan.size(), buffer.size() - info.position);
return;
}

g_logger().trace("[{}] called at line '{}:{}' in '{}'", __FUNCTION__, location.line(), location.column(), location.function_name());

// Copy the bytes into the buffer at the correct position
std::ranges::copy(byteSpan, buffer.begin() + info.position);

info.position += sizeof(T);
info.length += sizeof(T);
}
Expand Down
3 changes: 2 additions & 1 deletion vcproj/canary.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@
<WarningLevel>Level1</WarningLevel>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<LanguageStandard>stdcpp20</LanguageStandard>
<PrecompiledHeaderFile />
<PrecompiledHeaderFile>pch.hpp</PrecompiledHeaderFile>
<SupportJustMyCode>true</SupportJustMyCode>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
<WholeProgramOptimization>true</WholeProgramOptimization>
Expand All @@ -517,6 +517,7 @@
<LanguageStandard_C>Default</LanguageStandard_C>
<AdditionalOptions>/Zc:__cplusplus /fsanitize=address %(AdditionalOptions)</AdditionalOptions>
<PreprocessorDefinitions>_DISABLE_VECTOR_ANNOTATION;_DISABLE_STRING_ANNOTATION;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PrecompiledHeader>Use</PrecompiledHeader>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand Down

0 comments on commit 0db73b2

Please sign in to comment.