summaryrefslogtreecommitdiff
path: root/src/syncevo/icalstrdup.h
blob: 8c5017097bead6e3872c128e646515034b1d0bf2 (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
94
95
96
97
98
99
100
/*
 * Copyright (C) 2008-2009 Patrick Ohly <patrick.ohly@gmx.de>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) version 3.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

#ifndef ICALSTRDUP_H
#define ICALSTRDUP_H

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#ifdef ENABLE_ICAL

#ifndef HANDLE_LIBICAL_MEMORY
# define HANDLE_LIBICAL_MEMORY 1
#endif
#include <libical/ical.h>

#include <syncevo/eds_abi_wrapper.h>

#ifdef __cplusplus
extern "C" {
#pragma }
#endif /* __cplusplus */

#if !defined(LIBICAL_MEMFIXES) || defined(EVOLUTION_COMPATIBILITY)
/**
 * The patch in http://bugzilla.gnome.org/show_bug.cgi?id=516408
 * changes the ownership of strings returned by some libical and libecal
 * functions: previously, the memory was owned by the library.
 * After the patch the caller owns the copied string and must free it.
 *
 * The upstream SourceForge libical has incorporated the patch, but
 * without changing the semantic of the existing calls. Instead they
 * added _r variants which return memory that the caller must free.
 * As soon as Evolution switches to upstream libical (planned for 2.25),
 * it probably will have to bump the libecal version because the API
 * is reverted so that binaries which free strings will crash.
 * When EVOLUTION_COMPATIBILITY is defined, SyncEvolution deals with
 * this by always checking at runtime what the memory handling is.
 *
 * This utility function ensures that the caller *always* owns the
 * returned string. When compiled against a current Evolution
 * Dataserver, the function becomes a NOP macro, unless compatibility
 * mode is on (in which case the current binary might later run with
 * an older Evolution release!). If not a NOP macro, then the function
 * duplicates the string; it handles NULL by passing it through.
 *
 * When compiled against an old Evolution Dataserver, then a runtime
 * check can be enabled to to determine whether the string needs to be
 * duplicated. If uncertain, it always duplicates the string. If the
 * check fails, then memory is leaked, which also happens when running
 * programs which do not know about the patch.
 *
 * To enable the runtime check, compile the .c file with -D_GNU_SOURCE
 * and -DHAVE_DLFCN_H. If HAVE_CONFIG_H is set, then config.h is included
 * and can be set to set some of these defines. If enabled, then link with
 * -ldl.
 *
 * ical_strdup() must be wrapped around the following functions:
 * - icalreqstattype_as_string
 * - icalproperty_as_ical_string
 * - icalproperty_get_parameter_as_string
 * - icalproperty_get_value_as_string
 * - icallangbind_property_eval_string
 * - icalperiodtype_as_ical_string
 * - icaltime_as_ical_string
 * - icalvalue_as_ical_string
 * - icalcomponent_as_ical_string
 * - e_cal_component_get_recurid_as_string
 *
 * @param x    result of one of the functions above
 * @return string which has to be freed by caller, may be NULL if x was NULL
 */
extern char *ical_strdup(const char *x);
#else
# define ical_strdup(_x) (_x)
#endif

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* ENABLE_ICAL */
#endif /* ICALSTRDUP_H */