Skip to content

Commit b780498

Browse files
author
Mimi Zohar
committed
lib: add error checking to hex2bin
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 v1: - removed unpack_hex_byte() - changed return code from boolean to int 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@linux.vnet.ibm.com> Acked-by: Andy Shevchenko <andy.shevchenko@gmail.com>
1 parent 6bce98e commit b780498

2 files changed

Lines changed: 12 additions & 5 deletions

File tree

‎include/linux/kernel.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ static inline char *pack_hex_byte(char *buf, u8 byte)
382382
}
383383

384384
extern int hex_to_bin(char ch);
385-
extern void hex2bin(u8 *dst, const char *src, size_t count);
385+
extern int __must_check hex2bin(u8 *dst, const char *src, size_t count);
386386

387387
/*
388388
* General tracing related utility functions - trace_printk(),

‎lib/hexdump.c‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,21 @@ EXPORT_SYMBOL(hex_to_bin);
3838
* @dst: binary result
3939
* @src: ascii hexadecimal string
4040
* @count: result length
41+
*
42+
* Return 0 on success, -1 in case of bad input.
4143
*/
42-
void hex2bin(u8 *dst, const char *src, size_t count)
44+
int hex2bin(u8 *dst, const char *src, size_t count)
4345
{
4446
while (count--) {
45-
*dst = hex_to_bin(*src++) << 4;
46-
*dst += hex_to_bin(*src++);
47-
dst++;
47+
int hi = hex_to_bin(*src++);
48+
int lo = hex_to_bin(*src++);
49+
50+
if ((hi < 0) || (lo < 0))
51+
return -1;
52+
53+
*dst++ = (hi << 4) | lo;
4854
}
55+
return 0;
4956
}
5057
EXPORT_SYMBOL(hex2bin);
5158

0 commit comments

Comments
 (0)