From e2ded11868b5dc07a3f8f1aab1e6976fb22c6bee Mon Sep 17 00:00:00 2001 From: Joursoir Date: Wed, 31 Aug 2022 18:20:00 +0300 Subject: tree: handle a pointer returned by malloc() It is wrong to assume that malloc() always returns allocated memory. Unfortunately, it can return NULL. The rest of the code that uses malloc() should be refactored. That's the reason why we can't do it right now. --- src/routines.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'src/routines.c') diff --git a/src/routines.c b/src/routines.c index 2250fec..da83c38 100644 --- a/src/routines.c +++ b/src/routines.c @@ -99,6 +99,12 @@ char *get_pubkey() } pubkey = malloc(sizeof(char) * (maxlen_fingerprint + 1)); + if (!pubkey) { + print_error("Unable to allocate space for the public key.\n"); + fclose(fileGPG); + return NULL; + } + if(fgets(pubkey, maxlen_fingerprint + 1, fileGPG) == NULL) { print_error("Error: %s\n", strerror(errno)); free(pubkey); @@ -159,8 +165,12 @@ int insert_pass(const char *path, const char *password) char *get_input(int minlen, int maxlen) { - char *pass = malloc(sizeof(char) * (maxlen + 1)); int len; + char *pass = malloc(sizeof(char) * (maxlen + 1)); + if (!pass) { + print_error("Unable to allocate space for the password.\n"); + return NULL; + } if(fgets(pass, maxlen + 1, stdin) == NULL) { print_error("Error: %s\n", strerror(errno)); @@ -189,6 +199,10 @@ char *gen_password(int length) "0123456789" "!@#$^&*?"; char *password = malloc(sizeof(char) * (length + 1)); + if (!password) { + print_error("Unable to allocate space for the password.\n"); + return NULL; + } srand(time(NULL)); for(i = 0; i < length; i++) -- cgit v1.2.3-18-g5258