From d6ab9640056af4dbf9a27f18a9291426b20b7eb9 Mon Sep 17 00:00:00 2001 From: Nicholas Tay Date: Thu, 2 Dec 2021 18:44:22 +1100 Subject: Dynamically allocate password instead of using VLA This allows the code to compile with `cl` (MS Windows). Additionally allows massive passwords to be made more easily I suppose, as we aren't allocating (potentially dangerously) on the stack anymore. But, generation may be slightly slower due to allocation of memory. We could possibly have some statically allocated memory (e.g. 20 bytes) and if we go over then malloc(). --- passgen.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'passgen.c') diff --git a/passgen.c b/passgen.c index a21f3ac..bea56fc 100644 --- a/passgen.c +++ b/passgen.c @@ -9,6 +9,11 @@ #include #else #include +#endif + +#if defined (_WIN32) && ! defined (__MINGW32__) +#include +#else #include #endif @@ -43,6 +48,10 @@ int main(int argc, char *argv[]) grammar_size = triplets * 3 + specials + numbers; grammar = malloc(grammar_size + 1); + if (grammar == NULL) { + perror("malloc"); + return 1; + } grammar[grammar_size] = 0; memcpy(grammar, "Cvc", 3); @@ -55,7 +64,11 @@ int main(int argc, char *argv[]) custom_grammar = true; } - char password[grammar_size+1]; + char *password = malloc(grammar_size + 1); + if (password == NULL) { + perror("malloc"); + return 1; + } password[grammar_size] = 0; #ifndef __linux__ @@ -80,6 +93,7 @@ int main(int argc, char *argv[]) printf("ERROR: Invalid grammar character '%c'.\n", c); if (custom_grammar) free(grammar); + free(password); return 1; } @@ -98,6 +112,7 @@ int main(int argc, char *argv[]) free(grammar); printf("%s\n", password); + free(password); return 0; } -- cgit