gstdatetime: Add missing NULL check to gst_date_time_new_local_time
authorOndřej Hruška <ondra@ondrovo.com>
Sun, 22 Mar 2020 08:47:35 +0000 (09:47 +0100)
committerGStreamer Merge Bot <gitlab-merge-bot@gstreamer-foundation.org>
Sun, 22 Mar 2020 14:00:41 +0000 (14:00 +0000)
Also add a unit test for this.

Fixes #524

gst/gstdatetime.c
tests/check/gst/gstdatetime.c

index 8d363de2acef20ffdf953cd8ab37bb31c0d8f4e3..8bf1d5d4b5afb092f15caf010b76871b87acd006 100644 (file)
@@ -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;
 
index bd9fa896b12daeec73087c875bb5568a23d6cf1b..2d4502ae622f9155eeceb386b702ba2a3c0d6507 100644 (file)
@@ -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;
 }