From b0809d42d151925c4cff666379ae27eeaf7e554c Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Fri, 2 Jan 2015 20:10:56 +0000 Subject: [PATCH] alsa: Avoid alloca(). It is not in POSIX.1-2001, and its use is discouraged. Fortunately, the alsa library provides also variants which call ordinary malloc(), so use these instead. --- alsa_mix.c | 22 ++++++++++++++++------ alsa_write.c | 19 +++++++++++++------ 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/alsa_mix.c b/alsa_mix.c index 5a46963e..cad58af0 100644 --- a/alsa_mix.c +++ b/alsa_mix.c @@ -100,11 +100,13 @@ static bool channel_has_playback(snd_mixer_selem_channel_id_t chn, static char *alsa_mix_get_channels(struct mixer_handle *handle) { + int ret; char *list = NULL; snd_mixer_selem_id_t *sid; snd_mixer_elem_t *elem; - snd_mixer_selem_id_alloca(&sid); + ret = snd_mixer_selem_id_malloc(&sid); + assert(ret >= 0); elem = snd_mixer_first_elem(handle->mixer); for (; elem; elem = snd_mixer_elem_next(elem)) { char *tmp = list; @@ -122,6 +124,7 @@ static char *alsa_mix_get_channels(struct mixer_handle *handle) name); free(tmp); } + snd_mixer_selem_id_free(sid); return list; } @@ -133,7 +136,8 @@ static int alsa_mix_set_channel(struct mixer_handle *h, if (!mixer_channel) mixer_channel = "Master"; - snd_mixer_selem_id_alloca(&sid); + ret = snd_mixer_selem_id_malloc(&sid); + assert(ret >= 0); snd_mixer_selem_id_set_index(sid, selem_id); snd_mixer_selem_id_set_name(sid, mixer_channel); h->elem = snd_mixer_find_selem(h->mixer, sid); @@ -141,21 +145,27 @@ static int alsa_mix_set_channel(struct mixer_handle *h, PARA_NOTICE_LOG("unable to find simple control '%s',%i\n", snd_mixer_selem_id_get_name(sid), snd_mixer_selem_id_get_index(sid)); - return -E_BAD_CHANNEL; + ret = -E_BAD_CHANNEL; + goto out; } ret = snd_mixer_selem_get_playback_volume_range(h->elem, &h->pmin, &h->pmax); if (ret < 0) { PARA_NOTICE_LOG("unable to get %s range (%s): %s\n", mixer_channel, h->card, snd_strerror(ret)); - return -E_ALSA_MIX_RANGE; + ret = -E_ALSA_MIX_RANGE; + goto out; } if (h->pmin >= h->pmax) { PARA_NOTICE_LOG("alsa reported %s range %ld-%ld (%s)\n", mixer_channel, h->pmin, h->pmax, h->card); - return -E_ALSA_MIX_RANGE; + ret = -E_ALSA_MIX_RANGE; + goto out; } - return 1; + ret = 1; +out: + snd_mixer_selem_id_free(sid); + return ret; } static int alsa_mix_get(struct mixer_handle *h) diff --git a/alsa_write.c b/alsa_write.c index b0b21f2b..63a7055b 100644 --- a/alsa_write.c +++ b/alsa_write.c @@ -74,8 +74,8 @@ static snd_pcm_format_t get_alsa_pcm_format(enum sample_format sf) static int alsa_init(struct private_alsa_write_data *pad, struct alsa_write_args_info *conf) { - snd_pcm_hw_params_t *hwparams; - snd_pcm_sw_params_t *swparams; + snd_pcm_hw_params_t *hwparams = NULL; + snd_pcm_sw_params_t *swparams = NULL; snd_pcm_uframes_t start_threshold, stop_threshold; snd_pcm_uframes_t buffer_size, period_size; snd_output_t *output_log; @@ -89,7 +89,8 @@ static int alsa_init(struct private_alsa_write_data *pad, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK); if (ret < 0) goto fail; - snd_pcm_hw_params_alloca(&hwparams); + ret = snd_pcm_hw_params_malloc(&hwparams); + assert(ret >= 0); msg = "Broken alsa configuration"; ret = snd_pcm_hw_params_any(pad->handle, hwparams); if (ret < 0) @@ -140,7 +141,8 @@ static int alsa_init(struct private_alsa_write_data *pad, goto fail; /* software parameter setup */ - snd_pcm_sw_params_alloca(&swparams); + ret = snd_pcm_sw_params_malloc(&swparams); + assert(ret >= 0); snd_pcm_sw_params_current(pad->handle, swparams); snd_pcm_sw_params_set_avail_min(pad->handle, swparams, period_size); if (buffer_size < 1) @@ -190,13 +192,18 @@ static int alsa_init(struct private_alsa_write_data *pad, } snd_output_close(output_log); } - return 1; + ret = 1; + goto out; fail: if (ret < 0) PARA_ERROR_LOG("%s: %s\n", msg, snd_strerror(-ret)); else PARA_ERROR_LOG("%s\n", msg); - return -E_ALSA; + ret = -E_ALSA; +out: + snd_pcm_hw_params_free(hwparams); + snd_pcm_sw_params_free(swparams); + return ret; } static void alsa_write_pre_select(struct sched *s, void *context) -- 2.30.2