Quellcode durchsuchen

2006-03-27 Gonzalo Paniagua Javier <[email protected]>

	* HttpUtility.cs: UrlDecode does not throw if an hexadecimal sequence
	fails to parse as an integer. Fixes bug #77931.


svn path=/trunk/mcs/; revision=58625
Gonzalo Paniagua Javier vor 20 Jahren
Ursprung
Commit
fb50f8a7b2

+ 5 - 0
mcs/class/System.Web/System.Web/ChangeLog

@@ -1,3 +1,8 @@
+2006-03-27 Gonzalo Paniagua Javier <[email protected]>
+
+	* HttpUtility.cs: UrlDecode does not throw if an hexadecimal sequence
+	fails to parse as an integer. Fixes bug #77931.
+
 2006-03-23 Gonzalo Paniagua Javier <[email protected]>
 
 	* HttpResponse.cs: more fixes for CacheControl: MS allows to set it to

+ 24 - 6
mcs/class/System.Web/System.Web/HttpUtility.cs

@@ -369,7 +369,18 @@ namespace System.Web {
 		{
 			return e.GetChars (b.GetBuffer (), 0, (int) b.Length);
 		}
-		
+
+		static bool TryParseHexa (string str, out uint result)
+		{
+			result = 0;
+			try {
+				result = UInt32.Parse (str, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
+				return true;
+			} catch {
+				return false;
+			}
+		}
+
 		public static string UrlDecode (string s, Encoding e)
 		{
 			if (null == s) 
@@ -383,8 +394,8 @@ namespace System.Web {
 	
 			StringBuilder output = new StringBuilder ();
 			long len = s.Length;
-			NumberStyles hexa = NumberStyles.HexNumber;
 			MemoryStream bytes = new MemoryStream ();
+			uint xchar;
 	
 			for (int i = 0; i < len; i++) {
 				if (s [i] == '%' && i + 2 < len) {
@@ -393,11 +404,18 @@ namespace System.Web {
 							output.Append (GetChars (bytes, e));
 							bytes.SetLength (0);
 						}
-						output.Append ((char) Int32.Parse (s.Substring (i + 2, 4), hexa));
-						i += 5;
-					} else {
-						bytes.WriteByte ((byte) Int32.Parse (s.Substring (i + 1, 2), hexa));
+
+						if (TryParseHexa (s.Substring (i + 2, 4), out xchar)) {
+							output.Append ((char) xchar);
+							i += 5;
+						} else {
+							output.Append ('%');
+						}
+					} else if (TryParseHexa (s.Substring (i + 1, 2), out xchar)) {
+						bytes.WriteByte ((byte) xchar);
 						i += 2;
+					} else {
+						output.Append ('%');
 					}
 					continue;
 				}