[evolution-data-server] ECalComponent: Add safety checks on get/set of DTEND/DUE properties



commit 1a125551b41d0aa02b0270bb8a556b1a5a09dadb
Author: Milan Crha <mcrha redhat com>
Date:   Wed Jan 5 13:45:37 2022 +0100

    ECalComponent: Add safety checks on get/set of DTEND/DUE properties
    
    These are available only on certain component types, thus test for it.
    Let the getters return NULL silently, but warn when the setter is used
    with an invalid component type (though still allow to do it).

 src/calendar/libecal/e-cal-component.c | 26 ++++++++++++++++++++++----
 tests/libecal/test-cal-component.c     |  2 +-
 2 files changed, 23 insertions(+), 5 deletions(-)
---
diff --git a/src/calendar/libecal/e-cal-component.c b/src/calendar/libecal/e-cal-component.c
index c7629813a..ace7033da 100644
--- a/src/calendar/libecal/e-cal-component.c
+++ b/src/calendar/libecal/e-cal-component.c
@@ -1711,10 +1711,18 @@ static ECalComponentDateTime *
 e_cal_component_get_start_plus_duration (ECalComponent *comp)
 {
        ICalDuration *duration;
+       ICalProperty *prop;
        ICalTime *tt;
        ECalComponentDateTime *dt;
        guint dur_days, dur_hours, dur_minutes, dur_seconds;
 
+       /* libical can calculate it from DTSTART/DTEND, which is not needed here */
+       prop = i_cal_component_get_first_property (comp->priv->icalcomp, I_CAL_DURATION_PROPERTY);
+       if (!prop)
+               return NULL;
+
+       g_clear_object (&prop);
+
        duration = i_cal_component_get_duration (comp->priv->icalcomp);
        if (!duration || i_cal_duration_is_null_duration (duration) || i_cal_duration_is_bad_duration 
(duration)) {
                g_clear_object (&duration);
@@ -1778,10 +1786,13 @@ e_cal_component_get_dtend (ECalComponent *comp)
        g_return_val_if_fail (E_IS_CAL_COMPONENT (comp), NULL);
        g_return_val_if_fail (comp->priv->icalcomp != NULL, NULL);
 
+       if (e_cal_component_get_vtype (comp) != E_CAL_COMPONENT_EVENT &&
+           e_cal_component_get_vtype (comp) != E_CAL_COMPONENT_FREEBUSY)
+               return NULL;
+
        dt = get_datetime (comp->priv->icalcomp, I_CAL_DTEND_PROPERTY, i_cal_property_get_dtend, NULL);
 
-       /* If we don't have a DTEND property, then we try to get DTSTART
-        * + DURATION. */
+       /* If we don't have a DTEND property, then we try to get DTSTART + DURATION. */
        if (!dt)
                dt = e_cal_component_get_start_plus_duration (comp);
 
@@ -1804,6 +1815,9 @@ e_cal_component_set_dtend (ECalComponent *comp,
        g_return_if_fail (E_IS_CAL_COMPONENT (comp));
        g_return_if_fail (comp->priv->icalcomp != NULL);
 
+       g_warn_if_fail (e_cal_component_get_vtype (comp) == E_CAL_COMPONENT_EVENT ||
+                       e_cal_component_get_vtype (comp) == E_CAL_COMPONENT_FREEBUSY);
+
        set_datetime (comp->priv->icalcomp, I_CAL_DTEND_PROPERTY,
                i_cal_property_new_dtend,
                i_cal_property_set_dtend,
@@ -1951,10 +1965,12 @@ e_cal_component_get_due (ECalComponent *comp)
        g_return_val_if_fail (E_IS_CAL_COMPONENT (comp), NULL);
        g_return_val_if_fail (comp->priv->icalcomp != NULL, NULL);
 
+       if (e_cal_component_get_vtype (comp) != E_CAL_COMPONENT_TODO)
+               return NULL;
+
        dt = get_datetime (comp->priv->icalcomp, I_CAL_DUE_PROPERTY, i_cal_property_get_due, NULL);
 
-       /* If we don't have a DTEND property, then we try to get DTSTART
-        * + DURATION. */
+       /* If we don't have a DUE property, then we try to get DTSTART + DURATION. */
        if (!dt)
                dt = e_cal_component_get_start_plus_duration (comp);
 
@@ -1979,6 +1995,8 @@ e_cal_component_set_due (ECalComponent *comp,
        g_return_if_fail (E_IS_CAL_COMPONENT (comp));
        g_return_if_fail (comp->priv->icalcomp != NULL);
 
+       g_warn_if_fail (e_cal_component_get_vtype (comp) == E_CAL_COMPONENT_TODO);
+
        set_datetime (comp->priv->icalcomp, I_CAL_DUE_PROPERTY,
                i_cal_property_new_due,
                i_cal_property_set_due,
diff --git a/tests/libecal/test-cal-component.c b/tests/libecal/test-cal-component.c
index 959a9dc0a..2b8146662 100644
--- a/tests/libecal/test-cal-component.c
+++ b/tests/libecal/test-cal-component.c
@@ -2548,7 +2548,7 @@ test_component_datetime (void (* set_func) (ECalComponent *comp,
        g_assert_true (set_func != NULL);
        g_assert_true (verify_func != NULL);
 
-       comp = e_cal_component_new_vtype (E_CAL_COMPONENT_EVENT);
+       comp = e_cal_component_new_vtype (set_func == e_cal_component_set_due ? E_CAL_COMPONENT_TODO : 
E_CAL_COMPONENT_EVENT);
        g_assert_nonnull (comp);
 
        for (ii = 0; ii < G_N_ELEMENTS (values); ii++) {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]