glib: Don’t use time(NULL) to get current time

Use either g_get_real_time() or g_date_time_new_now_local(). This means
we don’t need to worry about time_t being 32b in future (the year 2038
problem), and it makes the need for error handling a bit more explicit.
Improve the error handling in several cases.

Based on a patch by Niels De Graef
(https://gitlab.gnome.org/GNOME/glib/merge_requests/142).

Signed-off-by: Philip Withnall <withnall@endlessm.com>

https://gitlab.gnome.org/GNOME/glib/issues/1402
This commit is contained in:
Philip Withnall 2018-07-06 13:49:55 +01:00
parent c5321810f4
commit 92e059280f
7 changed files with 33 additions and 22 deletions

View File

@ -603,7 +603,7 @@ keyring_generate_entry (const gchar *cookie_context,
gchar **lines;
gint max_line_id;
GString *new_contents;
guint64 now;
gint64 now;
gboolean have_id;
gint use_id;
gchar *use_cookie;
@ -658,7 +658,7 @@ keyring_generate_entry (const gchar *cookie_context,
}
new_contents = g_string_new (NULL);
now = (guint64) time (NULL);
now = g_get_real_time () / G_USEC_PER_SEC;
changed_file = FALSE;
max_line_id = 0;
@ -672,7 +672,7 @@ keyring_generate_entry (const gchar *cookie_context,
gchar **tokens;
gchar *endp;
gint line_id;
guint64 line_when;
gint64 line_when;
gboolean keep_entry;
if (line[0] == '\0')
@ -807,9 +807,9 @@ keyring_generate_entry (const gchar *cookie_context,
g_free (raw_cookie);
g_string_append_printf (new_contents,
"%d %" G_GUINT64_FORMAT " %s\n",
"%d %" G_GINT64_FORMAT " %s\n",
*out_id,
(guint64) time (NULL),
g_get_real_time () / G_USEC_PER_SEC,
*out_cookie);
changed_file = TRUE;
}

View File

@ -1909,7 +1909,7 @@ g_local_file_trash (GFile *file,
int i;
char *data;
gboolean is_homedir_trash;
char delete_time[32];
char *delete_time = NULL;
int fd;
GStatBuf trash_stat, global_stat;
char *dirname, *globaldir;
@ -2137,16 +2137,17 @@ g_local_file_trash (GFile *file,
g_free (topdir);
{
time_t t;
struct tm now;
t = time (NULL);
localtime_r (&t, &now);
delete_time[0] = 0;
strftime(delete_time, sizeof (delete_time), "%Y-%m-%dT%H:%M:%S", &now);
GDateTime *now = g_date_time_new_now_local ();
if (now != NULL)
delete_time = g_date_time_format (now, "%Y-%m-%dT%H:%M:%S");
else
delete_time = g_strdup ("9999-12-31T23:59:59");
g_date_time_unref (now);
}
data = g_strdup_printf ("[Trash Info]\nPath=%s\nDeletionDate=%s\n",
original_name_escaped, delete_time);
g_free (delete_time);
g_file_set_contents (infofile, data, -1, NULL);

View File

@ -273,7 +273,8 @@ main (int argc, char *argv[])
"Negotiated capabilities: unix-fd-passing=%d\n",
g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
greeting = g_strdup_printf ("Hey, it's %" G_GUINT64_FORMAT " already!", (guint64) time (NULL));
greeting = g_strdup_printf ("Hey, it's %" G_GINT64_FORMAT " already!",
g_get_real_time () / G_USEC_PER_SEC);
value = g_dbus_connection_call_sync (connection,
NULL, /* bus_name */
"/org/gtk/GDBus/TestObject",

View File

@ -74,16 +74,14 @@ on_name_appeared (GDBusConnection *connection,
}
else
{
gchar now_buf[256];
time_t now;
gchar *now_buf = NULL;
gssize len;
gchar *str;
GDateTime *now = g_date_time_new_now_local ();
now = time (NULL);
strftime (now_buf,
sizeof now_buf,
"%Y-%m-%d %H:%M:%S",
localtime (&now));
g_assert_nonnull (now);
now_buf = g_date_time_format (now, "%Y-%m-%d %H:%M:%S");
g_date_time_unref (now);
str = g_strdup_printf ("On %s, gdbus-example-unix-fd-client with pid %d was here!\n",
now_buf,
@ -95,6 +93,7 @@ on_name_appeared (GDBusConnection *connection,
g_print ("Wrote the following on server's stdout:\n%s", str);
g_free (str);
g_free (now_buf);
exit (0);
}
}

View File

@ -1387,7 +1387,10 @@ g_date_set_parse (GDate *d,
*
* To set the value of a date to the current day, you could write:
* |[<!-- language="C" -->
* g_date_set_time_t (date, time (NULL));
* time_t now = time (NULL);
* if (now == (time_t) -1)
* // handle the error
* g_date_set_time_t (date, now);
* ]|
*
* Since: 2.10

View File

@ -89,11 +89,14 @@ test_dates (void)
{
GDate *d;
GTimeVal tv;
time_t now;
d = g_date_new ();
/* today */
g_date_set_time (d, time (NULL));
now = time (NULL);
g_assert_cmpint (now, !=, (time_t) -1);
g_date_set_time (d, now);
g_assert (g_date_valid (d));
/* Unix epoch */

View File

@ -112,6 +112,7 @@ test_GDateTime_new_from_unix (void)
memset (&tm, 0, sizeof (tm));
t = time (NULL);
g_assert_cmpint (t, !=, (time_t) -1);
get_localtime_tm (t, &tm);
dt = g_date_time_new_from_unix_local (t);
@ -787,6 +788,7 @@ test_GDateTime_to_unix (void)
time_t t;
t = time (NULL);
g_assert_cmpint (t, !=, (time_t) -1);
dt = g_date_time_new_from_unix_local (t);
g_assert_cmpint (g_date_time_to_unix (dt), ==, t);
g_date_time_unref (dt);
@ -1283,6 +1285,7 @@ test_GDateTime_to_utc (void)
struct tm tm;
t = time (NULL);
g_assert_cmpint (t, !=, (time_t) -1);
#ifdef HAVE_GMTIME_R
gmtime_r (&t, &tm);
#else
@ -1362,6 +1365,7 @@ GDateTime *__dt = g_date_time_new_local (2009, 10, 24, 0, 0, 0);\
* that of the generated timezone.
*/
t = time (NULL);
g_assert_cmpint (t, !=, (time_t) -1);
memset (&tt, 0, sizeof(tt));
get_localtime_tm (t, &tt);
tt.tm_year = 2009 - 1900;