]> git.saurik.com Git - wxWidgets.git/commitdiff
Check wxDateTime components validity more rigorously.
authorVadim Zeitlin <vadim@wxwidgets.org>
Thu, 18 Nov 2010 12:41:13 +0000 (12:41 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Thu, 18 Nov 2010 12:41:13 +0000 (12:41 +0000)
Check that the provided day is strictly positive and also that the month is in
valid range: while it should always be, considering that it's an enum element,
in practice people often cast ints to wxDateTime::Month with potentially fatal
results. Catch this with an assert in wxDateTime::Tm::IsValid().

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@66203 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

src/common/datetime.cpp

index 2a26b7b1213c7e51535f551678a7cdc0f5e6f4b1..20b8e782400b54488f6259567be371f65f7860b1 100644 (file)
@@ -519,9 +519,17 @@ wxDateTime::Tm::Tm(const struct tm& tm, const TimeZone& tz)
 
 bool wxDateTime::Tm::IsValid() const
 {
+    if ( mon == wxDateTime::Inv_Month )
+        return false;
+
+    // We need to check this here to avoid crashing in GetNumOfDaysInMonth() if
+    // somebody passed us "(wxDateTime::Month)1000".
+    wxCHECK_MSG( mon >= wxDateTime::Jan && mon < wxDateTime::Inv_Month, false,
+                 wxS("Invalid month value") );
+
     // we allow for the leap seconds, although we don't use them (yet)
     return (year != wxDateTime::Inv_Year) && (mon != wxDateTime::Inv_Month) &&
-           (mday <= GetNumOfDaysInMonth(year, mon)) &&
+           (mday > 0 && mday <= GetNumOfDaysInMonth(year, mon)) &&
            (hour < 24) && (min < 60) && (sec < 62) && (msec < 1000);
 }