pactl: Fix crash with older servers

Servers older than 0.9.15 don't know anything about cards, and card
operations will return a NULL pa_operation object when connected to
that old server. We must check the pa_operation pointer before passing
it to pa_operation_unref(), otherwise a NULL operation will result in
a crash.
This commit is contained in:
Tanu Kaskinen 2014-03-19 09:18:06 +02:00
parent e0e6bf6875
commit e1440395d1

View file

@ -93,7 +93,12 @@ static pa_stream *sample_stream = NULL;
static pa_sample_spec sample_spec; static pa_sample_spec sample_spec;
static pa_channel_map channel_map; static pa_channel_map channel_map;
static size_t sample_length = 0; static size_t sample_length = 0;
static int actions = 1;
/* This variable tracks the number of ongoing asynchronous operations. When a
* new operation begins, this is incremented simply with actions++, and when
* an operation finishes, this is decremented with the complete_action()
* function, which shuts down the program if actions reaches zero. */
static int actions = 0;
static bool nl = false; static bool nl = false;
@ -1015,6 +1020,7 @@ static void set_sink_formats(pa_context *c, uint32_t sink, const char *str) {
char *format = NULL; char *format = NULL;
const char *state = NULL; const char *state = NULL;
int i = 0; int i = 0;
pa_operation *o = NULL;
while ((format = pa_split(str, ";", &state))) { while ((format = pa_split(str, ";", &state))) {
pa_format_info *f = pa_format_info_from_string(pa_strip(format)); pa_format_info *f = pa_format_info_from_string(pa_strip(format));
@ -1028,7 +1034,11 @@ static void set_sink_formats(pa_context *c, uint32_t sink, const char *str) {
pa_xfree(format); pa_xfree(format);
} }
pa_operation_unref(pa_ext_device_restore_save_formats(c, PA_DEVICE_TYPE_SINK, sink, i, f_arr, simple_callback, NULL)); o = pa_ext_device_restore_save_formats(c, PA_DEVICE_TYPE_SINK, sink, i, f_arr, simple_callback, NULL);
if (o) {
pa_operation_unref(o);
actions++;
}
done: done:
if (format) if (format)
@ -1154,7 +1164,10 @@ static void context_subscribe_callback(pa_context *c, pa_subscription_event_type
} }
static void context_state_callback(pa_context *c, void *userdata) { static void context_state_callback(pa_context *c, void *userdata) {
pa_operation *o = NULL;
pa_assert(c); pa_assert(c);
switch (pa_context_get_state(c)) { switch (pa_context_get_state(c)) {
case PA_CONTEXT_CONNECTING: case PA_CONTEXT_CONNECTING:
case PA_CONTEXT_AUTHORIZING: case PA_CONTEXT_AUTHORIZING:
@ -1164,21 +1177,26 @@ static void context_state_callback(pa_context *c, void *userdata) {
case PA_CONTEXT_READY: case PA_CONTEXT_READY:
switch (action) { switch (action) {
case STAT: case STAT:
pa_operation_unref(pa_context_stat(c, stat_callback, NULL)); o = pa_context_stat(c, stat_callback, NULL);
if (short_list_format) if (short_list_format)
break; break;
if (o) {
pa_operation_unref(o);
actions++; actions++;
}
/* Fall through */
case INFO: case INFO:
pa_operation_unref(pa_context_get_server_info(c, get_server_info_callback, NULL)); o = pa_context_get_server_info(c, get_server_info_callback, NULL);
break; break;
case PLAY_SAMPLE: case PLAY_SAMPLE:
pa_operation_unref(pa_context_play_sample(c, sample_name, sink_name, PA_VOLUME_NORM, simple_callback, NULL)); o = pa_context_play_sample(c, sample_name, sink_name, PA_VOLUME_NORM, simple_callback, NULL);
break; break;
case REMOVE_SAMPLE: case REMOVE_SAMPLE:
pa_operation_unref(pa_context_remove_sample(c, sample_name, simple_callback, NULL)); o = pa_context_remove_sample(c, sample_name, simple_callback, NULL);
break; break;
case UPLOAD_SAMPLE: case UPLOAD_SAMPLE:
@ -1188,163 +1206,203 @@ static void context_state_callback(pa_context *c, void *userdata) {
pa_stream_set_state_callback(sample_stream, stream_state_callback, NULL); pa_stream_set_state_callback(sample_stream, stream_state_callback, NULL);
pa_stream_set_write_callback(sample_stream, stream_write_callback, NULL); pa_stream_set_write_callback(sample_stream, stream_write_callback, NULL);
pa_stream_connect_upload(sample_stream, sample_length); pa_stream_connect_upload(sample_stream, sample_length);
actions++;
break; break;
case EXIT: case EXIT:
pa_operation_unref(pa_context_exit_daemon(c, simple_callback, NULL)); o = pa_context_exit_daemon(c, simple_callback, NULL);
break; break;
case LIST: case LIST:
if (list_type) { if (list_type) {
if (pa_streq(list_type, "modules")) if (pa_streq(list_type, "modules"))
pa_operation_unref(pa_context_get_module_info_list(c, get_module_info_callback, NULL)); o = pa_context_get_module_info_list(c, get_module_info_callback, NULL);
else if (pa_streq(list_type, "sinks")) else if (pa_streq(list_type, "sinks"))
pa_operation_unref(pa_context_get_sink_info_list(c, get_sink_info_callback, NULL)); o = pa_context_get_sink_info_list(c, get_sink_info_callback, NULL);
else if (pa_streq(list_type, "sources")) else if (pa_streq(list_type, "sources"))
pa_operation_unref(pa_context_get_source_info_list(c, get_source_info_callback, NULL)); o = pa_context_get_source_info_list(c, get_source_info_callback, NULL);
else if (pa_streq(list_type, "sink-inputs")) else if (pa_streq(list_type, "sink-inputs"))
pa_operation_unref(pa_context_get_sink_input_info_list(c, get_sink_input_info_callback, NULL)); o = pa_context_get_sink_input_info_list(c, get_sink_input_info_callback, NULL);
else if (pa_streq(list_type, "source-outputs")) else if (pa_streq(list_type, "source-outputs"))
pa_operation_unref(pa_context_get_source_output_info_list(c, get_source_output_info_callback, NULL)); o = pa_context_get_source_output_info_list(c, get_source_output_info_callback, NULL);
else if (pa_streq(list_type, "clients")) else if (pa_streq(list_type, "clients"))
pa_operation_unref(pa_context_get_client_info_list(c, get_client_info_callback, NULL)); o = pa_context_get_client_info_list(c, get_client_info_callback, NULL);
else if (pa_streq(list_type, "samples")) else if (pa_streq(list_type, "samples"))
pa_operation_unref(pa_context_get_sample_info_list(c, get_sample_info_callback, NULL)); o = pa_context_get_sample_info_list(c, get_sample_info_callback, NULL);
else if (pa_streq(list_type, "cards")) else if (pa_streq(list_type, "cards"))
pa_operation_unref(pa_context_get_card_info_list(c, get_card_info_callback, NULL)); o = pa_context_get_card_info_list(c, get_card_info_callback, NULL);
else else
pa_assert_not_reached(); pa_assert_not_reached();
} else { } else {
actions = 8; o = pa_context_get_module_info_list(c, get_module_info_callback, NULL);
pa_operation_unref(pa_context_get_module_info_list(c, get_module_info_callback, NULL)); if (o) {
pa_operation_unref(pa_context_get_sink_info_list(c, get_sink_info_callback, NULL)); pa_operation_unref(o);
pa_operation_unref(pa_context_get_source_info_list(c, get_source_info_callback, NULL)); actions++;
pa_operation_unref(pa_context_get_sink_input_info_list(c, get_sink_input_info_callback, NULL)); }
pa_operation_unref(pa_context_get_source_output_info_list(c, get_source_output_info_callback, NULL));
pa_operation_unref(pa_context_get_client_info_list(c, get_client_info_callback, NULL)); o = pa_context_get_sink_info_list(c, get_sink_info_callback, NULL);
pa_operation_unref(pa_context_get_sample_info_list(c, get_sample_info_callback, NULL)); if (o) {
pa_operation_unref(pa_context_get_card_info_list(c, get_card_info_callback, NULL)); pa_operation_unref(o);
actions++;
}
o = pa_context_get_source_info_list(c, get_source_info_callback, NULL);
if (o) {
pa_operation_unref(o);
actions++;
}
o = pa_context_get_sink_input_info_list(c, get_sink_input_info_callback, NULL);
if (o) {
pa_operation_unref(o);
actions++;
}
o = pa_context_get_source_output_info_list(c, get_source_output_info_callback, NULL);
if (o) {
pa_operation_unref(o);
actions++;
}
o = pa_context_get_client_info_list(c, get_client_info_callback, NULL);
if (o) {
pa_operation_unref(o);
actions++;
}
o = pa_context_get_sample_info_list(c, get_sample_info_callback, NULL);
if (o) {
pa_operation_unref(o);
actions++;
}
o = pa_context_get_card_info_list(c, get_card_info_callback, NULL);
if (o) {
pa_operation_unref(o);
actions++;
}
o = NULL;
} }
break; break;
case MOVE_SINK_INPUT: case MOVE_SINK_INPUT:
pa_operation_unref(pa_context_move_sink_input_by_name(c, sink_input_idx, sink_name, simple_callback, NULL)); o = pa_context_move_sink_input_by_name(c, sink_input_idx, sink_name, simple_callback, NULL);
break; break;
case MOVE_SOURCE_OUTPUT: case MOVE_SOURCE_OUTPUT:
pa_operation_unref(pa_context_move_source_output_by_name(c, source_output_idx, source_name, simple_callback, NULL)); o = pa_context_move_source_output_by_name(c, source_output_idx, source_name, simple_callback, NULL);
break; break;
case LOAD_MODULE: case LOAD_MODULE:
pa_operation_unref(pa_context_load_module(c, module_name, module_args, index_callback, NULL)); o = pa_context_load_module(c, module_name, module_args, index_callback, NULL);
break; break;
case UNLOAD_MODULE: case UNLOAD_MODULE:
if (module_name) if (module_name)
pa_operation_unref(pa_context_get_module_info_list(c, unload_module_by_name_callback, NULL)); o = pa_context_get_module_info_list(c, unload_module_by_name_callback, NULL);
else else
pa_operation_unref(pa_context_unload_module(c, module_index, simple_callback, NULL)); o = pa_context_unload_module(c, module_index, simple_callback, NULL);
break; break;
case SUSPEND_SINK: case SUSPEND_SINK:
if (sink_name) if (sink_name)
pa_operation_unref(pa_context_suspend_sink_by_name(c, sink_name, suspend, simple_callback, NULL)); o = pa_context_suspend_sink_by_name(c, sink_name, suspend, simple_callback, NULL);
else else
pa_operation_unref(pa_context_suspend_sink_by_index(c, PA_INVALID_INDEX, suspend, simple_callback, NULL)); o = pa_context_suspend_sink_by_index(c, PA_INVALID_INDEX, suspend, simple_callback, NULL);
break; break;
case SUSPEND_SOURCE: case SUSPEND_SOURCE:
if (source_name) if (source_name)
pa_operation_unref(pa_context_suspend_source_by_name(c, source_name, suspend, simple_callback, NULL)); o = pa_context_suspend_source_by_name(c, source_name, suspend, simple_callback, NULL);
else else
pa_operation_unref(pa_context_suspend_source_by_index(c, PA_INVALID_INDEX, suspend, simple_callback, NULL)); o = pa_context_suspend_source_by_index(c, PA_INVALID_INDEX, suspend, simple_callback, NULL);
break; break;
case SET_CARD_PROFILE: case SET_CARD_PROFILE:
pa_operation_unref(pa_context_set_card_profile_by_name(c, card_name, profile_name, simple_callback, NULL)); o = pa_context_set_card_profile_by_name(c, card_name, profile_name, simple_callback, NULL);
break; break;
case SET_SINK_PORT: case SET_SINK_PORT:
pa_operation_unref(pa_context_set_sink_port_by_name(c, sink_name, port_name, simple_callback, NULL)); o = pa_context_set_sink_port_by_name(c, sink_name, port_name, simple_callback, NULL);
break; break;
case SET_DEFAULT_SINK: case SET_DEFAULT_SINK:
pa_operation_unref(pa_context_set_default_sink(c, sink_name, simple_callback, NULL)); o = pa_context_set_default_sink(c, sink_name, simple_callback, NULL);
break; break;
case SET_SOURCE_PORT: case SET_SOURCE_PORT:
pa_operation_unref(pa_context_set_source_port_by_name(c, source_name, port_name, simple_callback, NULL)); o = pa_context_set_source_port_by_name(c, source_name, port_name, simple_callback, NULL);
break; break;
case SET_DEFAULT_SOURCE: case SET_DEFAULT_SOURCE:
pa_operation_unref(pa_context_set_default_source(c, source_name, simple_callback, NULL)); o = pa_context_set_default_source(c, source_name, simple_callback, NULL);
break; break;
case SET_SINK_MUTE: case SET_SINK_MUTE:
if (mute == TOGGLE_MUTE) if (mute == TOGGLE_MUTE)
pa_operation_unref(pa_context_get_sink_info_by_name(c, sink_name, sink_toggle_mute_callback, NULL)); o = pa_context_get_sink_info_by_name(c, sink_name, sink_toggle_mute_callback, NULL);
else else
pa_operation_unref(pa_context_set_sink_mute_by_name(c, sink_name, mute, simple_callback, NULL)); o = pa_context_set_sink_mute_by_name(c, sink_name, mute, simple_callback, NULL);
break; break;
case SET_SOURCE_MUTE: case SET_SOURCE_MUTE:
if (mute == TOGGLE_MUTE) if (mute == TOGGLE_MUTE)
pa_operation_unref(pa_context_get_source_info_by_name(c, source_name, source_toggle_mute_callback, NULL)); o = pa_context_get_source_info_by_name(c, source_name, source_toggle_mute_callback, NULL);
else else
pa_operation_unref(pa_context_set_source_mute_by_name(c, source_name, mute, simple_callback, NULL)); o = pa_context_set_source_mute_by_name(c, source_name, mute, simple_callback, NULL);
break; break;
case SET_SINK_INPUT_MUTE: case SET_SINK_INPUT_MUTE:
if (mute == TOGGLE_MUTE) if (mute == TOGGLE_MUTE)
pa_operation_unref(pa_context_get_sink_input_info(c, sink_input_idx, sink_input_toggle_mute_callback, NULL)); o = pa_context_get_sink_input_info(c, sink_input_idx, sink_input_toggle_mute_callback, NULL);
else else
pa_operation_unref(pa_context_set_sink_input_mute(c, sink_input_idx, mute, simple_callback, NULL)); o = pa_context_set_sink_input_mute(c, sink_input_idx, mute, simple_callback, NULL);
break; break;
case SET_SOURCE_OUTPUT_MUTE: case SET_SOURCE_OUTPUT_MUTE:
if (mute == TOGGLE_MUTE) if (mute == TOGGLE_MUTE)
pa_operation_unref(pa_context_get_source_output_info(c, source_output_idx, source_output_toggle_mute_callback, NULL)); o = pa_context_get_source_output_info(c, source_output_idx, source_output_toggle_mute_callback, NULL);
else else
pa_operation_unref(pa_context_set_source_output_mute(c, source_output_idx, mute, simple_callback, NULL)); o = pa_context_set_source_output_mute(c, source_output_idx, mute, simple_callback, NULL);
break; break;
case SET_SINK_VOLUME: case SET_SINK_VOLUME:
if ((volume_flags & VOL_RELATIVE) == VOL_RELATIVE) { if ((volume_flags & VOL_RELATIVE) == VOL_RELATIVE) {
pa_operation_unref(pa_context_get_sink_info_by_name(c, sink_name, get_sink_volume_callback, NULL)); o = pa_context_get_sink_info_by_name(c, sink_name, get_sink_volume_callback, NULL);
} else { } else {
pa_cvolume v; pa_cvolume v;
pa_cvolume_set(&v, 1, volume); pa_cvolume_set(&v, 1, volume);
pa_operation_unref(pa_context_set_sink_volume_by_name(c, sink_name, &v, simple_callback, NULL)); o = pa_context_set_sink_volume_by_name(c, sink_name, &v, simple_callback, NULL);
} }
break; break;
case SET_SOURCE_VOLUME: case SET_SOURCE_VOLUME:
if ((volume_flags & VOL_RELATIVE) == VOL_RELATIVE) { if ((volume_flags & VOL_RELATIVE) == VOL_RELATIVE) {
pa_operation_unref(pa_context_get_source_info_by_name(c, source_name, get_source_volume_callback, NULL)); o = pa_context_get_source_info_by_name(c, source_name, get_source_volume_callback, NULL);
} else { } else {
pa_cvolume v; pa_cvolume v;
pa_cvolume_set(&v, 1, volume); pa_cvolume_set(&v, 1, volume);
pa_operation_unref(pa_context_set_source_volume_by_name(c, source_name, &v, simple_callback, NULL)); o = pa_context_set_source_volume_by_name(c, source_name, &v, simple_callback, NULL);
} }
break; break;
case SET_SINK_INPUT_VOLUME: case SET_SINK_INPUT_VOLUME:
if ((volume_flags & VOL_RELATIVE) == VOL_RELATIVE) { if ((volume_flags & VOL_RELATIVE) == VOL_RELATIVE) {
pa_operation_unref(pa_context_get_sink_input_info(c, sink_input_idx, get_sink_input_volume_callback, NULL)); o = pa_context_get_sink_input_info(c, sink_input_idx, get_sink_input_volume_callback, NULL);
} else { } else {
pa_cvolume v; pa_cvolume v;
pa_cvolume_set(&v, 1, volume); pa_cvolume_set(&v, 1, volume);
pa_operation_unref(pa_context_set_sink_input_volume(c, sink_input_idx, &v, simple_callback, NULL)); o = pa_context_set_sink_input_volume(c, sink_input_idx, &v, simple_callback, NULL);
} }
break; break;
case SET_SOURCE_OUTPUT_VOLUME: case SET_SOURCE_OUTPUT_VOLUME:
if ((volume_flags & VOL_RELATIVE) == VOL_RELATIVE) { if ((volume_flags & VOL_RELATIVE) == VOL_RELATIVE) {
pa_operation_unref(pa_context_get_source_output_info(c, source_output_idx, get_source_output_volume_callback, NULL)); o = pa_context_get_source_output_info(c, source_output_idx, get_source_output_volume_callback, NULL);
} else { } else {
pa_cvolume v; pa_cvolume v;
pa_cvolume_set(&v, 1, volume); pa_cvolume_set(&v, 1, volume);
pa_operation_unref(pa_context_set_source_output_volume(c, source_output_idx, &v, simple_callback, NULL)); o = pa_context_set_source_output_volume(c, source_output_idx, &v, simple_callback, NULL);
} }
break; break;
@ -1353,14 +1411,13 @@ static void context_state_callback(pa_context *c, void *userdata) {
break; break;
case SET_PORT_LATENCY_OFFSET: case SET_PORT_LATENCY_OFFSET:
pa_operation_unref(pa_context_set_port_latency_offset(c, card_name, port_name, latency_offset, simple_callback, NULL)); o = pa_context_set_port_latency_offset(c, card_name, port_name, latency_offset, simple_callback, NULL);
break; break;
case SUBSCRIBE: case SUBSCRIBE:
pa_context_set_subscribe_callback(c, context_subscribe_callback, NULL); pa_context_set_subscribe_callback(c, context_subscribe_callback, NULL);
pa_operation_unref(pa_context_subscribe( o = pa_context_subscribe(c,
c,
PA_SUBSCRIPTION_MASK_SINK| PA_SUBSCRIPTION_MASK_SINK|
PA_SUBSCRIPTION_MASK_SOURCE| PA_SUBSCRIPTION_MASK_SOURCE|
PA_SUBSCRIPTION_MASK_SINK_INPUT| PA_SUBSCRIPTION_MASK_SINK_INPUT|
@ -1371,12 +1428,23 @@ static void context_state_callback(pa_context *c, void *userdata) {
PA_SUBSCRIPTION_MASK_SERVER| PA_SUBSCRIPTION_MASK_SERVER|
PA_SUBSCRIPTION_MASK_CARD, PA_SUBSCRIPTION_MASK_CARD,
NULL, NULL,
NULL)); NULL);
break; break;
default: default:
pa_assert_not_reached(); pa_assert_not_reached();
} }
if (o) {
pa_operation_unref(o);
actions++;
}
if (actions == 0) {
pa_log("Operation failed: %s", pa_strerror(pa_context_errno(c)));
quit(1);
}
break; break;
case PA_CONTEXT_TERMINATED: case PA_CONTEXT_TERMINATED: