summaryrefslogtreecommitdiff
path: root/src/libical/icalerror.c
blob: d8a85b4c5cce9b5969ded8ebc2f94a40eb71f103 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*======================================================================
 FILE: icalerror.c
 CREATOR: eric 16 May 1999

 SPDX-FileCopyrightText: 2000, Eric Busboom <eric@civicknowledge.com>

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

  The original code is icalerror.c
======================================================================*/

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

#include "icalerror.h"
#include "icalmemory.h"

#include <stdlib.h>

#if defined(HAVE_BACKTRACE)
#include <execinfo.h>
#endif

#if ICAL_SYNC_MODE == ICAL_SYNC_MODE_PTHREAD
#include <pthread.h>

static pthread_key_t icalerrno_key;
static pthread_once_t icalerrno_key_once = PTHREAD_ONCE_INIT;

static void icalerrno_destroy(void *buf)
{
    icalmemory_free_buffer(buf);
    pthread_setspecific(icalerrno_key, NULL);
}

static void icalerrno_key_alloc(void)
{
    pthread_key_create(&icalerrno_key, icalerrno_destroy);
}

icalerrorenum *icalerrno_return(void)
{
    icalerrorenum *_errno;

    pthread_once(&icalerrno_key_once, icalerrno_key_alloc);

    _errno = (icalerrorenum *) pthread_getspecific(icalerrno_key);

    if (!_errno) {
        _errno = icalmemory_new_buffer(sizeof(icalerrorenum));
        *_errno = ICAL_NO_ERROR;
        pthread_setspecific(icalerrno_key, _errno);
    }
    return _errno;
}

#else

static ICAL_GLOBAL_VAR icalerrorenum icalerrno_storage = ICAL_NO_ERROR;

icalerrorenum *icalerrno_return(void)
{
    return &icalerrno_storage;
}

#endif

static ICAL_GLOBAL_VAR int foo;

void icalerror_stop_here(void)
{
    foo++;      /* Keep optimizers from removing routine */
}

void icalerror_crash_here(void)
{
#if !defined(__clang_analyzer__)
    int *p = 0;
    /* coverity[var_deref_op] */
    /* cppcheck-suppress nullPointer */
    *p = 1;

    /* cppcheck-suppress nullPointer */
    icalassert(*p);
#endif
}

void icalerror_clear_errno(void)
{
    icalerrno = ICAL_NO_ERROR;
}

#if ICAL_ERRORS_ARE_FATAL == 1
static ICAL_GLOBAL_VAR int icalerror_errors_are_fatal = 1;
#else
static ICAL_GLOBAL_VAR int icalerror_errors_are_fatal = 0;
#endif

void icalerror_set_errors_are_fatal(int fatal)
{
    icalerror_errors_are_fatal = (fatal != 0);
}

int icalerror_get_errors_are_fatal(void)
{
    return icalerror_errors_are_fatal;
}

#if defined(ICAL_SETERROR_ISFUNC)
void icalerror_set_errno(icalerrorenum x)
{
    icalerrno = x;
    if (icalerror_get_error_state(x) == ICAL_ERROR_FATAL ||
        (icalerror_get_error_state(x) == ICAL_ERROR_DEFAULT && icalerror_errors_are_fatal == 1)) {
        icalerror_warn(icalerror_strerror(x));
        ical_bt();
        icalassert(0);
    }
}

#endif

struct icalerror_state
{
    icalerrorenum error;
    icalerrorstate state;
};

static ICAL_GLOBAL_VAR struct icalerror_state error_state_map[] = {
    {ICAL_BADARG_ERROR, ICAL_ERROR_DEFAULT},
    {ICAL_NEWFAILED_ERROR, ICAL_ERROR_DEFAULT},
    {ICAL_ALLOCATION_ERROR, ICAL_ERROR_DEFAULT},
    {ICAL_MALFORMEDDATA_ERROR, ICAL_ERROR_DEFAULT},
    {ICAL_PARSE_ERROR, ICAL_ERROR_DEFAULT},
    {ICAL_INTERNAL_ERROR, ICAL_ERROR_DEFAULT},
    {ICAL_FILE_ERROR, ICAL_ERROR_DEFAULT},
    {ICAL_USAGE_ERROR, ICAL_ERROR_DEFAULT},
    {ICAL_UNIMPLEMENTED_ERROR, ICAL_ERROR_DEFAULT},
    {ICAL_UNKNOWN_ERROR, ICAL_ERROR_DEFAULT},
    {ICAL_NO_ERROR, ICAL_ERROR_DEFAULT}

};

struct icalerror_string_map
{
    const char *str;
    icalerrorenum error;
    char name[160];
};

static const struct icalerror_string_map string_map[] = {
    {"BADARG", ICAL_BADARG_ERROR, "BADARG: Bad argument to function"},
    {"NEWFAILED", ICAL_NEWFAILED_ERROR,
     "NEWFAILED: Failed to create a new object via a *_new() routine"},
    {"ALLOCATION", ICAL_ALLOCATION_ERROR, "ALLOCATION: Failed to allocate new memory"},
    {"MALFORMEDDATA", ICAL_MALFORMEDDATA_ERROR,
     "MALFORMEDDATA: An input string was not correctly formed or a component has "
     "missing or extra properties"},
    {"PARSE", ICAL_PARSE_ERROR, "PARSE: Failed to parse a part of an iCal component"},
    {"INTERNAL", ICAL_INTERNAL_ERROR,
     "INTERNAL: Random internal error. This indicates an error in the library code, "
     "not an error in use"},
    {"FILE", ICAL_FILE_ERROR,
     "FILE: An operation on a file failed. Check errno for more detail."},
    {"USAGE", ICAL_USAGE_ERROR,
     "USAGE: Failed to propertyl sequence calls to a set of interfaces"},
    {"UNIMPLEMENTED", ICAL_UNIMPLEMENTED_ERROR,
     "UNIMPLEMENTED: This feature has not been implemented"},
    {"NO", ICAL_NO_ERROR, "NO: No error"},
    {"UNKNOWN", ICAL_UNKNOWN_ERROR,
     "UNKNOWN: Unknown error type -- icalerror_strerror() was probably given bad input"}
};

icalerrorenum icalerror_error_from_string(const char *str)
{
    int i;

    for (i = 0; string_map[i].error != ICAL_UNKNOWN_ERROR; i++) {
        if (strcmp(string_map[i].str, str) == 0) {
            break;
        }
    }

    return string_map[i].error;
}

icalerrorstate icalerror_supress(const char *error)
{
    icalerrorenum e = icalerror_error_from_string(error);
    icalerrorstate es;

    if (e == ICAL_NO_ERROR) {
        return ICAL_ERROR_UNKNOWN;
    }

    es = icalerror_get_error_state(e);
    icalerror_set_error_state(e, ICAL_ERROR_NONFATAL);

    return es;
}

const char *icalerror_perror(void)
{
    return icalerror_strerror(icalerrno);
}

void icalerror_restore(const char *error, icalerrorstate es)
{
    icalerrorenum e = icalerror_error_from_string(error);

    if (e != ICAL_NO_ERROR) {
        icalerror_set_error_state(e, es);
    }
}

void icalerror_set_error_state(icalerrorenum error, icalerrorstate state)
{
    int i;

    for (i = 0; error_state_map[i].error != ICAL_NO_ERROR; i++) {
        if (error_state_map[i].error == error) {
            error_state_map[i].state = state;
        }
    }
}

icalerrorstate icalerror_get_error_state(icalerrorenum error)
{
    int i;

    for (i = 0; error_state_map[i].error != ICAL_NO_ERROR; i++) {
        if (error_state_map[i].error == error) {
            return error_state_map[i].state;
        }
    }

    return ICAL_ERROR_UNKNOWN;
}

const char *icalerror_strerror(icalerrorenum e)
{
    int i;

    for (i = 0; string_map[i].error != ICAL_UNKNOWN_ERROR; i++) {
        if (string_map[i].error == e) {
            return string_map[i].name;
        }
    }

    return string_map[i].name;  /* Return string for ICAL_UNKNOWN_ERROR */
}

void ical_bt(void)
{
#if defined(HAVE_BACKTRACE)
    void *stack_frames[50];
    int i, num;
    size_t size;
    char **strings;

    size = sizeof(stack_frames) / sizeof(stack_frames[0]);
    num = backtrace(stack_frames, (int)size);
    strings = backtrace_symbols(stack_frames, num);
    for (i = 0; i < num; i++) {
        if (strings != NULL) {
            icalerrprintf("%s\n", strings[i]);
        } else {
            icalerrprintf("%p\n", stack_frames[i]);
        }
    }
    free(strings); /* Not icalmemory_free_buffer(), allocated by backtrace_symbols() */
#endif
}