From 4742f4fffd69cc453428269c8a9a73fe9bbf09c9 Mon Sep 17 00:00:00 2001 From: Chris Miles Date: Wed, 14 May 2025 20:44:47 -0500 Subject: [PATCH] Argument safety --- zone/gm_commands/zonevariable.cpp | 150 +++++++++--------------------- 1 file changed, 43 insertions(+), 107 deletions(-) diff --git a/zone/gm_commands/zonevariable.cpp b/zone/gm_commands/zonevariable.cpp index 1cfd57338..54c4ae65f 100644 --- a/zone/gm_commands/zonevariable.cpp +++ b/zone/gm_commands/zonevariable.cpp @@ -15,17 +15,13 @@ void command_zonevariable(Client *c, const Seperator *sep) return; } - const bool is_clear = !strcasecmp(sep->arg[1], "clear"); - const bool is_delete = !strcasecmp(sep->arg[1], "delete"); - const bool is_set = !strcasecmp(sep->arg[1], "set"); - const bool is_view = !strcasecmp(sep->arg[1], "view"); + const char* action = arguments >= 1 ? sep->arg[1] : ""; + const bool is_clear = !strcasecmp(action, "clear"); + const bool is_delete = !strcasecmp(action, "delete"); + const bool is_set = !strcasecmp(action, "set"); + const bool is_view = !strcasecmp(action, "view"); - if ( - !is_clear && - !is_delete && - !is_set && - !is_view - ) { + if (!is_clear && !is_delete && !is_set && !is_view) { c->Message(Chat::White, "Usage: #zonevariable clear - Clear all zone variables"); c->Message(Chat::White, "Usage: #zonevariable delete [Variable Name] - Delete a zone variable"); c->Message(Chat::White, "Usage: #zonevariable set [Variable Name] [Variable Value] - Set a zone variable"); @@ -38,82 +34,46 @@ void command_zonevariable(Client *c, const Seperator *sep) if (is_clear) { const bool cleared = zone->ClearVariables(); + c->Message(Chat::White, cleared ? "Cleared all zone variables." : "There are no zone variables to clear."); + return; + } - if (!cleared) { - c->Message(Chat::White, "There are no zone variables to clear."); - return; - } - - c->Message(Chat::White, "Cleared all zone variables."); - } else if (is_delete) { - const std::string variable_name = sep->argplus[2]; - - if (!zone->VariableExists(variable_name)) { - c->Message( - Chat::White, - fmt::format( - "A zone variable named '{}' does not exist.", - variable_name - ).c_str() - ); - + if (is_delete) { + const std::string variable_name = arguments >= 2 ? sep->argplus[2] : ""; + if (variable_name.empty() || !zone->VariableExists(variable_name)) { + c->Message(Chat::White, fmt::format("A zone variable named '{}' does not exist.", variable_name).c_str()); return; } zone->DeleteVariable(variable_name); - - c->Message( - Chat::White, - fmt::format( - "Deleted a zone variable named '{}'.", - variable_name - ).c_str() - ); - + c->Message(Chat::White, fmt::format("Deleted a zone variable named '{}'.", variable_name).c_str()); return; - } else if (is_set) { - const std::string variable_name = sep->arg[2]; - const std::string variable_value = sep->arg[3]; + } + if (is_set) { + const std::string variable_name = arguments >= 2 ? sep->arg[2] : ""; + const std::string variable_value = arguments >= 3 ? sep->arg[3] : ""; zone->SetVariable(variable_name, variable_value); + c->Message(Chat::White, fmt::format("Set a zone variable named '{}' to a value of '{}'.", variable_name, variable_value).c_str()); + return; + } - c->Message( - Chat::White, - fmt::format( - "Set a zone variable named '{}' to a value of '{}'.", - variable_name, - variable_value - ).c_str() - ); - } else if (is_view) { + if (is_view) { const auto& l = zone->GetVariables(); + const std::string search_criteria = arguments >= 2 ? sep->argplus[2] : ""; uint32 variable_count = 0; uint32 variable_number = 1; - const std::string search_criteria = arguments >= 2 ? sep->argplus[2] : ""; - for (const auto& e : l) { - if ( - search_criteria.empty() || - Strings::Contains(Strings::ToLower(e), Strings::ToLower(search_criteria)) - ) { - c->Message( - Chat::White, - fmt::format( - "Variable {} | Name: {} Value: {} | {}", - variable_number, - e, - zone->GetVariable(e), - Saylink::Silent( - fmt::format( - "#zonevariable delete {}", - e - ), - "Delete" - ) - ).c_str() - ); + if (search_criteria.empty() || Strings::Contains(Strings::ToLower(e), Strings::ToLower(search_criteria))) { + c->Message(Chat::White, fmt::format( + "Variable {} | Name: {} Value: {} | {}", + variable_number, + e, + zone->GetVariable(e), + Saylink::Silent(fmt::format("#zonevariable delete {}", e), "Delete") + ).c_str()); variable_count++; variable_number++; @@ -121,44 +81,20 @@ void command_zonevariable(Client *c, const Seperator *sep) } if (!variable_count) { - c->Message( - Chat::White, - fmt::format( - "There are no zone variables{}.", - ( - !search_criteria.empty() ? - fmt::format( - " matching '{}'", - search_criteria - ) : - "" - ) - ).c_str() - ); - + c->Message(Chat::White, fmt::format( + "There are no zone variables{}.", + (!search_criteria.empty() ? fmt::format(" matching '{}'", search_criteria) : "") + ).c_str()); return; } - c->Message( - Chat::White, - fmt::format( - "There {} {} zone variable{}{}, would you like to {} zone variables?", - variable_count != 1 ? "are" : "is", - variable_count, - variable_count != 1 ? "s" : "", - ( - !search_criteria.empty() ? - fmt::format( - " matching '{}'", - search_criteria - ) : - "" - ), - Saylink::Silent( - "#zonevariable clear", - "clear" - ) - ).c_str() - ); + c->Message(Chat::White, fmt::format( + "There {} {} zone variable{}{}, would you like to {} zone variables?", + variable_count != 1 ? "are" : "is", + variable_count, + variable_count != 1 ? "s" : "", + (!search_criteria.empty() ? fmt::format(" matching '{}'", search_criteria) : ""), + Saylink::Silent("#zonevariable clear", "clear") + ).c_str()); } }