Browse Source

lib: hex: remove unnecessary defensive programming

The hex2char() calls in bin2hex() can never fail since buf[i] >> 4
and buf[i] & 0xf always produce values in range 0-15.

Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
pull/90351/merge
Benjamin Cabé 4 weeks ago committed by Dan Kalowsky
parent
commit
455280e5aa
  1. 8
      lib/utils/hex.c

8
lib/utils/hex.c

@ -44,12 +44,8 @@ size_t bin2hex(const uint8_t *buf, size_t buflen, char *hex, size_t hexlen)
} }
for (size_t i = 0; i < buflen; i++) { for (size_t i = 0; i < buflen; i++) {
if (hex2char(buf[i] >> 4, &hex[2U * i]) < 0) { hex2char(buf[i] >> 4, &hex[2U * i]);
return 0; hex2char(buf[i] & 0xf, &hex[2U * i + 1U]);
}
if (hex2char(buf[i] & 0xf, &hex[2U * i + 1U]) < 0) {
return 0;
}
} }
hex[2U * buflen] = '\0'; hex[2U * buflen] = '\0';

Loading…
Cancel
Save