diff --git a/src/client/component/binding.cpp b/src/client/component/binding.cpp index f88a0a6..645b010 100644 --- a/src/client/component/binding.cpp +++ b/src/client/component/binding.cpp @@ -30,8 +30,7 @@ namespace binding if (value && value < get_num_keys()) { - const auto len = sprintf_s(&buffer[bytes_used], (buffer_size_align - bytes_used), - "bind %s \"%s\"\n", key_button, game::command_whitelist[value]); + const auto len = game::Com_sprintf(&buffer[bytes_used], (buffer_size_align - bytes_used), "bind %s \"%s\"\n", key_button, game::command_whitelist[value]); if (len < 0) { @@ -45,8 +44,7 @@ namespace binding value -= get_num_keys(); if (static_cast(value) < custom_binds.size() && !custom_binds[value].empty()) { - const auto len = sprintf_s(&buffer[bytes_used], (buffer_size_align - bytes_used), - "bind %s \"%s\"\n", key_button, custom_binds[value].data()); + const auto len = game::Com_sprintf(&buffer[bytes_used], (buffer_size_align - bytes_used), "bind %s \"%s\"\n", key_button, custom_binds[value].data()); if (len < 0) { diff --git a/src/client/component/console.cpp b/src/client/component/console.cpp index a66101d..b11d3d7 100644 --- a/src/client/component/console.cpp +++ b/src/client/component/console.cpp @@ -42,7 +42,7 @@ namespace console { static thread_local char buffer[0x1000]; - const auto count = vsnprintf_s(buffer, _TRUNCATE, message, *ap); + const auto count = vsnprintf(buffer, sizeof(buffer), message, *ap); if (count < 0) return {}; return {buffer, static_cast(count)}; @@ -84,15 +84,14 @@ namespace console void print_stub(const char* fmt, ...) { + char buffer[4096]{}; + va_list ap; va_start(ap, fmt); - - char buffer[4096]{}; - const auto res = vsnprintf_s(buffer, _TRUNCATE, fmt, ap); - (void)res; - print_message(buffer); - + [[maybe_unused]] const auto res = vsnprintf(buffer, sizeof(buffer), fmt, ap); va_end(ap); + + print_message(buffer); } void append_text(const char* text) diff --git a/src/client/component/demonware.cpp b/src/client/component/demonware.cpp index a07cbc6..cbbc7c2 100644 --- a/src/client/component/demonware.cpp +++ b/src/client/component/demonware.cpp @@ -348,15 +348,14 @@ namespace demonware return; } - char buffer[2048]; + char buffer[2048]{}; va_list ap; va_start(ap, msg); - - vsnprintf_s(buffer, _TRUNCATE, msg, ap); - printf("%s: %s\n", function, buffer); - + vsnprintf(buffer, sizeof(buffer), msg, ap); va_end(ap); + + printf("%s: %s\n", function, buffer); } void startup_dw() diff --git a/src/client/component/game_console.cpp b/src/client/component/game_console.cpp index 90e0167..9384ac4 100644 --- a/src/client/component/game_console.cpp +++ b/src/client/component/game_console.cpp @@ -63,7 +63,7 @@ namespace game_console void clear() { - strncpy_s(con.buffer, "", sizeof(con.buffer)); + game::I_strncpyz(con.buffer, "", sizeof(con.buffer)); con.cursor = 0; fixed_input = ""; @@ -249,7 +249,7 @@ namespace game_console dvars::con_inputDvarInactiveValueColor->current.vector, offset); } - strncpy_s(con.globals.auto_complete_choice, matches[0].data(), 64); + game::I_strncpyz(con.globals.auto_complete_choice, matches[0].data(), 64); con.globals.may_auto_complete = true; } else if (matches.size() > 1) @@ -274,7 +274,7 @@ namespace game_console } } - strncpy_s(con.globals.auto_complete_choice, matches[0].data(), 64); + game::I_strncpyz(con.globals.auto_complete_choice, matches[0].data(), 64); con.globals.may_auto_complete = true; } } @@ -365,11 +365,11 @@ namespace game_console void print_internal(const char* fmt, ...) { - char va_buffer[0x200]{}; + char va_buffer[1024]{}; va_list ap; va_start(ap, fmt); - vsprintf_s(va_buffer, fmt, ap); + vsnprintf(va_buffer, sizeof(va_buffer), fmt, ap); va_end(ap); const auto formatted = std::string(va_buffer); @@ -425,7 +425,7 @@ namespace game_console con.buffer[1] = '\0'; } - strncat_s(con.buffer, con.globals.auto_complete_choice, 64); + game::I_strncat(con.buffer, sizeof(con.buffer), con.globals.auto_complete_choice); con.cursor = static_cast(std::string(con.buffer).length()); if (con.cursor != 254) @@ -550,7 +550,7 @@ namespace game_console if (history_index != -1) { - strncpy_s(con.buffer, history.at(history_index).c_str(), sizeof(con.buffer)); + game::I_strncpyz(con.buffer, history.at(history_index).c_str(), sizeof(con.buffer)); con.cursor = static_cast(strlen(con.buffer)); } } @@ -565,7 +565,7 @@ namespace game_console if (history_index != -1) { - strncpy_s(con.buffer, history.at(history_index).c_str(), sizeof(con.buffer)); + game::I_strncpyz(con.buffer, history.at(history_index).c_str(), sizeof(con.buffer)); con.cursor = static_cast(strlen(con.buffer)); } } @@ -720,7 +720,7 @@ namespace game_console con.output_visible = false; con.display_line_offset = 0; con.line_count = 0; - strncpy_s(con.buffer, "", sizeof(con.buffer)); + game::I_strncpyz(con.buffer, "", sizeof(con.buffer)); con.globals.x = 0.0f; con.globals.y = 0.0f; @@ -728,7 +728,7 @@ namespace game_console con.globals.font_height = 0.0f; con.globals.may_auto_complete = false; con.globals.info_line_count = 0; - strncpy_s(con.globals.auto_complete_choice, "", 64); + game::I_strncpyz(con.globals.auto_complete_choice, "", 64); // add clear command command::add("clear", [&]() diff --git a/src/client/component/game_log.cpp b/src/client/component/game_log.cpp index 23775aa..cdde815 100644 --- a/src/client/component/game_log.cpp +++ b/src/client/component/game_log.cpp @@ -10,7 +10,6 @@ #include "gsc/script_extension.hpp" -#include #include #include @@ -23,7 +22,7 @@ namespace game_log char buf[1024]{}; std::size_t out_chars = 0; - for (auto i = 0u; i < game::Scr_GetNumParam(); ++i) + for (std::uint32_t i = 0; i < game::Scr_GetNumParam(); ++i) { const auto* value = game::Scr_GetString(i); const auto len = std::strlen(value); @@ -34,7 +33,7 @@ namespace game_log break; } - strncat_s(buf, value, _TRUNCATE); + game::I_strncat(buf, sizeof(buf), value); } g_log_printf("%s", buf); @@ -53,18 +52,11 @@ namespace game_log va_list ap; va_start(ap, fmt); - - vsnprintf_s(buffer, _TRUNCATE, fmt, ap); - + vsnprintf(buffer, sizeof(buffer), fmt, ap); va_end(ap); const auto time = *game::level_time / 1000; - utils::io::write_file(log, utils::string::va("%3i:%i%i %s", - time / 60, - time % 60 / 10, - time % 60 % 10, - buffer - ), true); + utils::io::write_file(log, utils::string::va("%3i:%i%i %s", time / 60, time % 60 / 10, time % 60 % 10, buffer), true); } class component final : public component_interface diff --git a/src/client/component/logger.cpp b/src/client/component/logger.cpp index 60eb61d..abb5494 100644 --- a/src/client/component/logger.cpp +++ b/src/client/component/logger.cpp @@ -2,7 +2,6 @@ #include "loader/component_loader.hpp" #include "game/game.hpp" -#include "party.hpp" #include "console.hpp" #include @@ -11,57 +10,15 @@ namespace logger { namespace { - utils::hook::detour com_error_hook; - game::dvar_t* logger_dev = nullptr; - void print_error(const char* msg, ...) - { - char buffer[2048]{}; - va_list ap; - - va_start(ap, msg); - vsnprintf_s(buffer, _TRUNCATE, msg, ap); - va_end(ap); - - console::error("%s", buffer); - } - - void print_com_error(int, const char* msg, ...) - { - char buffer[2048]{}; - va_list ap; - - va_start(ap, msg); - vsnprintf_s(buffer, _TRUNCATE, msg, ap); - va_end(ap); - - console::error("%s", buffer); - } - - void com_error_stub(const int error, const char* msg, ...) - { - char buffer[2048]{}; - va_list ap; - - va_start(ap, msg); - vsnprintf_s(buffer, _TRUNCATE, msg, ap); - va_end(ap); - - console::error("Error: %s\n", buffer); - - party::clear_sv_motd(); // clear sv_motd on error if it exists - - com_error_hook.invoke(error, "%s", buffer); - } - void print_warning(const char* msg, ...) { char buffer[2048]{}; va_list ap; va_start(ap, msg); - vsnprintf_s(buffer, _TRUNCATE, msg, ap); + vsnprintf(buffer, sizeof(buffer), msg, ap); va_end(ap); console::warn("%s", buffer); @@ -73,7 +30,7 @@ namespace logger va_list ap; va_start(ap, msg); - vsnprintf_s(buffer, _TRUNCATE, msg, ap); + vsnprintf(buffer, sizeof(buffer), msg, ap); va_end(ap); console::info("%s", buffer); @@ -90,7 +47,7 @@ namespace logger va_list ap; va_start(ap, msg); - vsnprintf_s(buffer, _TRUNCATE, msg, ap); + vsnprintf(buffer, sizeof(buffer), msg, ap); va_end(ap); console::info("%s", buffer); @@ -157,13 +114,6 @@ namespace logger utils::hook::jump(0x140701A1C, print); } - if (!game::environment::is_sp()) - { - utils::hook::call(0x1404D8543, print_com_error); - } - - com_error_hook.create(game::Com_Error, com_error_stub); - logger_dev = game::Dvar_RegisterBool("logger_dev", false, game::DVAR_FLAG_SAVED); } }; diff --git a/src/client/component/party.cpp b/src/client/component/party.cpp index 8f94790..81e9201 100644 --- a/src/client/component/party.cpp +++ b/src/client/component/party.cpp @@ -160,7 +160,7 @@ namespace party if (game::mp::g_entities[i].client) { char client_name[16] = {0}; - strncpy_s(client_name, game::mp::g_entities[i].client->name, sizeof(client_name)); + game::I_strncpyz(client_name, game::mp::g_entities[i].client->name, sizeof(client_name)); game::I_CleanStr(client_name); if (client_name == name) diff --git a/src/client/component/patches.cpp b/src/client/component/patches.cpp index b71cebc..28b5556 100644 --- a/src/client/component/patches.cpp +++ b/src/client/component/patches.cpp @@ -118,7 +118,7 @@ namespace patches char* db_read_raw_file_stub(const char* filename, char* buf, const int size) { std::string file_name = filename; - if (file_name.find(".cfg") == std::string::npos) + if (!file_name.ends_with(".cfg")) { file_name.append(".cfg"); } @@ -127,6 +127,7 @@ namespace patches if (file.exists()) { snprintf(buf, size, "%s\n", file.get_buffer().data()); + buf[size - 1] = '\0'; return buf; } @@ -327,6 +328,9 @@ namespace patches game::Dvar_RegisterInt("scr_game_spectatetype", 1, 0, 99, game::DVAR_FLAG_REPLICATED); + // Disable Com_Error in NET_SendPacket + utils::hook::nop(0x1404D8543, 5); + // Prevent clients from ending the game as non host by sending 'end_game' lui notification cmd_lui_notify_server_hook.create(0x1402E9390, cmd_lui_notify_server_stub); diff --git a/src/client/component/rcon.cpp b/src/client/component/rcon.cpp index 123dc80..727e898 100644 --- a/src/client/component/rcon.cpp +++ b/src/client/component/rcon.cpp @@ -86,7 +86,7 @@ namespace rcon } char clean_name[32]{}; - strncpy_s(clean_name, client->name, _TRUNCATE); + game::I_strncpyz(clean_name, client->name, sizeof(clean_name)); game::I_CleanStr(clean_name); buffer.append(utils::string::va("%3i %5i %3s %s %32s %16s %21s %5i\n", diff --git a/src/client/component/security.cpp b/src/client/component/security.cpp index fdc4f08..2152374 100644 --- a/src/client/component/security.cpp +++ b/src/client/component/security.cpp @@ -84,6 +84,36 @@ namespace security ui_replace_directive_hook.invoke(local_client_num, src_string, dst_string, dst_buffer_size); } + + int hud_elem_set_enum_string_stub(char* string, const char* format, ...) + { + va_list ap; + va_start(ap, format); + const auto len = vsnprintf(string, 0x800, format, ap); + va_end(ap); + + return len; + } + + int sv_add_bot_stub(char* string, const char* format, ...) + { + va_list ap; + va_start(ap, format); + const auto len = vsnprintf(string, 0x400, format, ap); + va_end(ap); + + return len; + } + + int sv_add_test_client_stub(char* string, const char* format, ...) + { + va_list ap; + va_start(ap, format); + const auto len = vsnprintf(string, 0x400, format, ap); + va_end(ap); + + return len; + } } class component final : public component_interface @@ -91,11 +121,18 @@ namespace security public: void post_unpack() override { + // sprinf + utils::hook::call(SELECT_VALUE(0x1402203DF, 0x1402F0BAF), hud_elem_set_enum_string_stub); + if (game::environment::is_sp()) return; // Patch vulnerability in PlayerCards_SetCachedPlayerData utils::hook::call(0x1401BB909, set_cached_playerdata_stub); + // sprinf + utils::hook::call(0x140439075, sv_add_bot_stub); + utils::hook::call(0x14043932A, sv_add_test_client_stub); + // Patch entity overflow utils::hook::jump(0x14044DE3A, assemble(remap_cached_entities_stub), true); diff --git a/src/client/game/engine/sv_game.cpp b/src/client/game/engine/sv_game.cpp index b5dd968..10ce196 100644 --- a/src/client/game/engine/sv_game.cpp +++ b/src/client/game/engine/sv_game.cpp @@ -136,7 +136,7 @@ namespace game::engine const auto server_command_buf_large = std::make_unique(0x20000); va_start(va, fmt); - len = vsnprintf_s(server_command_buf_large.get(), 0x20000, _TRUNCATE, fmt, va); + len = vsnprintf(server_command_buf_large.get(), 0x20000, fmt, va); va_end(va); assert(len >= 0); diff --git a/src/client/game/symbols.hpp b/src/client/game/symbols.hpp index 3ff18aa..256f103 100644 --- a/src/client/game/symbols.hpp +++ b/src/client/game/symbols.hpp @@ -30,6 +30,7 @@ namespace game WEAK symbol Cmd_RemoveCommand{0x1402EE910, 0x1403AFEF0}; WEAK symbol Cmd_TokenizeString{0x1402EEA30, 0x1403B0020}; WEAK symbol Cmd_EndTokenizeString{0x1402EE000, 0x1403AF5B0}; + WEAK symbol Com_sprintf{0x140378E30, 0x1404C97B0}; WEAK symbol Conbuf_AppendText{0x14038F220, 0x1404D9040}; @@ -221,6 +222,7 @@ namespace game WEAK symbol LargeLocalResetToMark{0x140369C40, 0x1404B6790}; WEAK symbol I_strncpyz{0x1403793B0, 0x1404C9E60}; + WEAK symbol I_strncat{0x1403792E0, 0x1404C9D90}; WEAK symbol MSG_WriteReliableCommandToBuffer{0x0, 0x1403E1090}; diff --git a/src/client/main.cpp b/src/client/main.cpp index c0ad37d..d7d2686 100644 --- a/src/client/main.cpp +++ b/src/client/main.cpp @@ -64,9 +64,7 @@ LONG WINAPI exception_handler(PEXCEPTION_POINTERS exception_info) &exception_information, nullptr, nullptr)) { char buf[4096]{}; - sprintf_s(buf, "An exception 0x%08X occurred at location 0x%p\n", - exception_info->ExceptionRecord->ExceptionCode, - exception_info->ExceptionRecord->ExceptionAddress); + sprintf_s(buf, "An exception 0x%08X occurred at location 0x%p\n", exception_info->ExceptionRecord->ExceptionCode, exception_info->ExceptionRecord->ExceptionAddress); game::show_error(buf); }