diff --git a/glib/gdatetime.c b/glib/gdatetime.c index f64b281be..f3a951cd0 100644 --- a/glib/gdatetime.c +++ b/glib/gdatetime.c @@ -948,6 +948,14 @@ g_date_time_new (GTimeZone *tz, GDateTime *datetime; gint64 full_time; + if (year < 1 || year > 9999 || + month < 1 || month > 12 || + day < 1 || day > 31 || + hour < 0 || hour > 23 || + minute < 0 || minute > 59 || + seconds < 0.0 || seconds >= 60.0) + return NULL; + datetime = g_date_time_alloc (tz); datetime->days = ymd_to_days (year, month, day); datetime->usec = (hour * USEC_PER_HOUR) diff --git a/glib/tests/gdatetime.c b/glib/tests/gdatetime.c index 3bb5ebbe6..0cf5e8505 100644 --- a/glib/tests/gdatetime.c +++ b/glib/tests/gdatetime.c @@ -127,6 +127,17 @@ test_GDateTime_new_from_unix (void) g_date_time_unref (dt); } +static void +test_GDateTime_invalid (void) +{ + GDateTime *dt; + + g_test_bug ("702674"); + + dt = g_date_time_new_utc (2013, -2147483647, 31, 17, 15, 48); + g_assert (dt == NULL); +} + static void test_GDateTime_compare (void) { @@ -1233,7 +1244,7 @@ test_z (void) g_test_bug ("642935"); tz = g_time_zone_new ("-08:00"); - dt = g_date_time_new (tz, 0, 0, 0, 0, 0, 0); + dt = g_date_time_new (tz, 1, 1, 1, 0, 0, 0); p = g_date_time_format (dt, "%z"); g_assert_cmpstr (p, ==, "-0800"); g_date_time_unref (dt); @@ -1540,6 +1551,7 @@ main (gint argc, /* GDateTime Tests */ + g_test_add_func ("/GDateTime/invalid", test_GDateTime_invalid); g_test_add_func ("/GDateTime/add_days", test_GDateTime_add_days); g_test_add_func ("/GDateTime/add_full", test_GDateTime_add_full); g_test_add_func ("/GDateTime/add_hours", test_GDateTime_add_hours);