From d9f9253504605194b6bac1dd18491f132059b145 Mon Sep 17 00:00:00 2001 From: Henrique de Carvalho Date: Sat, 2 Mar 2024 02:55:16 -0300 Subject: [PATCH] Fix SegFault in pgagroal-cli pgagroal-cli was raising segmentation fault when no command is specified, but a password is specified. --- AUTHORS | 3 ++- src/cli.c | 22 ++++++---------------- 2 files changed, 8 insertions(+), 17 deletions(-) diff --git a/AUTHORS b/AUTHORS index 3707d5a4..a81701ca 100644 --- a/AUTHORS +++ b/AUTHORS @@ -8,4 +8,5 @@ Luca Ferrari Nikita Bugrovsky Lawrence Wu Yongting You <2010youy01@gmail.com> -Ashutosh Sharma \ No newline at end of file +Ashutosh Sharma +Henrique de Carvalho \ No newline at end of file diff --git a/src/cli.c b/src/cli.c index c9bdc6c6..8d7a6ca1 100644 --- a/src/cli.c +++ b/src/cli.c @@ -163,7 +163,6 @@ main(int argc, char** argv) char* password = NULL; bool verbose = false; char* logfile = NULL; - bool do_free = true; int c; int option_index = 0; size_t size; @@ -218,7 +217,11 @@ main(int argc, char** argv) username = optarg; break; case 'P': - password = optarg; + password = strdup(optarg); + if (password == NULL) + { + errx(1, "Error allocating memory for password"); + } break; case 'L': logfile = optarg; @@ -533,20 +536,10 @@ main(int argc, char** argv) /* Password */ if (password == NULL) { - if (password != NULL) - { - free(password); - password = NULL; - } - printf("Password : "); password = pgagroal_get_password(); printf("\n"); } - else - { - do_free = false; - } for (int i = 0; i < strlen(password); i++) { @@ -676,10 +669,7 @@ main(int argc, char** argv) pgagroal_stop_logging(); pgagroal_destroy_shared_memory(shmem, size); - if (do_free) - { - free(password); - } + free(password); if (verbose) {