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 | /*-*- Mode: C; c-basic-offset: 8 -*-*/
/***
This file is part of libkanberra.
Copyright 2008 Lennart Poettering
libkanberra 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) any later version.
libkanberra 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 libkanberra. If not, see
<http://www.gnu.org/licenses/>.
***/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string.h>
#include <locale.h>
#include <ctk/ctk.h>
#include <kanberra-ctk.h>
static int ret = 0;
static ka_proplist *proplist = NULL;
static int n_loops = 1;
static void callback(ka_context *c, uint32_t id, int error, void *userdata);
static gboolean idle_quit(gpointer userdata) {
ctk_main_quit();
return FALSE;
}
static gboolean idle_play(gpointer userdata) {
int r;
g_assert(n_loops > 1);
n_loops--;
r = ka_context_play_full(ka_ctk_context_get(), 1, proplist, callback, NULL);
if (r < 0) {
g_printerr("Failed to play sound: %s\n", ka_strerror(r));
ret = 1;
ctk_main_quit();
}
return FALSE;
}
static void callback(ka_context *c, uint32_t id, int error, void *userdata) {
if (error < 0) {
g_printerr("Failed to play sound (callback): %s\n", ka_strerror(error));
ret = 1;
} else if (n_loops > 1) {
/* So, why don't we call ka_context_play_full() here directly?
-- Because the context this callback is called from is
explicitly documented as undefined and no libkanberra function
may be called from it. */
g_idle_add(idle_play, NULL);
return;
}
/* So, why don't we call ctk_main_quit() here directly? -- Because
* otherwise we might end up with a small race condition: this
* callback might get called before the main loop actually started
* running */
g_idle_add(idle_quit, NULL);
}
static GQuark error_domain(void) {
return g_quark_from_static_string("kanberra-error-quark");
}
static gboolean property_callback(
const gchar *option_name,
const gchar *value,
gpointer data,
GError **error) {
const char *equal;
char *t;
if (!(equal = strchr(value, '='))) {
g_set_error(error, error_domain(), 0, "Property lacks '='.");
return FALSE;
}
t = g_strndup(value, equal - value);
if (ka_proplist_sets(proplist, t, equal + 1) < 0) {
g_set_error(error, error_domain(), 0, "Invalid property.");
g_free(t);
return FALSE;
}
g_free(t);
return TRUE;
}
int main (int argc, char *argv[]) {
GOptionContext *oc;
static gchar *event_id = NULL, *filename = NULL, *event_description = NULL, *cache_control = NULL, *volume = NULL;
int r;
static gboolean version = FALSE;
GError *error = NULL;
static const GOptionEntry options[] = {
{ "version", 'v', 0, G_OPTION_ARG_NONE, &version, "Display version number and quit", NULL },
{ "id", 'i', 0, G_OPTION_ARG_STRING, &event_id, "Event sound identifier", "STRING" },
{ "file", 'f', 0, G_OPTION_ARG_STRING, &filename, "Play file", "PATH" },
{ "description", 'd', 0, G_OPTION_ARG_STRING, &event_description, "Event sound description", "STRING" },
{ "cache-control", 'c', 0, G_OPTION_ARG_STRING, &cache_control, "Cache control (permanent, volatile, never)", "STRING" },
{ "loop", 'l', 0, G_OPTION_ARG_INT, &n_loops, "Loop how many times (detault: 1)", "INTEGER" },
{ "volume", 'V', 0, G_OPTION_ARG_STRING, &volume, "A floating point dB value for the sample volume (ex: 0.0)", "STRING" },
{ "property", 0, 0, G_OPTION_ARG_CALLBACK, (void*) property_callback, "An arbitrary property", "STRING" },
{ NULL, 0, 0, 0, NULL, NULL, NULL }
};
setlocale(LC_ALL, "");
ka_proplist_create(&proplist);
oc = g_option_context_new("- kanberra-ctk-play");
g_option_context_add_main_entries(oc, options, NULL);
g_option_context_add_group(oc, ctk_get_option_group(TRUE));
g_option_context_set_help_enabled(oc, TRUE);
if (!(g_option_context_parse(oc, &argc, &argv, &error))) {
g_print("Option parsing failed: %s\n", error->message);
return 1;
}
g_option_context_free(oc);
if (version) {
g_print("kanberra-ctk-play from %s\n", PACKAGE_STRING);
return 0;
}
if (!event_id && !filename) {
g_printerr("No event id or file specified.\n");
return 1;
}
ka_context_change_props(ka_ctk_context_get(),
KA_PROP_APPLICATION_NAME, "kanberra-ctk-play",
KA_PROP_APPLICATION_VERSION, PACKAGE_VERSION,
KA_PROP_APPLICATION_ID, "org.freedesktop.libkanberra.ctk-play",<--- Passing NULL after the last typed argument to a variadic function leads to undefined behaviour. [+]Passing NULL after the last typed argument to a variadic function leads to undefined behaviour.
The C99 standard, in section 7.15.1.1, states that if the type used by va_arg() is not compatible with the type of the actual next argument (as promoted according to the default argument promotions), the behavior is undefined.
The value of the NULL macro is an implementation-defined null pointer constant (7.17), which can be any integer constant expression with the value 0, or such an expression casted to (void*) (6.3.2.3). This includes values like 0, 0L, or even 0LL.
In practice on common architectures, this will cause real crashes if sizeof(int) != sizeof(void*), and NULL is defined to 0 or any other null pointer constant that promotes to int.
To reproduce you might be able to use this little code example on 64bit platforms. If the output includes "ERROR", the sentinel had only 4 out of 8 bytes initialized to zero and was not detected as the final argument to stop argument processing via va_arg(). Changing the 0 to (void*)0 or 0L will make the "ERROR" output go away.
#include <stdarg.h>
#include <stdio.h>
void f(char *s, ...) {
va_list ap;
va_start(ap,s);
for (;;) {
char *p = va_arg(ap,char*);
printf("%018p, %s\n", p, (long)p & 255 ? p : "");
if(!p) break;
}
va_end(ap);
}
void g() {
char *s2 = "x";
char *s3 = "ERROR";
// changing 0 to 0L for the 7th argument (which is intended to act as sentinel) makes the error go away on x86_64
f("first", s2, s2, s2, s2, s2, 0, s3, (char*)0);
}
void h() {
int i;
volatile unsigned char a[1000];
for (i = 0; i<sizeof(a); i++)
a[i] = -1;
}
int main() {
h();
g();
return 0;
}
NULL);
if (event_id)
ka_proplist_sets(proplist, KA_PROP_EVENT_ID, event_id);
if (filename)
ka_proplist_sets(proplist, KA_PROP_MEDIA_FILENAME, filename);
if (cache_control)
ka_proplist_sets(proplist, KA_PROP_KANBERRA_CACHE_CONTROL, cache_control);
if (event_description)
ka_proplist_sets(proplist, KA_PROP_EVENT_DESCRIPTION, event_description);
if (volume)
ka_proplist_sets(proplist, KA_PROP_KANBERRA_VOLUME, volume);
r = ka_context_play_full(ka_ctk_context_get(), 1, proplist, callback, NULL);
if (r < 0) {
g_printerr("Failed to play sound: %s\n", ka_strerror(r));
ret = 1;
goto finish;
}
ctk_main();
finish:
ka_proplist_destroy(proplist);
return ret;
}
|