[glib] Make g_date_time_new check its arguments



commit 8b3d779d1ece535680724812fddffbdc940f328c
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Aug 17 12:35:33 2013 -0400

    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

 glib/gdatetime.c       |    8 ++++++++
 glib/tests/gdatetime.c |   14 +++++++++++++-
 2 files changed, 21 insertions(+), 1 deletions(-)
---
diff --git a/glib/gdatetime.c b/glib/gdatetime.c
index f64b281..f3a951c 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 3bb5ebb..0cf5e85 100644
--- a/glib/tests/gdatetime.c
+++ b/glib/tests/gdatetime.c
@@ -128,6 +128,17 @@ test_GDateTime_new_from_unix (void)
 }
 
 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)
 {
   GDateTime *dt1, *dt2;
@@ -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);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]