gstdatetime: Add missing NULL check to gst_date_time_new_local_time

Also add a unit test for this.

Fixes #524
This commit is contained in:
Ondřej Hruška 2020-03-22 09:47:35 +01:00 committed by Tim-Philipp Müller
parent 687c0f0ce7
commit 7b0e6f3e62
2 changed files with 129 additions and 0 deletions

View file

@ -520,6 +520,9 @@ gst_date_time_new_local_time (gint year, gint month, gint day, gint hour,
datetime = gst_date_time_new_from_g_date_time (g_date_time_new_local (year,
month, day, hour, minute, seconds));
if (datetime == NULL)
return NULL;
datetime->fields = fields;
return datetime;
}
@ -644,6 +647,9 @@ gst_date_time_new (gfloat tzoffset, gint year, gint month, gint day, gint hour,
dt = g_date_time_new (tz, year, month, day, hour, minute, seconds);
g_time_zone_unref (tz);
if (!dt)
return NULL; /* date failed validation */
datetime = gst_date_time_new_from_g_date_time (dt);
datetime->fields = fields;

View file

@ -176,6 +176,128 @@ GST_START_TEST (test_GstDateTime_get_hour)
GST_END_TEST;
GST_START_TEST (test_GstDateTime_new_local_time)
{
GstDateTime *dt;
/* Valid date */
dt = gst_date_time_new_local_time (2020, 2, 28, 12, 0, 0);
fail_unless (dt != NULL);
gst_date_time_unref (dt);
/* Date out of bounds - regression test for segfault #524 */
dt = gst_date_time_new_local_time (2020, 2, 31, 12, 0, 0);
fail_unless (dt == NULL);
// TODO more tests for correctness of the function (regarding local timezone)
/* Invalid values */
/* Year */
ASSERT_CRITICAL (dt = gst_date_time_new_local_time (0, 2, 28, 12, 0, 0)); // -1 has special meaning!
fail_unless (dt == NULL);
ASSERT_CRITICAL (dt = gst_date_time_new_local_time (10000, 2, 28, 12, 0, 0));
fail_unless (dt == NULL);
dt = gst_date_time_new_local_time (1, 2, 28, 12, 0, 0);
fail_unless (dt != NULL);
gst_date_time_unref (dt);
dt = gst_date_time_new_local_time (9999, 2, 28, 12, 0, 0);
fail_unless (dt != NULL);
gst_date_time_unref (dt);
/* Month */
ASSERT_CRITICAL (dt = gst_date_time_new_local_time (2020, 0, 28, 12, 0, 0));
fail_unless (dt == NULL);
ASSERT_CRITICAL (dt = gst_date_time_new_local_time (2020, 13, 28, 12, 0, 0));
fail_unless (dt == NULL);
dt = gst_date_time_new_local_time (2020, 1, 28, 12, 0, 0);
fail_unless (dt != NULL);
gst_date_time_unref (dt);
dt = gst_date_time_new_local_time (2020, 12, 28, 12, 0, 0);
fail_unless (dt != NULL);
gst_date_time_unref (dt);
dt = gst_date_time_new_local_time (2038, 6, 15, 12, 0, 0);
fail_unless (dt != NULL);
gst_date_time_unref (dt);
/* Day */
ASSERT_CRITICAL (dt = gst_date_time_new_local_time (2020, 2, 0, 12, 0, 0));
fail_unless (dt == NULL);
ASSERT_CRITICAL (dt = gst_date_time_new_local_time (2020, 2, 32, 12, 0, 0));
fail_unless (dt == NULL);
dt = gst_date_time_new_local_time (2020, 2, 1, 12, 0, 0);
fail_unless (dt != NULL);
gst_date_time_unref (dt);
dt = gst_date_time_new_local_time (2020, 2, 29, 12, 0, 0); // leap year
fail_unless (dt != NULL);
gst_date_time_unref (dt);
dt = gst_date_time_new_local_time (2020, 1, 31, 12, 0, 0);
fail_unless (dt != NULL);
gst_date_time_unref (dt);
/* Hour */
ASSERT_CRITICAL (dt = gst_date_time_new_local_time (2020, 2, 28, -10, 0, 0)); // -1 has special meaning!
fail_unless (dt == NULL);
ASSERT_CRITICAL (dt = gst_date_time_new_local_time (2020, 2, 28, 24, 0, 0));
fail_unless (dt == NULL);
dt = gst_date_time_new_local_time (2020, 2, 28, 0, 0, 0);
fail_unless (dt != NULL);
gst_date_time_unref (dt);
dt = gst_date_time_new_local_time (2020, 2, 28, 23, 0, 0);
fail_unless (dt != NULL);
gst_date_time_unref (dt);
/* Min */
ASSERT_CRITICAL (dt = gst_date_time_new_local_time (2020, 2, 28, 12, -10, 0)); // -1 has special meaning!
fail_unless (dt == NULL);
ASSERT_CRITICAL (dt = gst_date_time_new_local_time (2020, 2, 28, 12, 60, 0));
fail_unless (dt == NULL);
dt = gst_date_time_new_local_time (2020, 2, 28, 12, 0, 0);
fail_unless (dt != NULL);
gst_date_time_unref (dt);
dt = gst_date_time_new_local_time (2020, 2, 28, 12, 59, 0);
fail_unless (dt != NULL);
gst_date_time_unref (dt);
/* Sec */
ASSERT_CRITICAL (dt = gst_date_time_new_local_time (2020, 2, 28, 12, 0, -10)); // -1 has special meaning!
fail_unless (dt == NULL);
ASSERT_CRITICAL (dt = gst_date_time_new_local_time (2020, 2, 28, 12, 0, 60));
fail_unless (dt == NULL);
dt = gst_date_time_new_local_time (2020, 2, 28, 12, 0, 0);
fail_unless (dt != NULL);
gst_date_time_unref (dt);
dt = gst_date_time_new_local_time (2020, 2, 28, 12, 0, 59);
fail_unless (dt != NULL);
gst_date_time_unref (dt);
dt = gst_date_time_new_local_time (2020, 12, 31, 23, 59, 59);
fail_unless (dt != NULL);
gst_date_time_unref (dt);
}
GST_END_TEST;
GST_START_TEST (test_GstDateTime_get_microsecond)
{
GTimeVal tv;
@ -784,6 +906,7 @@ gst_date_time_suite (void)
tcase_add_test (tc_chain, test_GstDateTime_iso8601);
tcase_add_test (tc_chain, test_GstDateTime_to_g_date_time);
tcase_add_test (tc_chain, test_GstDateTime_new_from_g_date_time);
tcase_add_test (tc_chain, test_GstDateTime_new_local_time);
return s;
}