From 30fed3b906f3408aa4fc9a7996fa03cf7b940ebe Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Fri, 16 Jun 2017 12:42:32 +0100 Subject: [PATCH] tests: Fix GDateTime overflow tests on 32-bit architectures MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On architectures where sizeof(glong) == 32 bits, there are no problems with overflow when constructing a GDateTime from a GTimeVal. Adjust the test for this by basing it on the maximum supported tv_sec value it can calculate, rather than a fixed ‘known unsupported’ value. Signed-off-by: Philip Withnall https://bugzilla.gnome.org/show_bug.cgi?id=783841 --- glib/tests/gdatetime.c | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/glib/tests/gdatetime.c b/glib/tests/gdatetime.c index db04870d2..398544500 100644 --- a/glib/tests/gdatetime.c +++ b/glib/tests/gdatetime.c @@ -401,7 +401,10 @@ find_maximum_supported_tv_sec (void) /* Check that trying to create a #GDateTime too far in the future reliably * fails. With a #GTimeVal, this is subtle, as the tv_usec are added into the - * calculation part-way through. */ + * calculation part-way through. + * + * This varies a bit between 32- and 64-bit architectures, due to the + * differences in the size of glong (tv.tv_sec). */ static void test_GDateTime_new_from_timeval_overflow (void) { @@ -410,29 +413,33 @@ test_GDateTime_new_from_timeval_overflow (void) g_test_bug ("782089"); - tv.tv_sec = G_MAXLONG; - tv.tv_usec = 0; - - dt = g_date_time_new_from_timeval_utc (&tv); - g_assert_null (dt); - - dt = g_date_time_new_from_timeval_local (&tv); - g_assert_null (dt); - tv.tv_sec = find_maximum_supported_tv_sec (); tv.tv_usec = G_USEC_PER_SEC - 1; g_test_message ("Maximum supported GTimeVal.tv_sec = %lu", tv.tv_sec); + /* Sanity check: do we support the year 2000? */ + g_assert_cmpint (tv.tv_sec, >=, 946684800); + dt = g_date_time_new_from_timeval_utc (&tv); g_assert_nonnull (dt); g_date_time_unref (dt); - tv.tv_sec++; - tv.tv_usec = 0; + dt = g_date_time_new_from_timeval_local (&tv); + g_assert_nonnull (dt); + g_date_time_unref (dt); - dt = g_date_time_new_from_timeval_utc (&tv); - g_assert_null (dt); + if (tv.tv_sec < G_MAXLONG) + { + tv.tv_sec++; + tv.tv_usec = 0; + + dt = g_date_time_new_from_timeval_utc (&tv); + g_assert_null (dt); + + dt = g_date_time_new_from_timeval_local (&tv); + g_assert_null (dt); + } } static void