Re: [RFC][PATCH 2/5] lib: add error checking to hex2bin

From: Andy Shevchenko
Date: Fri Sep 16 2011 - 09:13:50 EST


On Fri, Sep 16, 2011 at 3:50 PM, Mimi Zohar <zohar@xxxxxxxxxxxxxxxxxx> wrote:
> hex2bin converts a hexadecimal string to its binary representation.
> The original version of hex2bin did not do any error checking. ÂThis
> patch adds error checking and returns the result.
>
> Changelog:
> - use the new unpack_hex_byte()
> - add __must_check compiler option (Andy Shevchenko's suggestion)
> - change function API to return error checking result
> Â(based on Tetsuo Handa's initial patch)
>
> Signed-off-by: Mimi Zohar <zohar@xxxxxxxxxxxxxxxxxx>
> ---
> Âinclude/linux/kernel.h | Â Â2 +-
> Âlib/hexdump.c     Â|  11 +++++++----
> Â2 files changed, 8 insertions(+), 5 deletions(-)
>
> diff --git a/include/linux/kernel.h b/include/linux/kernel.h
> index d8ea13b..3021e64 100644
> --- a/include/linux/kernel.h
> +++ b/include/linux/kernel.h
> @@ -382,7 +382,7 @@ static inline char *pack_hex_byte(char *buf, u8 byte)
> Â}
>
> Âextern int hex_to_bin(char ch);
> -extern void hex2bin(u8 *dst, const char *src, size_t count);
> +extern bool __must_check hex2bin(u8 *dst, const char *src, size_t count);
>
> Â/*
> Â* unpack_hex_byte - convert 2 asii hex digits into a byte
> diff --git a/lib/hexdump.c b/lib/hexdump.c
> index f5fe6ba..7711095 100644
> --- a/lib/hexdump.c
> +++ b/lib/hexdump.c
> @@ -38,14 +38,17 @@ EXPORT_SYMBOL(hex_to_bin);
> Â* @dst: binary result
> Â* @src: ascii hexadecimal string
> Â* @count: result length
> + *
> + * Returns true on success, false in case of bad input.
> Â*/
> -void hex2bin(u8 *dst, const char *src, size_t count)
> +bool hex2bin(u8 *dst, const char *src, size_t count)
> Â{
> Â Â Â Âwhile (count--) {
> - Â Â Â Â Â Â Â *dst = hex_to_bin(*src++) << 4;
> - Â Â Â Â Â Â Â *dst += hex_to_bin(*src++);
> - Â Â Â Â Â Â Â dst++;
> + Â Â Â Â Â Â Â if (!unpack_hex_byte(dst++, src))
> + Â Â Â Â Â Â Â Â Â Â Â return false;
> + Â Â Â Â Â Â Â src += 2;
> Â Â Â Â}
> + Â Â Â return true;
> Â}
> ÂEXPORT_SYMBOL(hex2bin);
>

Acked-by: Andy Shevchenko <andriy.shevchenko@xxxxxxxxxxxxxxx>

--
With Best Regards,
Andy Shevchenko
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/