[glib] tests: Fix overflows in find_maximum_supported_tv_sec()



commit 428acd9b1486beba186c073577ea659eee44ae0a
Author: Philip Withnall <withnall endlessm com>
Date:   Mon Jun 19 13:57:36 2017 +0100

    tests: Fix overflows in find_maximum_supported_tv_sec()
    
    The addition (highest_success + lowest_failure) could have overflowed,
    and typically would do on 32-bit platforms where the real
    highest_success should be G_MAXLONG. Fix that, and introduce special
    handling of the corner case of (highest_success = G_MAXLONG).
    
    Signed-off-by: Philip Withnall <withnall endlessm com>
    
    https://bugzilla.gnome.org/show_bug.cgi?id=783841

 glib/tests/gdatetime.c |   16 ++++++++++++----
 1 files changed, 12 insertions(+), 4 deletions(-)
---
diff --git a/glib/tests/gdatetime.c b/glib/tests/gdatetime.c
index 3985445..4cb5a0a 100644
--- a/glib/tests/gdatetime.c
+++ b/glib/tests/gdatetime.c
@@ -370,19 +370,27 @@ test_GDateTime_new_from_timeval (void)
   g_date_time_unref (dt);
 }
 
-static gint64
+static glong
 find_maximum_supported_tv_sec (void)
 {
   glong highest_success = 0, lowest_failure = G_MAXLONG;
   GTimeVal tv;
+  GDateTime *dt = NULL;
 
   tv.tv_usec = 0;
 
-  while (highest_success < lowest_failure - 1)
+  /* Corner case of all glong values being valid. */
+  tv.tv_sec = G_MAXLONG;
+  dt = g_date_time_new_from_timeval_utc (&tv);
+  if (dt != NULL)
     {
-      GDateTime *dt;
+      highest_success = tv.tv_sec;
+      g_date_time_unref (dt);
+    }
 
-      tv.tv_sec = (highest_success + lowest_failure) / 2;
+  while (highest_success < lowest_failure - 1)
+    {
+      tv.tv_sec = highest_success + (lowest_failure - highest_success) / 2;
       dt = g_date_time_new_from_timeval_utc (&tv);
 
       if (dt != NULL)


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