[Mono-dev] Patch for DateTime.TryParseExact handling of null input string

Tom Philpot tom.philpot at logos.com
Wed Feb 24 13:16:27 EST 2010


.NET 3.5 does not throw a null reference exception on DateTime.TryParseExact if the first parameter is null.

Attached is a test case and a fix for DateTime.cs

Index: System/DateTime.cs
===================================================================
--- System/DateTime.cs	(revision 152377)
+++ System/DateTime.cs	(working copy)
@@ -1164,6 +1164,9 @@
 			result = new DateTime (0);
 			if (format == null)
 				return false;
+			
+			if (s == null)
+				return false;
 
 			if ((style & DateTimeStyles.AllowLeadingWhite) != 0) {
 				format = format.TrimStart (null);
Index: Test/System/DateTimeTest.cs
===================================================================
--- Test/System/DateTimeTest.cs	(revision 152377)
+++ Test/System/DateTimeTest.cs	(working copy)
@@ -2417,6 +2417,15 @@
 			// bug #444103.
 			DateTime.ParseExact ("12:00:00", "HH:mm:ss.FFFFFFF", null);
 		}
+		
+		[Test]
+		public void TryParseExact_NullString ()
+		{
+			DateTime dt;
+			DateTime.TryParseExact(null, "yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'", CultureInfo.InvariantCulture,
+							DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal, out dt);
+			Assert.AreEqual(default(DateTime), dt);
+		}
 #endif
 	}
 }



More information about the Mono-devel-list mailing list