Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1llp3E-0005cc-GP for mharc-grub-devel@gnu.org; Wed, 26 May 2021 04:38:04 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:48392) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1llp3C-0005TU-OD for grub-devel@gnu.org; Wed, 26 May 2021 04:38:02 -0400 Received: from out4436.biz.mail.alibaba.com ([47.88.44.36]:38834) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1llp36-0007h4-9D for grub-devel@gnu.org; Wed, 26 May 2021 04:37:58 -0400 X-Alimail-AntiSpam: AC=PASS; BC=-1|-1; BR=01201311R161e4; CH=green; DM=||false|; DS=||; FP=0|-1|-1|-1|0|-1|-1|-1; HT=e01e04400; MF=tianjia.zhang@linux.alibaba.com; NM=1; PH=DS; RN=3; SR=0; TI=SMTPD_---0Ua9lYjg_1622018238; Received: from B-455UMD6M-2027.local(mailfrom:tianjia.zhang@linux.alibaba.com fp:SMTPD_---0Ua9lYjg_1622018238) by smtp.aliyun-inc.com(127.0.0.1); Wed, 26 May 2021 16:37:18 +0800 Subject: Re: [PATCH] grub-mkpasswd-pbkdf2: Simplify the main function implementation To: The development of GNU GRUB , Glenn Washburn , Daniel Kiper Cc: Daniel Kiper References: <20201118065842.32049-1-tianjia.zhang@linux.alibaba.com> <48c2f315-c124-123f-4350-16e6e068e545@linux.alibaba.com> <20210314202648.167be00f@crass-HP-ZBook-15-G2> From: Tianjia Zhang Message-ID: Date: Wed, 26 May 2021 16:37:17 +0800 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.10.1 MIME-Version: 1.0 In-Reply-To: <20210314202648.167be00f@crass-HP-ZBook-15-G2> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Received-SPF: pass client-ip=47.88.44.36; envelope-from=tianjia.zhang@linux.alibaba.com; helo=out4436.biz.mail.alibaba.com X-Spam_score_int: -105 X-Spam_score: -10.6 X-Spam_bar: ---------- X-Spam_report: (-10.6 / 5.0 requ) BAYES_00=-1.9, ENV_AND_HDR_SPF_MATCH=-0.5, NICE_REPLY_A=-0.001, RCVD_IN_DNSWL_LOW=-0.7, 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, 26 May 2021 08:38:03 -0000 On 3/15/21 9:26 AM, Glenn Washburn wrote: > On Sun, 14 Mar 2021 19:09:36 +0800 > Tianjia Zhang wrote: > >> ping. >> >> Thanks, >> Tianjia >> >> On 11/18/20 2:58 PM, Tianjia Zhang wrote: >>> 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)); >>> > > LGTM. I wonder if there's some conventions as with local variable > declaration, where we try to have memory allocation near the beginning > of the function as a kind of "declaration" that this should be freed > before leaving the function. > > Reviewed-by: Glenn Washburn > > Glenn > > _______________________________________________ > Grub-devel mailing list > Grub-devel@gnu.org > https://lists.gnu.org/mailman/listinfo/grub-devel > Thanks for your review. Best regards, Tianjia