From: Ondřej Hruška Date: Sun, 22 Mar 2020 08:47:35 +0000 (+0100) Subject: gstdatetime: Add missing NULL check to gst_date_time_new_local_time X-Git-Tag: 1.19.3~963 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=99f72263441e9442a201449a2753c987ca44544c;p=platform%2Fupstream%2Fgstreamer.git gstdatetime: Add missing NULL check to gst_date_time_new_local_time Also add a unit test for this. Fixes #524 --- diff --git a/gst/gstdatetime.c b/gst/gstdatetime.c index 8d363de2ac..8bf1d5d4b5 100644 --- a/gst/gstdatetime.c +++ b/gst/gstdatetime.c @@ -568,6 +568,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; } @@ -692,6 +695,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; diff --git a/tests/check/gst/gstdatetime.c b/tests/check/gst/gstdatetime.c index bd9fa896b1..2d4502ae62 100644 --- a/tests/check/gst/gstdatetime.c +++ b/tests/check/gst/gstdatetime.c @@ -223,6 +223,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) { gint64 now_us; @@ -833,6 +955,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; }