sms: prevent crash if date is out of range
g_date_time_new, and g_date_time_new_utc return NULL if inputs are out of range, and currently mm_new_iso8601_time passes the GDateTime created by those two functions to date_time_format_iso8601 without checking for NULL values, causing a g_date_time_format_iso8601 crash if PDU data is corrupted with wrong date. To prevent this, mm_new_iso8601_time now can return NULL and set a new GError if GDateTime created by g_date_time_new is NULL. Fixes #546
This commit is contained in:
@@ -1769,7 +1769,8 @@ mm_new_iso8601_time (guint year,
|
||||
guint minute,
|
||||
guint second,
|
||||
gboolean have_offset,
|
||||
gint offset_minutes)
|
||||
gint offset_minutes,
|
||||
GError **error)
|
||||
{
|
||||
g_autoptr(GDateTime) dt = NULL;
|
||||
|
||||
@@ -1781,6 +1782,14 @@ mm_new_iso8601_time (guint year,
|
||||
} else
|
||||
dt = g_date_time_new_utc (year, month, day, hour, minute, second);
|
||||
|
||||
if (dt == NULL) {
|
||||
g_set_error (error,
|
||||
MM_CORE_ERROR,
|
||||
MM_CORE_ERROR_INVALID_ARGS,
|
||||
"Invalid input for date: got year:%u, month:%u, day:%u, hour:%u, minute:%u, second:%u",
|
||||
year, month, day, hour, minute, second);
|
||||
return NULL;
|
||||
}
|
||||
return date_time_format_iso8601 (dt);
|
||||
}
|
||||
|
||||
|
@@ -216,7 +216,8 @@ gchar *mm_new_iso8601_time (guint year,
|
||||
guint minute,
|
||||
guint second,
|
||||
gboolean have_offset,
|
||||
gint offset_minutes);
|
||||
gint offset_minutes,
|
||||
GError **error);
|
||||
|
||||
/******************************************************************************/
|
||||
/* Type checkers and conversion utilities */
|
||||
|
@@ -602,18 +602,40 @@ static void
|
||||
date_time_iso8601 (void)
|
||||
{
|
||||
gchar *date = NULL;
|
||||
GError *error = NULL;
|
||||
|
||||
date = mm_new_iso8601_time_from_unix_time (1634307342);
|
||||
g_assert_cmpstr (date, ==, "2021-10-15T14:15:42Z");
|
||||
g_free (date);
|
||||
|
||||
date = mm_new_iso8601_time (2021, 10, 15, 16, 15, 42, FALSE, 0);
|
||||
date = mm_new_iso8601_time (2021, 10, 15, 16, 15, 42, FALSE, 0, &error);
|
||||
g_assert_no_error (error);
|
||||
g_assert_cmpstr (date, ==, "2021-10-15T16:15:42Z");
|
||||
g_free (date);
|
||||
|
||||
date = mm_new_iso8601_time (2021, 10, 15, 16, 15, 42, TRUE, 120);
|
||||
date = mm_new_iso8601_time (2021, 10, 15, 16, 15, 42, TRUE, 120, &error);
|
||||
g_assert_no_error (error);
|
||||
g_assert_cmpstr (date, ==, "2021-10-15T16:15:42+02");
|
||||
g_free (date);
|
||||
|
||||
/* Valid args:
|
||||
* - Year:[1-9999]
|
||||
* - Month:[1-12]
|
||||
* - Day:[1-28|29|30|31] according to year and month
|
||||
* - Hour: [0-23]
|
||||
* - Minute: [0-59]
|
||||
* - Seconds: [0.0-60.0)
|
||||
* */
|
||||
date = mm_new_iso8601_time (2021, 13, 15, 16, 15, 42, TRUE, 120, &error);
|
||||
g_assert_error (error, MM_CORE_ERROR, MM_CORE_ERROR_INVALID_ARGS);
|
||||
g_assert_null (date);
|
||||
g_clear_error (&error);
|
||||
|
||||
/* No February 29 in 2021 */
|
||||
date = mm_new_iso8601_time (2021, 2, 29, 16, 15, 42, TRUE, 120, &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