fix: Remove instances of undefined behavior

This commit is contained in:
jadebenn 2024-11-21 02:05:29 -06:00
parent 53877a0bc3
commit 30d4076808
15 changed files with 71 additions and 65 deletions

1
.gitignore vendored
View File

@ -2,6 +2,7 @@ temp/
cmake-build-debug/
RelWithDebInfo/
docker/configs
valgrind-out.txt
# Third party libraries
thirdparty/mysql/

View File

@ -4,6 +4,10 @@ project(Darkflame
LANGUAGES C CXX
)
#add_compile_options("-fsanitize=address,undefined")
add_compile_options("-fsanitize=undefined" "-fvisibility=default")
add_link_options("-fsanitize=undefined" "-static-libsan")
# check if the path to the source directory contains a space
if("${CMAKE_SOURCE_DIR}" MATCHES " ")
message(FATAL_ERROR "The server cannot build in the path (" ${CMAKE_SOURCE_DIR} ") because it contains a space. Please move the server to a path without spaces.")
@ -17,7 +21,7 @@ set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # Export the compile commands for debugging
set(CMAKE_POLICY_DEFAULT_CMP0063 NEW) # Set CMAKE visibility policy to NEW on project and subprojects
set(CMAKE_VISIBILITY_INLINES_HIDDEN ON) # Set C and C++ symbol visibility to hide inlined functions
set(CMAKE_VISIBILITY_INLINES_HIDDEN OFF) # Set C and C++ symbol visibility to hide inlined functions
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
# Read variables from file

View File

@ -4,6 +4,7 @@
#include "dCommonVars.h"
#include "Logger.h"
#include "Game.h"
#include "GeneralUtils.h"
#include <unordered_map>
#include <vector>
@ -63,6 +64,10 @@ template class AMFValue<uint32_t>;
template class AMFValue<std::string>;
template class AMFValue<double>;
// Blank specialization to make sure AMFValue<const char*> fails
template <>
class AMFValue<const char*> {};
// AMFValue template class member function instantiations
template <> [[nodiscard]] constexpr eAmf AMFValue<std::nullptr_t>::GetValueType() const noexcept { return eAmf::Null; }
template <> [[nodiscard]] constexpr eAmf AMFValue<bool>::GetValueType() const noexcept { return m_Data ? eAmf::True : eAmf::False; }
@ -74,22 +79,6 @@ template <> [[nodiscard]] constexpr eAmf AMFValue<double>::GetValueType() const
template <typename ValueType>
[[nodiscard]] constexpr eAmf AMFValue<ValueType>::GetValueType() const noexcept { return eAmf::Undefined; }
// As a string this is much easier to write and read from a BitStream.
template <>
class AMFValue<const char*> : public AMFBaseValue {
public:
AMFValue() = default;
AMFValue(const char* value) { m_Data = value; }
virtual ~AMFValue() override = default;
[[nodiscard]] constexpr eAmf GetValueType() const noexcept override { return eAmf::String; }
[[nodiscard]] const std::string& GetValue() const { return m_Data; }
void SetValue(const std::string& value) { m_Data = value; }
protected:
std::string m_Data;
};
using AMFNullValue = AMFValue<std::nullptr_t>;
using AMFBoolValue = AMFValue<bool>;
using AMFIntValue = AMFValue<int32_t>;

View File

@ -10,40 +10,54 @@ void RakNet::BitStream::Write<AMFBaseValue&>(AMFBaseValue& value) {
this->Write(type);
switch (type) {
case eAmf::Integer: {
this->Write<AMFIntValue&>(*static_cast<AMFIntValue*>(&value));
this->Write<AMFIntValue&>(static_cast<AMFIntValue&>(value));
break;
}
case eAmf::Double: {
this->Write<AMFDoubleValue&>(*static_cast<AMFDoubleValue*>(&value));
this->Write<AMFDoubleValue&>(static_cast<AMFDoubleValue&>(value));
break;
}
case eAmf::String: {
this->Write<AMFStringValue&>(*static_cast<AMFStringValue*>(&value));
this->Write<AMFStringValue&>(static_cast<AMFStringValue&>(value));
break;
}
case eAmf::Array: {
this->Write<AMFArrayValue&>(*static_cast<AMFArrayValue*>(&value));
this->Write<AMFArrayValue&>(static_cast<AMFArrayValue&>(value));
break;
}
default: {
LOG("Encountered unwritable AMFType %i!", type);
[[fallthrough]];
}
case eAmf::Undefined:
[[fallthrough]];
case eAmf::Null:
[[fallthrough]];
case eAmf::False:
[[fallthrough]];
case eAmf::True:
[[fallthrough]];
case eAmf::Date:
[[fallthrough]];
case eAmf::Object:
[[fallthrough]];
case eAmf::XML:
[[fallthrough]];
case eAmf::XMLDoc:
[[fallthrough]];
case eAmf::ByteArray:
[[fallthrough]];
case eAmf::VectorInt:
[[fallthrough]];
case eAmf::VectorUInt:
[[fallthrough]];
case eAmf::VectorDouble:
[[fallthrough]];
case eAmf::VectorObject:
[[fallthrough]];
case eAmf::Dictionary:
break;
}

View File

@ -258,8 +258,8 @@ void DestroyableComponent::SetMaxHealth(float value, bool playAnim) {
if (!characterComponent) return;
AMFArrayValue args;
args.Insert("amount", std::to_string(difference));
args.Insert("type", "health");
args.Insert<std::string>("amount", std::to_string(difference));
args.Insert<std::string>("type", "health");
GameMessages::SendUIMessageServerToSingleClient(m_Parent, characterComponent->GetSystemAddress(), "MaxPlayerBarUpdate", args);
}
@ -300,8 +300,8 @@ void DestroyableComponent::SetMaxArmor(float value, bool playAnim) {
if (!characterComponent) return;
AMFArrayValue args;
args.Insert("amount", std::to_string(value));
args.Insert("type", "armor");
args.Insert<std::string>("amount", std::to_string(value));
args.Insert<std::string>("type", "armor");
GameMessages::SendUIMessageServerToSingleClient(m_Parent, characterComponent->GetSystemAddress(), "MaxPlayerBarUpdate", args);
}
@ -341,8 +341,8 @@ void DestroyableComponent::SetMaxImagination(float value, bool playAnim) {
if (!characterComponent) return;
AMFArrayValue args;
args.Insert("amount", std::to_string(difference));
args.Insert("type", "imagination");
args.Insert<std::string>("amount", std::to_string(difference));
args.Insert<std::string>("type", "imagination");
GameMessages::SendUIMessageServerToSingleClient(m_Parent, characterComponent->GetSystemAddress(), "MaxPlayerBarUpdate", args);
}

View File

@ -36,7 +36,7 @@ void PropertyEntranceComponent::OnUse(Entity* entity) {
AMFArrayValue args;
args.Insert("state", "property_menu");
args.Insert<std::string>("state", "property_menu");
GameMessages::SendUIMessageServerToSingleClient(entity, entity->GetSystemAddress(), "pushGameState", args);
}

View File

@ -98,7 +98,7 @@ namespace GMZeroCommands {
{
AMFArrayValue args;
args.Insert("state", "Story");
args.Insert<std::string>("state", "Story");
GameMessages::SendUIMessageServerToSingleClient(entity, entity->GetSystemAddress(), "pushGameState", args);
}
@ -121,7 +121,7 @@ namespace GMZeroCommands {
{
AMFArrayValue args;
args.Insert("state", "Story");
args.Insert<std::string>("state", "Story");
GameMessages::SendUIMessageServerToSingleClient(entity, entity->GetSystemAddress(), "pushGameState", args);
}

View File

@ -6,7 +6,7 @@
void BankInteractServer::OnUse(Entity* self, Entity* user) {
AMFArrayValue args;
args.Insert("state", "bank");
args.Insert<std::string>("state", "bank");
GameMessages::SendUIMessageServerToSingleClient(user, user->GetSystemAddress(), "pushGameState", args);
}

View File

@ -6,7 +6,7 @@
void MailBoxServer::OnUse(Entity* self, Entity* user) {
AMFArrayValue args;
args.Insert("state", "Mail");
args.Insert<std::string>("state", "Mail");
GameMessages::SendUIMessageServerToSingleClient(user, user->GetSystemAddress(), "pushGameState", args);
}

View File

@ -12,7 +12,7 @@ void StoryBoxInteractServer::OnUse(Entity* self, Entity* user) {
{
AMFArrayValue args;
args.Insert("state", "Story");
args.Insert<std::string>("state", "Story");
GameMessages::SendUIMessageServerToSingleClient(user, user->GetSystemAddress(), "pushGameState", args);
}

View File

@ -13,27 +13,27 @@ void NsLegoClubDoor::OnStartup(Entity* self) {
args = {};
args.Insert("callbackClient", std::to_string(self->GetObjectID()));
args.Insert("strIdentifier", "choiceDoor");
args.Insert("title", "%[UI_CHOICE_DESTINATION]");
args.Insert<std::string>("strIdentifier", "choiceDoor");
args.Insert<std::string>("title", "%[UI_CHOICE_DESTINATION]");
AMFArrayValue* choiceOptions = args.InsertArray("options");
{
AMFArrayValue* nsArgs = choiceOptions->PushArray();
nsArgs->Insert("image", "textures/ui/zone_thumnails/Nimbus_Station.dds");
nsArgs->Insert("caption", "%[UI_CHOICE_NS]");
nsArgs->Insert("identifier", "zoneID_1200");
nsArgs->Insert("tooltipText", "%[UI_CHOICE_NS_HOVER]");
nsArgs->Insert<std::string>("image", "textures/ui/zone_thumnails/Nimbus_Station.dds");
nsArgs->Insert<std::string>("caption", "%[UI_CHOICE_NS]");
nsArgs->Insert<std::string>("identifier", "zoneID_1200");
nsArgs->Insert<std::string>("tooltipText", "%[UI_CHOICE_NS_HOVER]");
}
{
AMFArrayValue* ntArgs = choiceOptions->PushArray();
ntArgs->Insert("image", "textures/ui/zone_thumnails/Nexus_Tower.dds");
ntArgs->Insert("caption", "%[UI_CHOICE_NT]");
ntArgs->Insert("identifier", "zoneID_1900");
ntArgs->Insert("tooltipText", "%[UI_CHOICE_NT_HOVER]");
ntArgs->Insert<std::string>("image", "textures/ui/zone_thumnails/Nexus_Tower.dds");
ntArgs->Insert<std::string>("caption", "%[UI_CHOICE_NT]");
ntArgs->Insert<std::string>("identifier", "zoneID_1900");
ntArgs->Insert<std::string>("tooltipText", "%[UI_CHOICE_NT_HOVER]");
}
options = choiceOptions;
@ -46,8 +46,8 @@ void NsLegoClubDoor::OnUse(Entity* self, Entity* user) {
AMFArrayValue multiArgs;
multiArgs.Insert("callbackClient", std::to_string(self->GetObjectID()));
multiArgs.Insert("strIdentifier", "choiceDoor");
multiArgs.Insert("title", "%[UI_CHOICE_DESTINATION]");
multiArgs.Insert<std::string>("strIdentifier", "choiceDoor");
multiArgs.Insert<std::string>("title", "%[UI_CHOICE_DESTINATION]");
multiArgs.Insert("options", static_cast<AMFBaseValue*>(options));
GameMessages::SendUIMessageServerToSingleClient(player, player->GetSystemAddress(), "QueueChoiceBox", multiArgs);
@ -55,13 +55,13 @@ void NsLegoClubDoor::OnUse(Entity* self, Entity* user) {
multiArgs.Remove("options", false); // We do not want the local amf to delete the options!
} else if (self->GetVar<int32_t>(u"currentZone") != m_ChoiceZoneID) {
AMFArrayValue multiArgs;
multiArgs.Insert("state", "Lobby");
multiArgs.Insert<std::string>("state", "Lobby");
AMFArrayValue* context = multiArgs.InsertArray("context");
context->Insert("user", std::to_string(player->GetObjectID()));
context->Insert("callbackObj", std::to_string(self->GetObjectID()));
context->Insert("HelpVisible", "show");
context->Insert("type", "Lego_Club_Valid");
context->Insert<std::string>("HelpVisible", "show");
context->Insert<std::string>("type", "Lego_Club_Valid");
GameMessages::SendUIMessageServerToSingleClient(player, player->GetSystemAddress(), "pushGameState", multiArgs);
} else {

View File

@ -13,27 +13,27 @@ void NsLupTeleport::OnStartup(Entity* self) {
args = {};
args.Insert("callbackClient", std::to_string(self->GetObjectID()));
args.Insert("strIdentifier", "choiceDoor");
args.Insert("title", "%[UI_CHOICE_DESTINATION]");
args.Insert<std::string>("strIdentifier", "choiceDoor");
args.Insert<std::string>("title", "%[UI_CHOICE_DESTINATION]");
AMFArrayValue* choiceOptions = args.InsertArray("options");
{
AMFArrayValue* nsArgs = choiceOptions->PushArray();
nsArgs->Insert("image", "textures/ui/zone_thumnails/Nimbus_Station.dds");
nsArgs->Insert("caption", "%[UI_CHOICE_NS]");
nsArgs->Insert("identifier", "zoneID_1200");
nsArgs->Insert("tooltipText", "%[UI_CHOICE_NS_HOVER]");
nsArgs->Insert<std::string>("image", "textures/ui/zone_thumnails/Nimbus_Station.dds");
nsArgs->Insert<std::string>("caption", "%[UI_CHOICE_NS]");
nsArgs->Insert<std::string>("identifier", "zoneID_1200");
nsArgs->Insert<std::string>("tooltipText", "%[UI_CHOICE_NS_HOVER]");
}
{
AMFArrayValue* ntArgs = choiceOptions->PushArray();
ntArgs->Insert("image", "textures/ui/zone_thumnails/Nexus_Tower.dds");
ntArgs->Insert("caption", "%[UI_CHOICE_NT]");
ntArgs->Insert("identifier", "zoneID_1900");
ntArgs->Insert("tooltipText", "%[UI_CHOICE_NT_HOVER]");
ntArgs->Insert<std::string>("image", "textures/ui/zone_thumnails/Nexus_Tower.dds");
ntArgs->Insert<std::string>("caption", "%[UI_CHOICE_NT]");
ntArgs->Insert<std::string>("identifier", "zoneID_1900");
ntArgs->Insert<std::string>("tooltipText", "%[UI_CHOICE_NT_HOVER]");
}
}

View File

@ -22,7 +22,7 @@ void PropertyBankInteract::OnUse(Entity* self, Entity* user) {
AMFArrayValue args;
args.Insert("state", "bank");
args.Insert<std::string>("state", "bank");
GameMessages::SendUIMessageServerToSingleClient(user, user->GetSystemAddress(), "pushGameState", args);

View File

@ -1396,7 +1396,7 @@ void HandlePacket(Packet* packet) {
}
default:
const auto messageId = *reinterpret_cast<MessageType::World*>(&packet->data[3]);
const auto messageId = static_cast<MessageType::World>(packet->data[3]);
const std::string_view messageIdString = StringifiedEnum::ToString(messageId);
LOG("Unknown world packet received: %4i, %s", messageId, messageIdString.data());
}

View File

@ -61,8 +61,8 @@ TEST(dCommonTests, AMF3AssociativeArrayTest) {
TEST(dCommonTests, AMF3InsertionAssociativeTest) {
AMFArrayValue array;
array.Insert("CString", "string");
array.Insert("String", std::string("string"));
array.Insert<std::string>("CString", "string");
array.Insert<std::string>("String", std::string("string"));
array.Insert("False", false);
array.Insert("True", true);
array.Insert<int32_t>("Integer", 42U);
@ -71,7 +71,6 @@ TEST(dCommonTests, AMF3InsertionAssociativeTest) {
array.Insert<std::vector<uint32_t>>("Undefined", {});
array.Insert("Null", nullptr);
ASSERT_EQ(array.Get<const char*>("CString")->GetValueType(), eAmf::String);
ASSERT_EQ(array.Get<std::string>("String")->GetValueType(), eAmf::String);
ASSERT_EQ(array.Get<bool>("False")->GetValueType(), eAmf::False);
ASSERT_EQ(array.Get<bool>("True")->GetValueType(), eAmf::True);
@ -85,7 +84,7 @@ TEST(dCommonTests, AMF3InsertionAssociativeTest) {
TEST(dCommonTests, AMF3InsertionDenseTest) {
AMFArrayValue array;
array.Push<std::string>("string");
array.Push("CString");
array.Push<std::string>("CString");
array.Push(false);
array.Push(true);
array.Push<int32_t>(42U);
@ -95,7 +94,6 @@ TEST(dCommonTests, AMF3InsertionDenseTest) {
array.Push<std::vector<uint32_t>>({});
ASSERT_EQ(array.Get<std::string>(0)->GetValueType(), eAmf::String);
ASSERT_EQ(array.Get<const char*>(1)->GetValueType(), eAmf::String);
ASSERT_EQ(array.Get<bool>(2)->GetValueType(), eAmf::False);
ASSERT_EQ(array.Get<bool>(3)->GetValueType(), eAmf::True);
ASSERT_EQ(array.Get<int32_t>(4)->GetValueType(), eAmf::Integer);