]> git.sesse.net Git - vlc/blob - lib/error.c
libvlc: fix possible free() of static const char[]
[vlc] / lib / error.c
1 /*****************************************************************************
2  * error.c: Error handling for libvlc
3  *****************************************************************************
4  * Copyright (C) 2009 RĂ©mi Denis-Courmont
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2.1 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19  *****************************************************************************/
20
21 #include "libvlc_internal.h"
22
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <assert.h>
26 #include <vlc/libvlc.h>
27
28
29 static const char oom[] = "Out of memory";
30 /* TODO: use only one thread-specific key for whole libvlc */
31 static vlc_threadvar_t context;
32
33 static vlc_mutex_t lock = VLC_STATIC_MUTEX;
34 static uintptr_t refs = 0;
35
36 static void free_msg (void *msg)
37 {
38     if (msg != oom)
39         free (msg);
40 }
41
42 void libvlc_threads_init (void)
43 {
44     vlc_mutex_lock (&lock);
45     if (refs++ == 0)
46         vlc_threadvar_create (&context, free_msg);
47     vlc_mutex_unlock (&lock);
48 }
49
50 void libvlc_threads_deinit (void)
51 {
52     vlc_mutex_lock (&lock);
53     assert (refs > 0);
54     if (--refs == 0)
55         vlc_threadvar_delete (&context);
56     vlc_mutex_unlock (&lock);
57 }
58
59 static char *get_error (void)
60 {
61     return vlc_threadvar_get (context);
62 }
63
64 static void free_error (void)
65 {
66     free_msg (get_error ());
67 }
68
69 /**
70  * Gets a human-readable error message for the last LibVLC error in the calling
71  * thread. The resulting string is valid until another error occurs (at least
72  * until the next LibVLC call).
73  *
74  * @return NULL if there was no error, a nul-terminated string otherwise.
75  */
76 const char *libvlc_errmsg (void)
77 {
78     return get_error ();
79 }
80
81 /**
82  * Clears the LibVLC error status for the current thread. This is optional.
83  * By default, the error status is automatically overridden when a new error
84  * occurs, and destroyed when the thread exits.
85  */
86 void libvlc_clearerr (void)
87 {
88     free_error ();
89     vlc_threadvar_set (context, NULL);
90 }
91
92 /**
93  * Sets the LibVLC error status and message for the current thread.
94  * Any previous error is overridden.
95  * @return a nul terminated string (always)
96  */
97 const char *libvlc_vprinterr (const char *fmt, va_list ap)
98 {
99     char *msg;
100
101     assert (fmt != NULL);
102     if (vasprintf (&msg, fmt, ap) == -1)
103         msg = (char *)oom;
104
105     free_error ();
106     vlc_threadvar_set (context, msg);
107     return msg;
108 }
109
110 /**
111  * Sets the LibVLC error status and message for the current thread.
112  * Any previous error is overridden.
113  * @return a nul terminated string (always)
114  */
115 const char *libvlc_printerr (const char *fmt, ...)
116 {
117     va_list ap;
118     const char *msg;
119
120     va_start (ap, fmt);
121     msg = libvlc_vprinterr (fmt, ap);
122     va_end (ap);
123     return msg;
124 }
125