From 789af6acdc2d072a1d5891900198cc8cb7bf5227 Mon Sep 17 00:00:00 2001 From: Niels De Graef Date: Tue, 26 Jun 2018 23:15:39 +0200 Subject: [PATCH 1/2] Prefer g_get_real_time() over time(NULL) Especially when casting the result to guint64, since this might not be always correct. See also issue #1402. --- gio/gdbusauthmechanismsha1.c | 12 ++++++------ gio/glocalfile.c | 2 +- gio/tests/gdbus-example-peer.c | 3 ++- gio/tests/gdbus-example-unix-fd-client.c | 2 +- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/gio/gdbusauthmechanismsha1.c b/gio/gdbusauthmechanismsha1.c index 869946d976..d47221f5b2 100644 --- a/gio/gdbusauthmechanismsha1.c +++ b/gio/gdbusauthmechanismsha1.c @@ -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 (); 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') @@ -745,7 +745,7 @@ keyring_generate_entry (const gchar *cookie_context, if (line_when - now > 24*60*60) { keep_entry = FALSE; - _log ("Deleted SHA1 cookie from %" G_GUINT64_FORMAT " seconds in the future", line_when - now); + _log ("Deleted SHA1 cookie from %" G_GINT64_FORMAT " seconds in the future", line_when - now); } } else @@ -764,7 +764,7 @@ keyring_generate_entry (const gchar *cookie_context, else { g_string_append_printf (new_contents, - "%d %" G_GUINT64_FORMAT " %s\n", + "%d %" G_GINT64_FORMAT " %s\n", line_id, line_when, tokens[2]); @@ -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 (), *out_cookie); changed_file = TRUE; } diff --git a/gio/glocalfile.c b/gio/glocalfile.c index 1c8fa42e8b..cf0b8feb5e 100644 --- a/gio/glocalfile.c +++ b/gio/glocalfile.c @@ -2138,7 +2138,7 @@ g_local_file_trash (GFile *file, { time_t t; struct tm now; - t = time (NULL); + t = g_get_real_time (); localtime_r (&t, &now); delete_time[0] = 0; strftime(delete_time, sizeof (delete_time), "%Y-%m-%dT%H:%M:%S", &now); diff --git a/gio/tests/gdbus-example-peer.c b/gio/tests/gdbus-example-peer.c index b954f7426d..767967ecb8 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 ()); 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 cf1330fda4..48a187d65e 100644 --- a/gio/tests/gdbus-example-unix-fd-client.c +++ b/gio/tests/gdbus-example-unix-fd-client.c @@ -79,7 +79,7 @@ on_name_appeared (GDBusConnection *connection, gssize len; gchar *str; - now = time (NULL); + now = g_get_real_time (); strftime (now_buf, sizeof now_buf, "%Y-%m-%d %H:%M:%S", -- GitLab From 2d9047de1ccc8ed6e5d3dec30eca0b86c969c80a Mon Sep 17 00:00:00 2001 From: Niels De Graef Date: Tue, 26 Jun 2018 23:23:40 +0200 Subject: [PATCH 2/2] Forgot to adjust declaration of `now` variable. --- gio/gdbusauthmechanismsha1.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gio/gdbusauthmechanismsha1.c b/gio/gdbusauthmechanismsha1.c index d47221f5b2..0f9e45c4d0 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; -- GitLab