summaryrefslogtreecommitdiff
path: root/src/test/icalattach-leak.c
blob: 3740742c7e017f97c2d2d7e7df07652f80f5d3da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*======================================================================
 FILE: icalattach-leak.c

 SPDX-FileCopyrightText: 2019 Red Hat, Inc. <www.redhat.com>

 SPDX-License-Identifier: LGPL-2.1-only OR MPL-2.0

 The Initial Developer of the Original Code is Milan Crha
======================================================================*/

/*
 * Program for testing ICalAttach memory handling.
 */

#include <stdio.h>

#include "libical-glib/libical-glib.h"

static GSList *get_attachments(ICalComponent *comp)
{
    ICalProperty *prop;
    GSList *attaches = NULL;

    for (prop = i_cal_component_get_first_property(comp, I_CAL_ATTACH_PROPERTY);
         prop;
         g_object_unref(prop),
         prop = i_cal_component_get_next_property(comp, I_CAL_ATTACH_PROPERTY)) {
        attaches = g_slist_prepend(attaches, i_cal_property_get_attach(prop));
    }

    return attaches;
}

static void remove_all_attachments(ICalComponent *comp)
{
    GSList *to_remove = NULL, *link;
    ICalProperty *prop;

    for (prop = i_cal_component_get_first_property(comp, I_CAL_ATTACH_PROPERTY);
         prop;
         g_object_unref(prop),
         prop = i_cal_component_get_next_property(comp, I_CAL_ATTACH_PROPERTY)) {
        to_remove = g_slist_prepend(to_remove, g_object_ref(prop));
    }

    for (link = to_remove; link; link = g_slist_next(link)) {
        prop = link->data;

        i_cal_component_remove_property(comp, prop);
    }

    g_slist_free_full(to_remove, g_object_unref);
}

static void set_attachments(ICalComponent *comp, GSList *attaches)
{
    GSList *link;

    remove_all_attachments (comp);

    for (link = attaches; link; link = g_slist_next (link)) {
        ICalAttach *attach = link->data;

        i_cal_component_take_property(comp, i_cal_property_new_attach (attach));
    }
}

int main (void)
{
    ICalComponent *comp;
    GSList *attaches;

    comp = i_cal_component_new_from_string(
        "BEGIN:VEVENT\r\n"
        "UID:123\r\n"
        "ATTACH:file:///tmp/f1.txt\r\n"
        "ATTACH:file:///tmp/f2.txt\r\n"
        "END:VEVENT\r\n"
    );

    attaches = get_attachments(comp);
    printf("%s: 1st: has %d attachments\n", __FUNCTION__, g_slist_length(attaches));
    set_attachments(comp, attaches);
    g_slist_free_full(attaches, g_object_unref);

    attaches = get_attachments(comp);
    printf("%s: 2nd: has %d attachments\n", __FUNCTION__, g_slist_length(attaches));
    g_slist_free_full(attaches, g_object_unref);

    g_object_unref(comp);

    return 0;
}