libmm-glib,common-helpers: don't assume new_from_unix_utc() always succeeds
The g_date_time_new_from_unix_utc() method in glib2 may fail if the given timestamp is too far into the future. The value is supposed to be seconds since unix time origin, but internally it will be converted into usecs, so any value longer than G_MAXINT64 / USEC_PER_SECOND isn't allowed. This is currently used in the CLI, and we're anyway ignoring the error returned in that case, but at least it won't crash if it ever happens.
This commit is contained in:
@@ -621,11 +621,11 @@ mmcli_output_signal_quality (MMModemState state,
|
||||
/* (Custom) Bearer start date output */
|
||||
|
||||
void
|
||||
mmcli_output_start_date (guint64 value)
|
||||
mmcli_output_start_date (guint64 value)
|
||||
{
|
||||
/* Merge value and recent flag in a single item in human output */
|
||||
if (selected_type == MMC_OUTPUT_TYPE_HUMAN) {
|
||||
output_item_new_take_single (MMC_F_BEARER_STATS_START_DATE, mm_new_iso8601_time_from_unix_time (value));
|
||||
output_item_new_take_single (MMC_F_BEARER_STATS_START_DATE, mm_new_iso8601_time_from_unix_time (value, NULL));
|
||||
return;
|
||||
}
|
||||
|
||||
|
@@ -1752,11 +1752,18 @@ date_time_format_iso8601 (GDateTime *dt)
|
||||
}
|
||||
|
||||
gchar *
|
||||
mm_new_iso8601_time_from_unix_time (guint64 timestamp)
|
||||
mm_new_iso8601_time_from_unix_time (guint64 timestamp,
|
||||
GError **error)
|
||||
{
|
||||
g_autoptr(GDateTime) dt = NULL;
|
||||
|
||||
dt = g_date_time_new_from_unix_utc ((gint64)timestamp);
|
||||
if (!dt) {
|
||||
g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_INVALID_ARGS,
|
||||
"Invalid unix time: %" G_GUINT64_FORMAT,
|
||||
timestamp);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return date_time_format_iso8601 (dt);
|
||||
}
|
||||
|
@@ -208,16 +208,17 @@ gboolean mm_get_double_from_match_info (GMatchInfo *match_info,
|
||||
gchar *mm_get_string_unquoted_from_match_info (GMatchInfo *match_info,
|
||||
guint32 match_index);
|
||||
|
||||
gchar *mm_new_iso8601_time_from_unix_time (guint64 timestamp);
|
||||
gchar *mm_new_iso8601_time (guint year,
|
||||
guint month,
|
||||
guint day,
|
||||
guint hour,
|
||||
guint minute,
|
||||
guint second,
|
||||
gboolean have_offset,
|
||||
gint offset_minutes,
|
||||
GError **error);
|
||||
gchar *mm_new_iso8601_time_from_unix_time (guint64 timestamp,
|
||||
GError **error);
|
||||
gchar *mm_new_iso8601_time (guint year,
|
||||
guint month,
|
||||
guint day,
|
||||
guint hour,
|
||||
guint minute,
|
||||
guint second,
|
||||
gboolean have_offset,
|
||||
gint offset_minutes,
|
||||
GError **error);
|
||||
|
||||
/******************************************************************************/
|
||||
/* Type checkers and conversion utilities */
|
||||
|
@@ -604,7 +604,8 @@ date_time_iso8601 (void)
|
||||
gchar *date = NULL;
|
||||
GError *error = NULL;
|
||||
|
||||
date = mm_new_iso8601_time_from_unix_time (1634307342);
|
||||
date = mm_new_iso8601_time_from_unix_time (1634307342, &error);
|
||||
g_assert_no_error (error);
|
||||
g_assert_cmpstr (date, ==, "2021-10-15T14:15:42Z");
|
||||
g_free (date);
|
||||
|
||||
@@ -636,6 +637,12 @@ date_time_iso8601 (void)
|
||||
g_assert_error (error, MM_CORE_ERROR, MM_CORE_ERROR_INVALID_ARGS);
|
||||
g_assert_null (date);
|
||||
g_clear_error (&error);
|
||||
|
||||
/* Too far into the future */
|
||||
date = mm_new_iso8601_time_from_unix_time (G_MAXINT64, &error);
|
||||
g_assert_error (error, MM_CORE_ERROR, MM_CORE_ERROR_INVALID_ARGS);
|
||||
g_assert_null (date);
|
||||
g_clear_error (&error);
|
||||
}
|
||||
|
||||
/**************************************************************/
|
||||
|
Reference in New Issue
Block a user