From ca0751cb0fbde40764852151235fc5a642e58107 Mon Sep 17 00:00:00 2001 From: Jeffrey Stedfast Date: Tue, 27 Jul 2021 11:59:55 -0400 Subject: [PATCH] If an invalid INTERNALDATE is encountered, just use DateTimeOffset.MinValue Do not throw a FormatException. Fixes issue #1236 --- MailKit/Net/Imap/ImapUtils.cs | 21 ++++++++------------- UnitTests/Net/Imap/ImapUtilsTests.cs | 3 ++- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/MailKit/Net/Imap/ImapUtils.cs b/MailKit/Net/Imap/ImapUtils.cs index cffd50aa10..e2f9fec318 100644 --- a/MailKit/Net/Imap/ImapUtils.cs +++ b/MailKit/Net/Imap/ImapUtils.cs @@ -195,11 +195,6 @@ static bool TryGetTimeZone (string text, ref int index, out TimeSpan timezone) return true; } - static Exception InvalidInternalDateFormat (string text) - { - return new FormatException ("Invalid INTERNALDATE format: " + text); - } - /// /// Parses the internal date string. /// @@ -215,37 +210,37 @@ public static DateTimeOffset ParseInternalDate (string text) index++; if (index >= text.Length || !TryGetInt32 (text, ref index, '-', out day) || day < 1 || day > 31) - throw InvalidInternalDateFormat (text); + return DateTimeOffset.MinValue; index++; if (index >= text.Length || !TryGetMonth (text, ref index, '-', out month)) - throw InvalidInternalDateFormat (text); + return DateTimeOffset.MinValue; index++; if (index >= text.Length || !TryGetInt32 (text, ref index, ' ', out year) || year < 1969) - throw InvalidInternalDateFormat (text); + return DateTimeOffset.MinValue; index++; if (index >= text.Length || !TryGetInt32 (text, ref index, ':', out hour) || hour > 23) - throw InvalidInternalDateFormat (text); + return DateTimeOffset.MinValue; index++; if (index >= text.Length || !TryGetInt32 (text, ref index, ':', out minute) || minute > 59) - throw InvalidInternalDateFormat (text); + return DateTimeOffset.MinValue; index++; if (index >= text.Length || !TryGetInt32 (text, ref index, ' ', out second) || second > 59) - throw InvalidInternalDateFormat (text); + return DateTimeOffset.MinValue; index++; if (index >= text.Length || !TryGetTimeZone (text, ref index, out timezone)) - throw InvalidInternalDateFormat (text); + return DateTimeOffset.MinValue; while (index < text.Length && char.IsWhiteSpace (text[index])) index++; if (index < text.Length) - throw InvalidInternalDateFormat (text); + return DateTimeOffset.MinValue; // return DateTimeOffset.ParseExact (text.Trim (), "d-MMM-yyyy HH:mm:ss zzz", CultureInfo.InvariantCulture.DateTimeFormat); return new DateTimeOffset (year, month, day, hour, minute, second, timezone); diff --git a/UnitTests/Net/Imap/ImapUtilsTests.cs b/UnitTests/Net/Imap/ImapUtilsTests.cs index 8f5752b53d..6ea54aaeed 100644 --- a/UnitTests/Net/Imap/ImapUtilsTests.cs +++ b/UnitTests/Net/Imap/ImapUtilsTests.cs @@ -165,6 +165,7 @@ public void TestFormattingReversedUids () public void TestParseInvalidInternalDates () { var internalDates = new string [] { + "00-Jan-0000 00:00:00 +0000", // Note: This example is taken from an actual response from a Domino IMAP server. Likely represents an uninitialized value. "98765432100-OCT-2018 13:41:57 -0400", "27-JAG-2018 13:41:57 -0400", "27-OCT-1909 13:41:57 -0400", @@ -176,7 +177,7 @@ public void TestParseInvalidInternalDates () }; foreach (var internalDate in internalDates) - Assert.Throws (() => ImapUtils.ParseInternalDate (internalDate), internalDate); + Assert.AreEqual (DateTimeOffset.MinValue, ImapUtils.ParseInternalDate (internalDate), internalDate); } [Test]