Skip to content

Commit

Permalink
If an invalid INTERNALDATE is encountered, just use DateTimeOffset.Mi…
Browse files Browse the repository at this point in the history
…nValue

Do not throw a FormatException.

Fixes issue #1236
  • Loading branch information
jstedfast committed Jul 27, 2021
1 parent f1323e6 commit ca0751c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
21 changes: 8 additions & 13 deletions MailKit/Net/Imap/ImapUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/// <summary>
/// Parses the internal date string.
/// </summary>
Expand All @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion UnitTests/Net/Imap/ImapUtilsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -176,7 +177,7 @@ public void TestParseInvalidInternalDates ()
};

foreach (var internalDate in internalDates)
Assert.Throws<FormatException> (() => ImapUtils.ParseInternalDate (internalDate), internalDate);
Assert.AreEqual (DateTimeOffset.MinValue, ImapUtils.ParseInternalDate (internalDate), internalDate);
}

[Test]
Expand Down

0 comments on commit ca0751c

Please sign in to comment.