On Thu, Sep 15, 2022 at 04:29:36AM -0400, Varik Valefor wrote:
> This change removes a fairly large routine from a large function and places
> the fairly large routine into a dedicated function.  The decreasing of the
> length of the main function should increase the readability of the large
> function.

This patch does not apply cleanly onto current CVS for me.

> 
> Varik Valefor
> "When lost, just pure :: Monad m => a -> m a."
> 
> diff --git a/sbin/bioctl/bioctl.c b/sbin/bioctl/bioctl.c
> index 58561e5f30f..19963ad762f 100644
> --- a/sbin/bioctl/bioctl.c
> +++ b/sbin/bioctl/bioctl.c
> @@ -1307,6 +1307,29 @@ bcrypt_pbkdf_autorounds(void)
> 
>         return r;
>  }
> +void
> +readpassphrasefile(char *passphrase, FILE *f, size_t pl, struct stat sb)
> +{

*f, pl and sb are still declared in derive_key() but unused there,
so they might as well be local to this new function.

> +        if ((f = fopen(password, "r")) == NULL)
> +                err(1, "invalid passphrase file");
> +
> +        if (fstat(fileno(f), &sb) == -1)
> +                err(1, "can't stat passphrase file");
> +        if (sb.st_uid != 0)
> +                errx(1, "passphrase file must be owned by root");
> +        if ((sb.st_mode & ~S_IFMT) != (S_IRUSR | S_IWUSR))
> +                errx(1, "passphrase file has the wrong permissions");
> +
> +        if (fgets(passphrase, sizeof(passphrase), f) == NULL)
> +                err(1, "can't read passphrase file");
> +        pl = strlen(passphrase);
> +        if (pl > 0 && passphrase[pl - 1] == '\n')
> +                passphrase[pl - 1] = '\0';
> +        else
> +                errx(1, "invalid passphrase length");
> +
> +        fclose(f);
> +}
> 
>  void
>  derive_key(u_int32_t type, int rounds, u_int8_t *key, size_t keysz,
> @@ -1331,25 +1354,7 @@ derive_key(u_int32_t type, int rounds, u_int8_t *key,
> size_t keysz,
> 
>         /* get passphrase */
>         if (password) {
> -               if ((f = fopen(password, "r")) == NULL)
> -                       err(1, "invalid passphrase file");
> -
> -               if (fstat(fileno(f), &sb) == -1)
> -                       err(1, "can't stat passphrase file");
> -               if (sb.st_uid != 0)
> -                       errx(1, "passphrase file must be owned by root");
> -               if ((sb.st_mode & ~S_IFMT) != (S_IRUSR | S_IWUSR))
> -                       errx(1, "passphrase file has the wrong
> permissions");
> -
> -               if (fgets(passphrase, sizeof(passphrase), f) == NULL)
> -                       err(1, "can't read passphrase file");
> -               pl = strlen(passphrase);
> -               if (pl > 0 && passphrase[pl - 1] == '\n')
> -                       passphrase[pl - 1] = '\0';
> -               else
> -                       errx(1, "invalid passphrase length");
> -
> -               fclose(f);
> +               readpassphrasefile(passphrase, f, pl, sb);
>         } else {
>                 if (readpassphrase(prompt, passphrase, sizeof(passphrase),
>                     rpp_flag) == NULL)
> 

Reply via email to