From f9a6a9ba53ee4cf93de5ce28b2a9b8345c406f9e Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Tue, 2 May 2017 15:53:13 +0100 Subject: [PATCH] gtimer: Handle gmtime() failure in g_time_val_to_iso8601() g_time_val_to_iso8601() has a limit to the future dates it can convert, imposed by what gmtime() can fit in its year field. If gmtime() fails, gracefully return NULL from g_time_val_to_iso8601() rather than trying to dereference the NULL structure and crashing. Signed-off-by: Philip Withnall https://bugzilla.gnome.org/show_bug.cgi?id=782075 --- glib/gtimer.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/glib/gtimer.c b/glib/gtimer.c index a51b359ca..38262ff9e 100644 --- a/glib/gtimer.c +++ b/glib/gtimer.c @@ -491,7 +491,14 @@ g_time_val_from_iso8601 (const gchar *iso_date, * Use g_date_time_format() or g_strdup_printf() if a different * variation of ISO 8601 format is required. * - * Returns: a newly allocated string containing an ISO 8601 date + * If @time_ represents a date which is too large to fit into a `struct tm`, + * %NULL will be returned. This is platform dependent, but it is safe to assume + * years up to 3000 are supported. The return value of g_time_val_to_iso8601() + * has been nullable since GLib 2.54; before then, GLib would crash under the + * same conditions. + * + * Returns: (nullable): a newly allocated string containing an ISO 8601 date, + * or %NULL if @time_ was too large * * Since: 2.12 */ @@ -518,6 +525,10 @@ g_time_val_to_iso8601 (GTimeVal *time_) #endif #endif + /* If the gmtime() call has failed, time_->tv_sec is too big. */ + if (tm == NULL) + return NULL; + if (time_->tv_usec != 0) { /* ISO 8601 date and time format, with fractionary seconds: