summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilip Withnall <withnall@endlessm.com>2018-07-06 13:49:55 +0100
committerPhilip Withnall <withnall@endlessm.com>2018-07-09 13:28:02 +0200
commit92e059280f5be23cf77bbe4ec016b5cc0f9af959 (patch)
treeacb0aaad14678b0f28ed66b0f0251e4a2483031f
parentc5321810f4d13f228fa5aacb103a1acf1f1578cb (diff)
downloadglib-92e059280f5be23cf77bbe4ec016b5cc0f9af959.tar.gz
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
-rw-r--r--gio/gdbusauthmechanismsha1.c10
-rw-r--r--gio/glocalfile.c15
-rw-r--r--gio/tests/gdbus-example-peer.c3
-rw-r--r--gio/tests/gdbus-example-unix-fd-client.c13
-rw-r--r--glib/gdate.c5
-rw-r--r--glib/tests/date.c5
-rw-r--r--glib/tests/gdatetime.c4
7 files changed, 33 insertions, 22 deletions
diff --git a/gio/gdbusauthmechanismsha1.c b/gio/gdbusauthmechanismsha1.c
index 869946d97..553bcdc32 100644
--- a/gio/gdbusauthmechanismsha1.c
+++ b/gio/gdbusauthmechanismsha1.c
@@ -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;
}
diff --git a/gio/glocalfile.c b/gio/glocalfile.c
index 83dff4342..354ac7c8c 100644
--- a/gio/glocalfile.c
+++ b/gio/glocalfile.c
@@ -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);
diff --git a/gio/tests/gdbus-example-peer.c b/gio/tests/gdbus-example-peer.c
index b954f7426..179aabb7f 100644
--- a/gio/tests/gdbus-example-peer.c
+++ b/gio/tests/gdbus-example-peer.c
@@ -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",
diff --git a/gio/tests/gdbus-example-unix-fd-client.c b/gio/tests/gdbus-example-unix-fd-client.c
index cf1330fda..c676bcbb5 100644
--- a/gio/tests/gdbus-example-unix-fd-client.c
+++ b/gio/tests/gdbus-example-unix-fd-client.c
@@ -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);
}
}
diff --git a/glib/gdate.c b/glib/gdate.c
index bf146d17e..6e163b8a7 100644
--- a/glib/gdate.c
+++ b/glib/gdate.c
@@ -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
diff --git a/glib/tests/date.c b/glib/tests/date.c
index 84c486903..b1057d932 100644
--- a/glib/tests/date.c
+++ b/glib/tests/date.c
@@ -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 */
diff --git a/glib/tests/gdatetime.c b/glib/tests/gdatetime.c
index 79a131ab5..400a2efa3 100644
--- a/glib/tests/gdatetime.c
+++ b/glib/tests/gdatetime.c
@@ -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;