[asterisk-commits] mnicholson: branch 1.4 r301305 - /branches/1.4/main/utils.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Jan 11 12:34:46 CST 2011


Author: mnicholson
Date: Tue Jan 11 12:34:40 2011
New Revision: 301305

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=301305
Log:
Prevent buffer overflows in ast_uri_encode()

ABE-2705

Modified:
    branches/1.4/main/utils.c

Modified: branches/1.4/main/utils.c
URL: http://svnview.digium.com/svn/asterisk/branches/1.4/main/utils.c?view=diff&rev=301305&r1=301304&r2=301305
==============================================================================
--- branches/1.4/main/utils.c (original)
+++ branches/1.4/main/utils.c Tue Jan 11 12:34:40 2011
@@ -388,28 +388,27 @@
 	char *reserved = ";/?:@&=+$,# ";	/* Reserved chars */
 
  	const char *ptr  = string;	/* Start with the string */
-	char *out = NULL;
-	char *buf = NULL;
-
-	ast_copy_string(outbuf, string, buflen);
-
-	/* If there's no characters to convert, just go through and don't do anything */
-	while (*ptr) {
+	char *out = outbuf;
+
+	/* 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;
 }
 




More information about the asterisk-commits mailing list