Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1kfHRh-00019S-K8 for mharc-grub-devel@gnu.org; Wed, 18 Nov 2020 02:00:01 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:60280) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kfHRc-00018P-HQ for grub-devel@gnu.org; Wed, 18 Nov 2020 01:59:57 -0500 Received: from out30-45.freemail.mail.aliyun.com ([115.124.30.45]:32963) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kfHRY-0000kd-4U for grub-devel@gnu.org; Wed, 18 Nov 2020 01:59:54 -0500 X-Alimail-AntiSpam: AC=PASS; BC=-1|-1; BR=01201311R141e4; CH=green; DM=||false|; DS=||; FP=0|-1|-1|-1|0|-1|-1|-1; HT=e01e04426; MF=tianjia.zhang@linux.alibaba.com; NM=1; PH=DS; RN=3; SR=0; TI=SMTPD_---0UFmVRlp_1605682722; Received: from localhost(mailfrom:tianjia.zhang@linux.alibaba.com fp:SMTPD_---0UFmVRlp_1605682722) by smtp.aliyun-inc.com(127.0.0.1); Wed, 18 Nov 2020 14:58:43 +0800 From: Tianjia Zhang To: Daniel Kiper , grub-devel@gnu.org Cc: Tianjia Zhang Subject: [PATCH] grub-mkpasswd-pbkdf2: Simplify the main function implementation Date: Wed, 18 Nov 2020 14:58:42 +0800 Message-Id: <20201118065842.32049-1-tianjia.zhang@linux.alibaba.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=115.124.30.45; envelope-from=tianjia.zhang@linux.alibaba.com; helo=out30-45.freemail.mail.aliyun.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/11/18 01:58:45 X-ACL-Warn: Detected OS = Linux 3.x [generic] [fuzzy] X-Spam_score_int: -98 X-Spam_score: -9.9 X-Spam_bar: --------- X-Spam_report: (-9.9 / 5.0 requ) BAYES_00=-1.9, ENV_AND_HDR_SPF_MATCH=-0.5, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, UNPARSEABLE_RELAY=0.001, USER_IN_DEF_SPF_WL=-7.5 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 18 Nov 2020 06:59:57 -0000 Allocate memory if needed, while saving the corresponding release operation, reducing the amount of code and code complexity. Signed-off-by: Tianjia Zhang --- util/grub-mkpasswd-pbkdf2.c | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/util/grub-mkpasswd-pbkdf2.c b/util/grub-mkpasswd-pbkdf2.c index 5805f3c10..cd7943df6 100644 --- a/util/grub-mkpasswd-pbkdf2.c +++ b/util/grub-mkpasswd-pbkdf2.c @@ -132,35 +132,25 @@ main (int argc, char *argv[]) fprintf (stderr, "%s", _("Error in parsing command line arguments\n")); exit(1); } - - buf = xmalloc (arguments.buflen); - salt = xmalloc (arguments.saltlen); printf ("%s", _("Enter password: ")); if (!grub_password_get (pass1, GRUB_AUTH_MAX_PASSLEN)) - { - free (buf); - free (salt); - grub_util_error ("%s", _("failure to read password")); - } + grub_util_error ("%s", _("failure to read password")); printf ("%s", _("Reenter password: ")); if (!grub_password_get (pass2, GRUB_AUTH_MAX_PASSLEN)) - { - free (buf); - free (salt); - grub_util_error ("%s", _("failure to read password")); - } + grub_util_error ("%s", _("failure to read password")); if (strcmp (pass1, pass2) != 0) { memset (pass1, 0, sizeof (pass1)); memset (pass2, 0, sizeof (pass2)); - free (buf); - free (salt); grub_util_error ("%s", _("passwords don't match")); } memset (pass2, 0, sizeof (pass2)); + buf = xmalloc (arguments.buflen); + salt = xmalloc (arguments.saltlen); + if (grub_get_random (salt, arguments.saltlen)) { memset (pass1, 0, sizeof (pass1)); -- 2.24.3 (Apple Git-128)