From: Sascha Hauer <s.hauer@pengutronix.de>
To: Hubert Feurstein <h.feurstein@gmail.com>
Cc: barebox@lists.infradead.org
Subject: Re: [PATCH] commands/digest: add verify support
Date: Sun, 21 Apr 2013 13:22:46 +0200 [thread overview]
Message-ID: <20130421112246.GF32299@pengutronix.de> (raw)
In-Reply-To: <1366361035-28653-1-git-send-email-h.feurstein@gmail.com>
On Fri, Apr 19, 2013 at 10:43:55AM +0200, Hubert Feurstein wrote:
> Signed-off-by: Hubert Feurstein <h.feurstein@gmail.com>
> ---
> commands/digest.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++-------
> 1 file changed, 83 insertions(+), 11 deletions(-)
>
> diff --git a/commands/digest.c b/commands/digest.c
> index c9bb132..56173a5 100644
> --- a/commands/digest.c
> +++ b/commands/digest.c
> @@ -25,24 +25,76 @@
> #include <xfuncs.h>
> #include <malloc.h>
> #include <digest.h>
> +#include <linux/ctype.h>
> +#include <getopt.h>
> +
> +static inline unsigned char parse_hexchar(char c)
> +{
> + if (!isxdigit(c))
> + return 0;
> +
> + return isdigit(c) ? (c - '0') : ((islower(c) ? toupper(c) : c) - 'A' + 0xA);
> +}
> +
> +static inline unsigned char parse_hexbyte(const char *p)
> +{
> + return (parse_hexchar(*p) << 4) | parse_hexchar(*(p + 1));
> +}
>
> static int do_digest(char *algorithm, int argc, char *argv[])
> {
> struct digest *d;
> int ret = 0;
> int i;
> - unsigned char *hash;
> + unsigned char *hash = 0;
> + int opt;
> + unsigned char *verify_hash = 0;
NULL for pointers please.
> + int verify = 0;
> + int min_argc = 2;
>
> d = digest_get_by_name(algorithm);
> BUG_ON(!d);
>
> - if (argc < 2)
> - return COMMAND_ERROR_USAGE;
> + while ((opt = getopt(argc, argv, "v:")) > 0) {
> + switch (opt) {
> + case 'v':
> + verify = 1;
> + min_argc += 2;
> +
> + if (d->length != (strlen(optarg) / 2)) {
> + ret = COMMAND_ERROR_USAGE;
> + goto out;
> + }
Maybe give the user a hint here? It's not really obvious to the user
what's wrong here.
> +
> + verify_hash = calloc(d->length, sizeof(unsigned char));
> + if (!verify_hash) {
> + perror("calloc");
> + ret = -ENOMEM;
> + goto out;
> + }
> +
> + for (i = 0; i < d->length; i++)
> + verify_hash[i] = parse_hexbyte(&optarg[i * 2]);
> +
> + break;
You should only save optarg for later use in the loop. Otherwise when
somebody gives -v multiple times you allocate memory each time which you
don't free later.
> + default:
> + ret = COMMAND_ERROR_USAGE;
> + goto out;
> + }
> + }
> +
> + if (argc < min_argc) {
> + ret = COMMAND_ERROR_USAGE;
> + goto out;
> + }
> +
> + argv += min_argc - 2;
>
> hash = calloc(d->length, sizeof(unsigned char));
> if (!hash) {
> perror("calloc");
> - return COMMAND_ERROR_USAGE;
> + ret = -ENOMEM;
> + goto out;
> }
>
> argv++;
> @@ -60,17 +112,33 @@ static int do_digest(char *algorithm, int argc, char *argv[])
> if (digest_file_window(d, filename, hash, start, size) < 0) {
> ret = 1;
> } else {
> - for (i = 0; i < d->length; i++)
> + for (i = 0; i < d->length; i++) {
> printf("%02x", hash[i]);
> + if (verify > 0 && hash[i] != verify_hash[i])
> + verify = -1;
> + }
>
> - printf(" %s\t0x%08llx ... 0x%08llx\n",
> + printf(" %s\t0x%08llx ... 0x%08llx",
> filename, start, start + size);
> +
> + if (verify < 0) {
> + printf(" ** ERROR **");
> + ret = 1;
> + verify = 1;
> + }
> +
> + printf("\n");
> }
The digest commands can work on multiple files. When verifying you
verify multiple files against a single hash. Is this really useful
behaviour? I'd suggest to allow only a single file in verify mode.
>
> argv++;
> }
>
> - free(hash);
> +out:
> + if (hash)
> + free(hash);
> +
> + if (verify_hash)
> + free(verify_hash);
No need to check. free() works fine on NULL pointers.
Sascha
--
Pengutronix e.K. | |
Industrial Linux Solutions | http://www.pengutronix.de/ |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
next prev parent reply other threads:[~2013-04-21 11:22 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-04-19 8:43 Hubert Feurstein
2013-04-21 11:22 ` Sascha Hauer [this message]
2013-04-23 9:24 ` [PATCHv2] " Hubert Feurstein
2013-04-24 5:55 ` Sascha Hauer
2014-09-24 13:21 [PATCH] " Eric Bénard
2014-09-25 7:15 ` Uwe Kleine-König
2015-02-16 13:02 h.feurstein
2015-02-17 7:39 ` Sascha Hauer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20130421112246.GF32299@pengutronix.de \
--to=s.hauer@pengutronix.de \
--cc=barebox@lists.infradead.org \
--cc=h.feurstein@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox