aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Tay <nick@windblume.net>2021-12-02 18:44:22 +1100
committerNicholas Tay <nick@windblume.net>2021-12-02 18:44:22 +1100
commitd6ab9640056af4dbf9a27f18a9291426b20b7eb9 (patch)
treea55790bec807289672700d2a8c252441ab253556
parent53346266b603caa37a2d516d13697867e04767d6 (diff)
downloadpassgen-d6ab9640056af4dbf9a27f18a9291426b20b7eb9.tar.gz
passgen-d6ab9640056af4dbf9a27f18a9291426b20b7eb9.tar.bz2
passgen-d6ab9640056af4dbf9a27f18a9291426b20b7eb9.zip
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().
-rw-r--r--passgen.c17
1 files changed, 16 insertions, 1 deletions
diff --git a/passgen.c b/passgen.c
index a21f3ac..bea56fc 100644
--- a/passgen.c
+++ b/passgen.c
@@ -9,6 +9,11 @@
#include <sys/random.h>
#else
#include <time.h>
+#endif
+
+#if defined (_WIN32) && ! defined (__MINGW32__)
+#include <io.h>
+#else
#include <unistd.h>
#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;
}