GNOME Bugzilla – Bug 677522
Improved error-handling when timezone lookup fails
Last modified: 2018-05-24 14:16:07 UTC
One of the problems with diagnosing Bug #644473 is that there is no special information produced when g_time_zone_new(identifier) is passed a timezone identifier string that cannot be processed: it just appears to return a valid pointer to a GTimeZone object with a tz offset of 0. That means the caller cannot distinguish "valid tz reference to UTC" from "invalid tz reference", which can then easily lead to later mistakes when handling time in the wrong zone (g_date_time_new, etc.) because the tz lookup did not fail. Common failure modes include Country/City strings that do not have a zoneinfo/ file, zoneinfo file is found but not processable for some reason (the situation in the parent bug), or known-to-be-unhandled valid formats (gtimezone.c:g_time_zone_new has a comment recognizing that these not-yet-implemented cases exist). A simple start (that does not require altering the existing API) would be for g_time_zone_new to return NULL if looking up the identifier fails. An enhanced way would be via GError, perhaps an alternative _new() method that took a &GError parameter. The new-interface alternative might be preferable to prevent breaking existing glib internal code that doesn't check a passed GTimeZone pointer before trying to read it. For example, passing NULL as the first parameter to g_date_time_new() currently causes a bus error.
Since it’s pretty unexpected for a timezone lookup to fail (a lot of your system is going to be broken if you have an invalid timezone specified somewhere), I’m not sure we need to wade in with a GError. The changes I’m proposing in bug #795165 allow errors to be detected by comparing the identifier passed to g_time_zone_new() to the identifier returned by g_time_zone_get_identifier(). On failure, the latter will return `UTC`. This isn’t particularly gainly, and I’m sure that basically everyone who calls g_time_zone_new() will fail to handle errors, but it does allow the error state to be checked. The question is whether we think errors are sufficiently unlikely that this is adequate — or whether we think a new g_time_zone_new_full(const gchar *identifier, GError **error) is still needed.
-- GitLab Migration Automatic Message -- This bug has been migrated to GNOME's GitLab instance and has been closed from further activity. You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.gnome.org/GNOME/glib/issues/553.