diff --git a/Quake/cmd.c b/Quake/cmd.c index 625a8efe..a50272c0 100644 --- a/Quake/cmd.c +++ b/Quake/cmd.c @@ -1474,6 +1474,9 @@ const char *Cmd_CompleteCommand (const char *partial) qboolean Cmd_IsQuitMistype (const char* input) // woods -- #smartquit { + if (!input) + return false; + if (Cvar_FindVar(input) || Cmd_Exists2(input) || Cmd_AliasExists(input)) return false; @@ -1485,6 +1488,9 @@ qboolean Cmd_IsQuitMistype (const char* input) // woods -- #smartquit int threshold = 2; // Define a threshold for mistypes (e.g., distance <= 2) int distance = LevenshteinDistance(input, correct_cmd); // Calculate the Levenshtein distance + if (distance == -1) + return false; + return distance > 0 && distance <= threshold; // Return true if within the threshold, else false } diff --git a/Quake/common.c b/Quake/common.c index 0fabeb27..b8231456 100644 --- a/Quake/common.c +++ b/Quake/common.c @@ -4710,14 +4710,32 @@ void SetChatInfo (int flags) // woods #chatinfo int LevenshteinDistance (const char* s, const char* t) // woods -- #smartquit -- function to calculate the Levenshtein Distance { + // Check for null pointers + if (!s || !t) return -1; + int len_s = strlen(s); int len_t = strlen(t); + // Handle empty strings + if (len_s == 0) return len_t; + if (len_t == 0) return len_s; + // Allocate a matrix dynamically - int** matrix = malloc((len_s + 1) * sizeof(int*)); - for (int i = 0; i <= len_s; i++) - { - matrix[i] = malloc((len_t + 1) * sizeof(int)); + int** matrix = (int**)calloc(len_s + 1, sizeof(int*)); + if (!matrix) return -1; // Handle allocation failure + + int distance = -1; + + for (int i = 0; i <= len_s; i++) { + matrix[i] = (int*)calloc(len_t + 1, sizeof(int)); + if (!matrix[i]) { + // Clean up previously allocated memory if allocation fails + for (int j = 0; j < i; j++) { + free(matrix[j]); + } + free(matrix); + return -1; + } } // Initialize the matrix @@ -4740,7 +4758,7 @@ int LevenshteinDistance (const char* s, const char* t) // woods -- #smartquit -- } } - int distance = matrix[len_s][len_t]; + distance = matrix[len_s][len_t]; // Free the matrix for (int i = 0; i <= len_s; i++) {