Skip to contentSkip to conversation

grub-mkpasswd-pbkdf2: Simplify the main function implementation

PATCH In review Tianjia Zhang 3 participants 6 messages Nov 18, 2020 – May 26, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 grub-mkpasswd-pbkdf2: Simplify the main function implementation

Allocate memory if needed, while saving the corresponding release operation, reducing the amount of code and code complexity.

---
 util/grub-mkpasswd-pbkdf2.c | 20 +++++---------------
 1 file changed, 5 insertions(+), 15 deletions(-)
util/grub-mkpasswd-pbkdf2.c +5 -15
index 5805f3c10..cd7943df6 100644
@@ -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));
Signature · 1 line
2.24.3 (Apple Git-128)
#1 lore raw

ping.

Thanks,
Tianjia

Quoted 54 lines · Tianjia Zhang

Allocate memory if needed, while saving the corresponding release operation, reducing the amount of code and code complexity.

---
  util/grub-mkpasswd-pbkdf2.c | 20 +++++---------------
  1 file changed, 5 insertions(+), 15 deletions(-)
util/grub-mkpasswd-pbkdf2.c +5 -15
index 5805f3c10..cd7943df6 100644
@@ -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));
Reviewed-by
#2 lore raw
Quoted 53 lines · On Sun, 14 Mar 2021 19:09:36 +0800 Tianjia Zhang…

ping.

Thanks,
Tianjia

Quoted 47 lines · Tianjia Zhang

Allocate memory if needed, while saving the corresponding release operation, reducing the amount of code and code complexity.

---
  util/grub-mkpasswd-pbkdf2.c | 20 +++++---------------
  1 file changed, 5 insertions(+), 15 deletions(-)
(quoted diff) +0 -0

b/util/grub-mkpasswd-pbkdf2.c index 5805f3c10..cd7943df6 100644

util/grub-mkpasswd-pbkdf2.c +2 -15
@@ -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));
 
Quoted 7 lines

util/grub-mkpasswd-pbkdf2.c main (int argc, char *argv[]) · line 156

Quoted 7 lines
util/grub-mkpasswd-pbkdf2.c +3 -0
+  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.

Glenn

#3 lore raw
Quoted 69 lines · Glenn Washburn
Quoted 53 lines · On Sun, 14 Mar 2021 19:09:36 +0800 Tianjia Zhang…

ping.

Thanks,
Tianjia

Quoted 47 lines · Tianjia Zhang

Allocate memory if needed, while saving the corresponding release operation, reducing the amount of code and code complexity.

---
   util/grub-mkpasswd-pbkdf2.c | 20 +++++---------------
   1 file changed, 5 insertions(+), 15 deletions(-)
(quoted diff) +0 -0

b/util/grub-mkpasswd-pbkdf2.c index 5805f3c10..cd7943df6 100644

util/grub-mkpasswd-pbkdf2.c +2 -15
@@ -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));
 
Quoted 7 lines

util/grub-mkpasswd-pbkdf2.c main (int argc, char *argv[]) · line 156

Quoted 7 lines
util/grub-mkpasswd-pbkdf2.c +3 -0
+  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.

Quoted 9 lines

Glenn

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

Thanks for your review.

Best regards,
Tianjia

#4 lore raw
In reply to Tianjia Zhang

Hi Daniel,

Please take the time to look at this patch.

Thanks,
Tianjia

Quoted 54 lines · Tianjia Zhang

Allocate memory if needed, while saving the corresponding release operation, reducing the amount of code and code complexity.

---
  util/grub-mkpasswd-pbkdf2.c | 20 +++++---------------
  1 file changed, 5 insertions(+), 15 deletions(-)
util/grub-mkpasswd-pbkdf2.c +5 -15
index 5805f3c10..cd7943df6 100644
@@ -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));
#5 lore raw

Hi Tianjia,

Quoted 3 lines · Tianjia Zhang

Hi Daniel,

Please take the time to look at this patch.

Thank you for the patch. I do not consider it as release material. So, I will take a look at this patch after release.

Daniel