Make g_date_time_new check its arguments

The documentation for this function explicitly gives valid
ranges for the arguments and states that out-of-range arguments
will cause NULL to be returned. Only, the code didn't check
the ranges, and crashed instead. Fix that and add a testcase
for invalid arguments. It turns out that the test_z testcase
was providing invalid arguments and relied on g_date_time_new
to return a non-NULL value anyway, so this commit fixes that
testcase as well.

https://bugzilla.gnome.org/show_bug.cgi?id=702674
This commit is contained in:
Matthias Clasen 2013-08-17 12:35:33 -04:00
parent e70250bbd5
commit 8b3d779d1e
2 changed files with 21 additions and 1 deletions

View File

@ -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)

View File

@ -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);