diff options
author | Joursoir <chat@joursoir.net> | 2022-08-31 18:20:00 +0300 |
---|---|---|
committer | Joursoir <chat@joursoir.net> | 2022-08-31 19:14:03 +0300 |
commit | e2ded11868b5dc07a3f8f1aab1e6976fb22c6bee (patch) | |
tree | 1263874d5debf94294b75ba365f1aa6068e239d6 | |
parent | 471a67cd6bb133267e9e26ee0c4a6a934299253f (diff) | |
download | lock-password-e2ded11868b5dc07a3f8f1aab1e6976fb22c6bee.tar.gz lock-password-e2ded11868b5dc07a3f8f1aab1e6976fb22c6bee.tar.bz2 lock-password-e2ded11868b5dc07a3f8f1aab1e6976fb22c6bee.zip |
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.
-rw-r--r-- | src/exec-cmd.c | 8 | ||||
-rw-r--r-- | src/r-gpgme.c | 4 | ||||
-rw-r--r-- | src/routines.c | 16 | ||||
-rw-r--r-- | src/tree.c | 1 |
4 files changed, 28 insertions, 1 deletions
diff --git a/src/exec-cmd.c b/src/exec-cmd.c index a54e67e..90a666e 100644 --- a/src/exec-cmd.c +++ b/src/exec-cmd.c @@ -238,6 +238,12 @@ int cmd_edit(int argc, char *argv[]) } password = malloc(sizeof(char) * (maxlen_pass + 1)); + if (!password) { + print_error("Unable to allocate space for the password.\n"); + unlink(path_tmpfile); + return 1; + } + len_pass = read(fd, password, maxlen_pass); save_errno = errno; close(fd); @@ -317,6 +323,8 @@ int cmd_generate(int argc, char *argv[]) // generate password char *g_pass = gen_password(pass_length); + if (!g_pass) + return 1; result = insert_pass(path, g_pass); if(result) { diff --git a/src/r-gpgme.c b/src/r-gpgme.c index cfa2f98..4641e60 100644 --- a/src/r-gpgme.c +++ b/src/r-gpgme.c @@ -175,6 +175,10 @@ char *decrypt_data(const char *path) } data = malloc(sizeof(char) * (maxlen_pass + 1)); + if (!data) { + print_error("Unable to allocate space for the decrypted data.\n"); + goto out_release_plain; + } gpgme_data_read(plain, data, maxlen_pass); out_release_plain: 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++) @@ -26,6 +26,7 @@ #include "tree.h" #include "xstd.h" #include "easydir.h" +#include "output.h" #define ANSIC_RST "\x1B[0m" #define ANSIC_BBLU "\x1B[34;1m" |