]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
Prevent buffer overflows in ast_uri_encode()
authorMatthew Nicholson <mnicholson@digium.com>
Tue, 11 Jan 2011 18:34:40 +0000 (18:34 +0000)
committerMatthew Nicholson <mnicholson@digium.com>
Tue, 11 Jan 2011 18:34:40 +0000 (18:34 +0000)
ABE-2705

git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/1.4@301305 65c4cc65-6c06-0410-ace0-fbb531ad65f3

main/utils.c

index 17e21390ef2e91dbe4c807921c2f7e4a3f3fbc60..70c66547e90d6642c53efdebc85aebbbc5144ae6 100644 (file)
@@ -388,28 +388,27 @@ char *ast_uri_encode(const char *string, char *outbuf, int buflen, int doreserve
        char *reserved = ";/?:@&=+$,# ";        /* Reserved chars */
 
        const char *ptr  = string;      /* Start with the string */
-       char *out = NULL;
-       char *buf = NULL;
+       char *out = outbuf;
 
-       ast_copy_string(outbuf, string, buflen);
-
-       /* If there's no characters to convert, just go through and don't do anything */
-       while (*ptr) {
+       /* If there's no characters to convert, just go through and copy the string */
+       while (*ptr && out - outbuf < buflen - 1) {
                if ((*ptr < 32) || (doreserved && strchr(reserved, *ptr))) {
-                       /* Oops, we need to start working here */
-                       if (!buf) {
-                               buf = outbuf;
-                               out = buf + (ptr - string) ;    /* Set output ptr */
+                       if (out - outbuf >= buflen - 3) {
+                               break;
                        }
+
                        out += sprintf(out, "%%%02x", (unsigned char) *ptr);
-               } else if (buf) {
-                       *out = *ptr;    /* Continue copying the string */
+               } else {
+                       *out = *ptr;    /* copy the character */
                        out++;
-               } 
+               }
                ptr++;
        }
-       if (buf)
+
+       if (buflen) {
                *out = '\0';
+       }
+
        return outbuf;
 }