From 6605f4ddaa0d363b12f140e61527825852aa3558 Mon Sep 17 00:00:00 2001 From: Philipp Schafft Date: Wed, 8 Apr 2015 10:25:25 +0000 Subject: [PATCH] Fix: Let util_url_escape() handle NULL parameter. This let util_url_escape() handle NULL passed as parameter. In case the parameter is NULL it will also return NULL. This patch also does some cleanup of the code such as migration away from int and thus avoiding future failtures. This is a re-apply of 3baa4e46aa4f40b0da653a1dfc43efd9a9475048 as git refuses to cherry-pick. --- src/util.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/util.c b/src/util.c index 43954c0c..5eaed9c7 100644 --- a/src/util.c +++ b/src/util.c @@ -265,21 +265,25 @@ static char safechars[256] = { char *util_url_escape (const char *src) { - int len = strlen(src); - /* Efficiency not a big concern here, keep the code simple/conservative */ - char *dst = calloc(1, len*3 + 1); + size_t len; + char *dst; unsigned char *source = (unsigned char *)src; - int i,j=0; + size_t i, j; - for(i=0; i < len; i++) { + if (!src) + return NULL; + + len = strlen(src); + /* Efficiency not a big concern here, keep the code simple/conservative */ + dst = calloc(1, len*3 + 1); + + for(i = 0, j = 0; i < len; i++) { if(safechars[source[i]]) { dst[j++] = source[i]; - } - else { - dst[j] = '%'; - dst[j+1] = hexchars[ (source[i] >> 4) & 0xf ]; - dst[j+2] = hexchars[ source[i] & 0xf ]; - j+= 3; + } else { + dst[j++] = '%'; + dst[j++] = hexchars[(source[i] >> 4) & 0x0F]; + dst[j++] = hexchars[ source[i] & 0x0F]; } }