From b660d584ca45e74f15711471be3ce9618cdc8d7e Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Sun, 30 Dec 2012 22:09:34 +0100 Subject: [PATCH] clean up uh_urldecode, null-terminate string --- utils.c | 31 +++++++++---------------------- 1 file changed, 9 insertions(+), 22 deletions(-) diff --git a/utils.c b/utils.c index ec0b3aa..aabb430 100644 --- a/utils.c +++ b/utils.c @@ -99,31 +99,18 @@ int uh_urldecode(char *buf, int blen, const char *src, int slen) for (i = 0; (i < slen) && (len < blen); i++) { - if (src[i] == '%') - { - if (((i+2) < slen) && isxdigit(src[i+1]) && isxdigit(src[i+2])) - { - buf[len++] = (char)(16 * hex(src[i+1]) + hex(src[i+2])); - i += 2; - } - else - { - /* Encoding error: it's hard to think of a - ** scenario in which returning an incorrect - ** 'decoding' of the malformed string is - ** preferable to signaling an error condition. */ - #if 0 /* WORSE_IS_BETTER */ - buf[len++] = '%'; - #else - return -2; - #endif - } - } - else - { + if (src[i] != '%') { buf[len++] = src[i]; + continue; } + + if (i + 2 >= slen || !isxdigit(src[i + 1]) || !isxdigit(src[i + 2])) + return -2; + + buf[len++] = (char)(16 * hex(src[i+1]) + hex(src[i+2])); + i += 2; } + buf[len] = 0; return (i == slen) ? len : -1; } -- 2.30.2