summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelix Fietkau <nbd@openwrt.org>2012-12-30 22:09:34 +0100
committerFelix Fietkau <nbd@openwrt.org>2012-12-30 22:09:34 +0100
commitb660d584ca45e74f15711471be3ce9618cdc8d7e (patch)
tree9d1000e8bea304024cbc827cbda0602b97f6ce73
parent5ddf71b3638788f8d1dd1d312ec79acfac7f3e74 (diff)
clean up uh_urldecode, null-terminate string
-rw-r--r--utils.c31
1 files 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;
}