]> arthur.barton.de Git - netdata.git/commitdiff
Try to shrink the buffer used on url_encode().
authorFrederico Lamberti Pissarra <fredericopissarra@gmail.com>
Mon, 9 May 2016 03:23:01 +0000 (00:23 -0300)
committerFrederico Lamberti Pissarra <fredericopissarra@gmail.com>
Mon, 9 May 2016 03:23:01 +0000 (00:23 -0300)
src/url.c

index edf52be7c7c2306c19116e58c54c61524dc90e74..010b07dddc69a8a88253d6cc7bf16d28ca665332 100644 (file)
--- a/src/url.c
+++ b/src/url.c
@@ -27,28 +27,36 @@ char to_hex(char code) {
 /* Returns a url-encoded version of str */
 /* IMPORTANT: be sure to free() the returned string after use */
 char *url_encode(char *str) {
-       char *pstr = str,
-               *buf = malloc(strlen(str) * 3 + 1),
-               *pbuf = buf;
+       char *buf, *pbuf;
+
+       pbuf = buf = malloc(strlen(str) * 3 + 1);
 
        if(!buf)
                fatal("Cannot allocate memory.");
 
-       while (*pstr) {
-               if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~')
-                       *pbuf++ = *pstr;
+       while (*str) {
+               if (isalnum(*str) || *str == '-' || *str == '_' || *str == '.' || *str == '~')
+                       *pbuf++ = *str;
 
-               else if (*pstr == ' ')
+               else if (*str == ' ')
                        *pbuf++ = '+';
 
                else
-                       *pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15);
+                       *pbuf++ = '%', *pbuf++ = to_hex(*str >> 4), *pbuf++ = to_hex(*str & 15);
 
-               pstr++;
+               str++;
        }
-
        *pbuf = '\0';
 
+       // FIX: I think this is prudent. URLs can be as long as 2 KiB or more.
+       //      We allocated 3 times more space to accomodate %NN encoding of
+       //      non ASCII chars. If URL has none of these kind of chars we will
+       //      end up with a big unused buffer.
+       //
+       //      Try to shrink the buffer...
+       if (!!(pbuf = (char *)realloc(buf, strlen(buf)+1)))
+               buf = pbuf;
+
        return buf;
 }