spa: improve error handling

Use impl_clear to clean up partially allocated handles. Make sure we
only clean up the initialized parts.
This commit is contained in:
Wim Taymans 2026-05-06 14:20:52 +02:00
parent 3e53487c72
commit 91755950dd
3 changed files with 33 additions and 33 deletions

View file

@ -2523,10 +2523,13 @@ static int impl_clear(struct spa_handle *handle)
this->codec->clear_props(this->codec_props);
if (this->transport)
spa_hook_remove(&this->transport_listener);
spa_system_close(this->data_system, this->timerfd);
spa_system_close(this->data_system, this->flush_timerfd);
if (this->codec->kind == MEDIA_CODEC_ASHA) {
spa_system_close(this->data_system, this->asha->timerfd);
if (this->timerfd > 0)
spa_system_close(this->data_system, this->timerfd);
if (this->flush_timerfd > 0)
spa_system_close(this->data_system, this->flush_timerfd);
if (this->codec->kind == MEDIA_CODEC_ASHA && this->asha) {
if (this->asha->timerfd > 0)
spa_system_close(this->data_system, this->asha->timerfd);
free(this->asha);
}
return 0;
@ -2674,38 +2677,30 @@ impl_init(const struct spa_handle_factory *factory,
if ((res = spa_system_timerfd_create(this->data_system,
CLOCK_MONOTONIC, SPA_FD_CLOEXEC | SPA_FD_NONBLOCK)) < 0)
goto error_remove_listener;
goto error;
this->timerfd = res;
if ((res = spa_system_timerfd_create(this->data_system,
CLOCK_MONOTONIC, SPA_FD_CLOEXEC | SPA_FD_NONBLOCK)) < 0)
goto error_close_timerfd;
goto error;
this->flush_timerfd = res;
if (this->codec->kind == MEDIA_CODEC_ASHA) {
this->asha = calloc(1, sizeof(struct spa_bt_asha));
if (this->asha == NULL) {
res = -errno;
goto error_close_flush_timerfd;
goto error;
}
this->asha->timerfd = -1;
if ((res = spa_system_timerfd_create(this->data_system,
CLOCK_MONOTONIC, SPA_FD_CLOEXEC | SPA_FD_NONBLOCK)) < 0)
goto error_free_asha;
goto error;
this->asha->timerfd = res;
}
return 0;
error_free_asha:
free(this->asha);
this->asha = NULL;
error_close_flush_timerfd:
spa_system_close(this->data_system, this->flush_timerfd);
error_close_timerfd:
spa_system_close(this->data_system, this->timerfd);
error_remove_listener:
spa_hook_remove(&this->transport_listener);
if (this->codec_props && this->codec->clear_props)
this->codec->clear_props(this->codec_props);
error:
impl_clear(handle);
return res;
}

View file

@ -2112,7 +2112,8 @@ static int impl_clear(struct spa_handle *handle)
this->codec->clear_props(this->codec_props);
if (this->transport)
spa_hook_remove(&this->transport_listener);
spa_system_close(this->data_system, this->timerfd);
if (this->timerfd > 0)
spa_system_close(this->data_system, this->timerfd);
spa_bt_decode_buffer_clear(&port->buffer);
return 0;
}
@ -2148,6 +2149,7 @@ impl_init(const struct spa_handle_factory *factory,
this->data_loop = spa_support_find(support, n_support, SPA_TYPE_INTERFACE_DataLoop);
this->data_system = spa_support_find(support, n_support, SPA_TYPE_INTERFACE_DataSystem);
this->loop_utils = spa_support_find(support, n_support, SPA_TYPE_INTERFACE_LoopUtils);
this->timerfd = -1;
spa_log_topic_init(this->log, &log_topic);
@ -2271,7 +2273,7 @@ impl_init(const struct spa_handle_factory *factory,
if ((res = spa_system_timerfd_create(this->data_system,
CLOCK_MONOTONIC, SPA_FD_CLOEXEC | SPA_FD_NONBLOCK)) < 0)
goto error_remove_listener;
goto error;
this->timerfd = res;
this->node_latency = 512;
@ -2282,10 +2284,8 @@ impl_init(const struct spa_handle_factory *factory,
return 0;
error_remove_listener:
spa_hook_remove(&this->transport_listener);
if (this->codec_props && this->codec->clear_props)
this->codec->clear_props(this->codec_props);
error:
impl_clear(handle);
return res;
}