]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blobdiff - lib/hexdump.c
lib: add error checking to hex2bin
[mirror_ubuntu-artful-kernel.git] / lib / hexdump.c
index f5fe6ba7a3ab35f163a63c86e6cac8015a02be44..51d5ae210244e5a452364882746dd8c7eaafef20 100644 (file)
@@ -38,14 +38,21 @@ EXPORT_SYMBOL(hex_to_bin);
  * @dst: binary result
  * @src: ascii hexadecimal string
  * @count: result length
+ *
+ * Return 0 on success, -1 in case of bad input.
  */
-void hex2bin(u8 *dst, const char *src, size_t count)
+int hex2bin(u8 *dst, const char *src, size_t count)
 {
        while (count--) {
-               *dst = hex_to_bin(*src++) << 4;
-               *dst += hex_to_bin(*src++);
-               dst++;
+               int hi = hex_to_bin(*src++);
+               int lo = hex_to_bin(*src++);
+
+               if ((hi < 0) || (lo < 0))
+                       return -1;
+
+               *dst++ = (hi << 4) | lo;
        }
+       return 0;
 }
 EXPORT_SYMBOL(hex2bin);