From d30414b3a302cf9b409feb35086a14b4465d1ee5 Mon Sep 17 00:00:00 2001 From: Craig Barnes Date: Mon, 4 Jan 2021 05:25:14 +0000 Subject: [PATCH 1/2] uri: use nibble2hex() instead of isxdigit(3) to check valid hex digits --- uri.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/uri.c b/uri.c index 31bbc7b6..57852f1b 100644 --- a/uri.c +++ b/uri.c @@ -2,7 +2,6 @@ #include #include -#include #include #include #include @@ -12,6 +11,10 @@ #include "log.h" #include "xmalloc.h" +enum { + HEX_DIGIT_INVALID = 16 +}; + static uint8_t nibble2hex(char c) { @@ -27,8 +30,7 @@ nibble2hex(char c) return c - 'A' + 10; } - assert(false); - return 0; + return HEX_DIGIT_INVALID; } bool @@ -199,7 +201,7 @@ uri_parse(const char *uri, size_t len, encoded_len -= prefix_len; decoded_len += prefix_len; - if (isxdigit(next[1]) && isxdigit(next[2])) { + if (nibble2hex(next[1]) <= 15 && nibble2hex(next[2]) <= 15) { *p++ = nibble2hex(next[1]) << 4 | nibble2hex(next[2]); decoded_len++; encoded_len -= 3; From b9a7cbf21daf00b21b2e2b516286761a39a9781b Mon Sep 17 00:00:00 2001 From: Craig Barnes Date: Mon, 4 Jan 2021 05:31:19 +0000 Subject: [PATCH 2/2] uri: rename nibbletohex() function to hex2nibble() It converts a hex digit to a nibble, not the other way around. --- uri.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/uri.c b/uri.c index 57852f1b..a0e3838a 100644 --- a/uri.c +++ b/uri.c @@ -16,7 +16,7 @@ enum { }; static uint8_t -nibble2hex(char c) +hex2nibble(char c) { switch (c) { case '0': case '1': case '2': case '3': case '4': @@ -201,8 +201,8 @@ uri_parse(const char *uri, size_t len, encoded_len -= prefix_len; decoded_len += prefix_len; - if (nibble2hex(next[1]) <= 15 && nibble2hex(next[2]) <= 15) { - *p++ = nibble2hex(next[1]) << 4 | nibble2hex(next[2]); + if (hex2nibble(next[1]) <= 15 && hex2nibble(next[2]) <= 15) { + *p++ = hex2nibble(next[1]) << 4 | hex2nibble(next[2]); decoded_len++; encoded_len -= 3; encoded = next + 3;