topology: avoid to use the atoi() directly when expected

Signed-off-by: Jaroslav Kysela <perex@perex.cz>
This commit is contained in:
Jaroslav Kysela 2019-12-14 13:36:09 +01:00
parent 9e2bbccfcc
commit f373bf1f6e
9 changed files with 150 additions and 133 deletions

View file

@ -80,8 +80,8 @@ int tplg_parse_channel(snd_tplg_t *tplg, snd_config_t *cfg,
snd_config_iterator_t i, next;
snd_config_t *n;
struct snd_soc_tplg_channel *channel = private;
const char *id, *value;
int channel_id;
const char *id;
int channel_id, value;
if (tplg->channel_idx >= SND_SOC_TPLG_MAX_CHAN)
return -EINVAL;
@ -109,13 +109,13 @@ int tplg_parse_channel(snd_tplg_t *tplg, snd_config_t *cfg,
continue;
/* get value */
if (snd_config_get_string(n, &value) < 0)
if (tplg_get_integer(n, &value, 0) < 0)
continue;
if (strcmp(id, "reg") == 0)
channel->reg = atoi(value);
channel->reg = value;
else if (strcmp(id, "shift") == 0)
channel->shift = atoi(value);
channel->shift = value;
tplg_dbg("\t\t%s = %s\n", id, value);
}

View file

@ -286,7 +286,8 @@ static int tplg_parse_tlv_dbscale(snd_config_t *cfg, struct tplg_elem *elem)
snd_config_t *n;
struct snd_soc_tplg_ctl_tlv *tplg_tlv;
struct snd_soc_tplg_tlv_dbscale *scale;
const char *id = NULL, *value = NULL;
const char *id = NULL;
int val;
tplg_dbg(" scale: %s\n", elem->id);
@ -310,18 +311,18 @@ static int tplg_parse_tlv_dbscale(snd_config_t *cfg, struct tplg_elem *elem)
}
/* get value */
if (snd_config_get_string(n, &value) < 0)
if (tplg_get_integer(n, &val, 0))
continue;
tplg_dbg("\t%s = %s\n", id, value);
tplg_dbg("\t%s = %i\n", id, val);
/* get TLV data */
if (strcmp(id, "min") == 0)
scale->min = atoi(value);
scale->min = val;
else if (strcmp(id, "step") == 0)
scale->step = atoi(value);
scale->step = val;
else if (strcmp(id, "mute") == 0)
scale->mute = atoi(value);
scale->mute = val;
else
SNDERR("error: unknown key %s\n", id);
}
@ -372,7 +373,7 @@ int tplg_parse_control_bytes(snd_tplg_t *tplg,
snd_config_iterator_t i, next;
snd_config_t *n;
const char *id, *val = NULL;
int err;
int err, ival;
bool access_set = false, tlv_set = false;
elem = tplg_elem_new_common(tplg, cfg, NULL, SND_TPLG_TYPE_BYTES);
@ -398,37 +399,37 @@ int tplg_parse_control_bytes(snd_tplg_t *tplg,
continue;
if (strcmp(id, "base") == 0) {
if (snd_config_get_string(n, &val) < 0)
if (tplg_get_integer(n, &ival, 0))
return -EINVAL;
be->base = atoi(val);
be->base = ival;
tplg_dbg("\t%s: %d\n", id, be->base);
continue;
}
if (strcmp(id, "num_regs") == 0) {
if (snd_config_get_string(n, &val) < 0)
if (tplg_get_integer(n, &ival, 0))
return -EINVAL;
be->num_regs = atoi(val);
be->num_regs = ival;
tplg_dbg("\t%s: %d\n", id, be->num_regs);
continue;
}
if (strcmp(id, "max") == 0) {
if (snd_config_get_string(n, &val) < 0)
if (tplg_get_integer(n, &ival, 0))
return -EINVAL;
be->max = atoi(val);
be->max = ival;
tplg_dbg("\t%s: %d\n", id, be->max);
continue;
}
if (strcmp(id, "mask") == 0) {
if (snd_config_get_string(n, &val) < 0)
if (tplg_get_integer(n, &ival, 16))
return -EINVAL;
be->mask = strtol(val, NULL, 16);
be->mask = ival;
tplg_dbg("\t%s: %d\n", id, be->mask);
continue;
}
@ -598,7 +599,7 @@ int tplg_parse_control_mixer(snd_tplg_t *tplg,
snd_config_iterator_t i, next;
snd_config_t *n;
const char *id, *val = NULL;
int err, j;
int err, j, ival;
bool access_set = false, tlv_set = false;
elem = tplg_elem_new_common(tplg, cfg, NULL, SND_TPLG_TYPE_MIXER);
@ -647,10 +648,10 @@ int tplg_parse_control_mixer(snd_tplg_t *tplg,
}
if (strcmp(id, "max") == 0) {
if (snd_config_get_string(n, &val) < 0)
if (tplg_get_integer(n, &ival, 0))
return -EINVAL;
mc->max = atoi(val);
mc->max = ival;
tplg_dbg("\t%s: %d\n", id, mc->max);
continue;
}

View file

@ -426,7 +426,7 @@ int tplg_parse_dapm_graph(snd_tplg_t *tplg, snd_config_t *cfg,
snd_config_iterator_t i, next;
snd_config_t *n;
int err;
const char *graph_id, *val = NULL;
const char *graph_id;
int index = -1;
if (snd_config_get_type(cfg) != SND_CONFIG_TYPE_COMPOUND) {
@ -445,9 +445,10 @@ int tplg_parse_dapm_graph(snd_tplg_t *tplg, snd_config_t *cfg,
}
if (strcmp(id, "index") == 0) {
if (snd_config_get_string(n, &val) < 0)
if (tplg_get_integer(n, &index, 0))
return -EINVAL;
if (index < 0)
return -EINVAL;
index = atoi(val);
}
if (strcmp(id, "lines") == 0) {
@ -479,6 +480,7 @@ int tplg_parse_dapm_widget(snd_tplg_t *tplg,
snd_config_t *n;
const char *id, *val = NULL;
int widget_type, err;
int ival;
elem = tplg_elem_new_common(tplg, cfg, NULL, SND_TPLG_TYPE_DAPM_WIDGET);
if (!elem)
@ -540,55 +542,55 @@ int tplg_parse_dapm_widget(snd_tplg_t *tplg,
}
if (strcmp(id, "shift") == 0) {
if (snd_config_get_string(n, &val) < 0)
if (tplg_get_integer(n, &ival, 0))
return -EINVAL;
widget->shift = atoi(val);
widget->shift = ival;
tplg_dbg("\t%s: %d\n", id, widget->shift);
continue;
}
if (strcmp(id, "reg") == 0) {
if (snd_config_get_string(n, &val) < 0)
if (tplg_get_integer(n, &ival, 0))
return -EINVAL;
widget->reg = atoi(val);
widget->reg = ival;
tplg_dbg("\t%s: %d\n", id, widget->reg);
continue;
}
if (strcmp(id, "invert") == 0) {
if (snd_config_get_string(n, &val) < 0)
if (tplg_get_integer(n, &ival, 0))
return -EINVAL;
widget->invert = atoi(val);
widget->invert = ival;
tplg_dbg("\t%s: %d\n", id, widget->invert);
continue;
}
if (strcmp(id, "subseq") == 0) {
if (snd_config_get_string(n, &val) < 0)
if (tplg_get_integer(n, &ival, 0))
return -EINVAL;
widget->subseq= atoi(val);
widget->subseq = ival;
tplg_dbg("\t%s: %d\n", id, widget->subseq);
continue;
}
if (strcmp(id, "event_type") == 0) {
if (snd_config_get_string(n, &val) < 0)
if (tplg_get_integer(n, &ival, 0))
return -EINVAL;
widget->event_type = atoi(val);
widget->event_type = ival;
tplg_dbg("\t%s: %d\n", id, widget->event_type);
continue;
}
if (strcmp(id, "event_flags") == 0) {
if (snd_config_get_string(n, &val) < 0)
if (tplg_get_integer(n, &ival, 0))
return -EINVAL;
widget->event_flags = atoi(val);
widget->event_flags = ival;
tplg_dbg("\t%s: %d\n", id, widget->event_flags);
continue;
}

View file

@ -800,10 +800,10 @@ int tplg_parse_tokens(snd_tplg_t *tplg, snd_config_t *cfg,
{
snd_config_iterator_t i, next;
snd_config_t *n;
const char *id, *value;
const char *id;
struct tplg_elem *elem;
struct tplg_vendor_tokens *tokens;
int num_tokens = 0;
int num_tokens = 0, value;
elem = tplg_elem_new_common(tplg, cfg, NULL, SND_TPLG_TYPE_TOKEN);
if (!elem)
@ -830,12 +830,12 @@ int tplg_parse_tokens(snd_tplg_t *tplg, snd_config_t *cfg,
if (snd_config_get_id(n, &id) < 0)
continue;
if (snd_config_get_string(n, &value) < 0)
if (tplg_get_integer(n, &value, 0))
continue;
snd_strlcpy(tokens->token[tokens->num_tokens].id, id,
SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
tokens->token[tokens->num_tokens].value = atoi(value);
tokens->token[tokens->num_tokens].value = value;
tplg_dbg("\t\t %s : %d\n", tokens->token[tokens->num_tokens].id,
tokens->token[tokens->num_tokens].value);
tokens->num_tokens++;
@ -1013,7 +1013,7 @@ int tplg_parse_data(snd_tplg_t *tplg, snd_config_t *cfg,
snd_config_iterator_t i, next;
snd_config_t *n;
const char *id, *val = NULL;
int err = 0;
int err = 0, ival;
struct tplg_elem *elem;
elem = tplg_elem_new_common(tplg, cfg, NULL, SND_TPLG_TYPE_DATA);
@ -1071,10 +1071,10 @@ int tplg_parse_data(snd_tplg_t *tplg, snd_config_t *cfg,
}
if (strcmp(id, "type") == 0) {
if (snd_config_get_string(n, &val) < 0)
if (tplg_get_integer(n, &ival, 0))
return -EINVAL;
elem->vendor_type = atoi(val);
elem->vendor_type = ival;
tplg_dbg("\t%s: %d\n", id, elem->index);
continue;
}

View file

@ -153,7 +153,7 @@ struct tplg_elem* tplg_elem_new_common(snd_tplg_t *tplg,
enum snd_tplg_type type)
{
struct tplg_elem *elem;
const char *id, *val = NULL;
const char *id;
int obj_size = 0;
void *obj;
snd_config_iterator_t i, next;
@ -178,11 +178,14 @@ struct tplg_elem* tplg_elem_new_common(snd_tplg_t *tplg,
if (snd_config_get_id(n, &id))
continue;
if (strcmp(id, "index") == 0) {
if (snd_config_get_string(n, &val) < 0) {
if (tplg_get_integer(n, &elem->index, 0)) {
free(elem);
return NULL;
}
if (elem->index < 0) {
free(elem);
return NULL;
}
elem->index = atoi(val);
}
}
} else if (name != NULL)

View file

@ -42,7 +42,7 @@ static int lookup_ops(const char *c)
}
/* cant find string name in our table so we use its ID number */
return atoi(c);
return strtol(c, NULL, 0);
}
/* Parse Control operations. Ops can come from standard names above or

View file

@ -21,6 +21,35 @@
#include "list.h"
#include "tplg_local.h"
/*
* Get integer value
*/
int tplg_get_integer(snd_config_t *n, int *val, int base)
{
const char *str;
long lval;
int err;
switch (snd_config_get_type(n)) {
case SND_CONFIG_TYPE_INTEGER:
err = snd_config_get_integer(n, &lval);
if (err < 0)
return err;
if (lval < INT_MIN || lval > INT_MAX)
return -EINVAL;
*val = lval;
return err;
case SND_CONFIG_TYPE_STRING:
err = snd_config_get_string(n, &str);
if (err < 0)
return err;
*val = strtol(str, NULL, base);
return 0;
default:
return -EINVAL;
}
}
/*
* Parse compound
*/

View file

@ -368,6 +368,24 @@ static int split_rate(struct snd_soc_tplg_stream_caps *caps, char *str)
return 0;
}
static int parse_unsigned(snd_config_t *n, unsigned int *dst)
{
int ival;
if (tplg_get_integer(n, &ival, 0) < 0)
return -EINVAL;
*dst = ival;
#if TPLG_DEBUG
{
const char *id;
if (snd_config_get_id(n, &id) >= 0)
tplg_dbg("\t\t%s: %d\n", id, *dst);
}
#endif
return 0;
}
/* Parse pcm stream capabilities */
int tplg_parse_stream_caps(snd_tplg_t *tplg,
snd_config_t *cfg,
@ -402,10 +420,10 @@ int tplg_parse_stream_caps(snd_tplg_t *tplg,
if (id[0] == '#')
continue;
if (snd_config_get_string(n, &val) < 0)
return -EINVAL;
if (strcmp(id, "formats") == 0) {
if (snd_config_get_string(n, &val) < 0)
return -EINVAL;
s = strdup(val);
if (s == NULL)
return -ENOMEM;
@ -421,6 +439,9 @@ int tplg_parse_stream_caps(snd_tplg_t *tplg,
}
if (strcmp(id, "rates") == 0) {
if (snd_config_get_string(n, &val) < 0)
return -EINVAL;
s = strdup(val);
if (!s)
return -ENOMEM;
@ -436,68 +457,68 @@ int tplg_parse_stream_caps(snd_tplg_t *tplg,
}
if (strcmp(id, "rate_min") == 0) {
sc->rate_min = atoi(val);
tplg_dbg("\t\t%s: %d\n", id, sc->rate_min);
if (parse_unsigned(n, &sc->rate_min))
return -EINVAL;
continue;
}
if (strcmp(id, "rate_max") == 0) {
sc->rate_max = atoi(val);
tplg_dbg("\t\t%s: %d\n", id, sc->rate_max);
if (parse_unsigned(n, &sc->rate_max))
return -EINVAL;
continue;
}
if (strcmp(id, "channels_min") == 0) {
sc->channels_min = atoi(val);
tplg_dbg("\t\t%s: %d\n", id, sc->channels_min);
if (parse_unsigned(n, &sc->channels_min))
return -EINVAL;
continue;
}
if (strcmp(id, "channels_max") == 0) {
sc->channels_max = atoi(val);
tplg_dbg("\t\t%s: %d\n", id, sc->channels_max);
if (parse_unsigned(n, &sc->channels_max))
return -EINVAL;
continue;
}
if (strcmp(id, "periods_min") == 0) {
sc->periods_min = atoi(val);
tplg_dbg("\t\t%s: %d\n", id, sc->periods_min);
if (parse_unsigned(n, &sc->periods_min))
return -EINVAL;
continue;
}
if (strcmp(id, "periods_max") == 0) {
sc->periods_max = atoi(val);
tplg_dbg("\t\t%s: %d\n", id, sc->periods_max);
if (parse_unsigned(n, &sc->periods_max))
return -EINVAL;
continue;
}
if (strcmp(id, "period_size_min") == 0) {
sc->period_size_min = atoi(val);
tplg_dbg("\t\t%s: %d\n", id, sc->period_size_min);
if (parse_unsigned(n, &sc->period_size_min))
return -EINVAL;
continue;
}
if (strcmp(id, "period_size_max") == 0) {
sc->period_size_max = atoi(val);
tplg_dbg("\t\t%s: %d\n", id, sc->period_size_max);
if (parse_unsigned(n, &sc->period_size_max))
return -EINVAL;
continue;
}
if (strcmp(id, "buffer_size_min") == 0) {
sc->buffer_size_min = atoi(val);
tplg_dbg("\t\t%s: %d\n", id, sc->buffer_size_min);
if (parse_unsigned(n, &sc->buffer_size_min))
return -EINVAL;
continue;
}
if (strcmp(id, "buffer_size_max") == 0) {
sc->buffer_size_max = atoi(val);
tplg_dbg("\t\t%s: %d\n", id, sc->buffer_size_max);
if (parse_unsigned(n, &sc->buffer_size_max))
return -EINVAL;
continue;
}
if (strcmp(id, "sig_bits") == 0) {
sc->sig_bits = atoi(val);
tplg_dbg("\t\t%s: %d\n", id, sc->sig_bits);
if (parse_unsigned(n, &sc->sig_bits))
return -EINVAL;
continue;
}
@ -674,11 +695,8 @@ int tplg_parse_pcm(snd_tplg_t *tplg, snd_config_t *cfg,
continue;
if (strcmp(id, "id") == 0) {
if (snd_config_get_string(n, &val) < 0)
if (parse_unsigned(n, &pcm->pcm_id))
return -EINVAL;
pcm->pcm_id = atoi(val);
tplg_dbg("\t%s: %d\n", id, pcm->pcm_id);
continue;
}
@ -784,30 +802,21 @@ int tplg_parse_dai(snd_tplg_t *tplg, snd_config_t *cfg,
continue;
if (strcmp(id, "id") == 0) {
if (snd_config_get_string(n, &val) < 0)
if (parse_unsigned(n, &dai->dai_id))
return -EINVAL;
dai->dai_id = atoi(val);
tplg_dbg("\t%s: %d\n", id, dai->dai_id);
continue;
}
if (strcmp(id, "playback") == 0) {
if (snd_config_get_string(n, &val) < 0)
if (parse_unsigned(n, &dai->playback))
return -EINVAL;
dai->playback = atoi(val);
tplg_dbg("\t%s: %d\n", id, dai->playback);
continue;
}
if (strcmp(id, "capture") == 0) {
if (snd_config_get_string(n, &val) < 0)
if (parse_unsigned(n, &dai->capture))
return -EINVAL;
dai->capture = atoi(val);
tplg_dbg("\t%s: %d\n", id, dai->capture);
continue;
}
@ -949,11 +958,8 @@ int tplg_parse_link(snd_tplg_t *tplg,
continue;
if (strcmp(id, "id") == 0) {
if (snd_config_get_string(n, &val) < 0)
if (parse_unsigned(n, &link->id))
return -EINVAL;
link->id = atoi(val);
tplg_dbg("\t%s: %d\n", id, link->id);
continue;
}
@ -975,10 +981,8 @@ int tplg_parse_link(snd_tplg_t *tplg,
}
if (strcmp(id, "default_hw_conf_id") == 0) {
if (snd_config_get_string(n, &val) < 0)
if (parse_unsigned(n, &link->default_hw_config_id))
return -EINVAL;
link->default_hw_config_id = atoi(val);
continue;
}
@ -1030,7 +1034,7 @@ int tplg_parse_cc(snd_tplg_t *tplg,
struct tplg_elem *elem;
snd_config_iterator_t i, next;
snd_config_t *n;
const char *id, *val = NULL;
const char *id;
elem = tplg_elem_new_common(tplg, cfg, NULL, SND_TPLG_TYPE_CC);
if (!elem)
@ -1054,11 +1058,8 @@ int tplg_parse_cc(snd_tplg_t *tplg,
continue;
if (strcmp(id, "id") == 0) {
if (snd_config_get_string(n, &val) < 0)
if (parse_unsigned(n, &link->id))
return -EINVAL;
link->id = atoi(val);
tplg_dbg("\t%s: %d\n", id, link->id);
continue;
}
@ -1130,11 +1131,8 @@ int tplg_parse_hw_config(snd_tplg_t *tplg, snd_config_t *cfg,
continue;
if (strcmp(id, "id") == 0) {
if (snd_config_get_string(n, &val) < 0)
if (parse_unsigned(n, &hw_cfg->id))
return -EINVAL;
hw_cfg->id = atoi(val);
tplg_dbg("\t%s: %d\n", id, hw_cfg->id);
continue;
}
@ -1173,10 +1171,8 @@ int tplg_parse_hw_config(snd_tplg_t *tplg, snd_config_t *cfg,
if (strcmp(id, "bclk_freq") == 0 ||
strcmp(id, "bclk_rate") == 0) {
if (snd_config_get_string(n, &val) < 0)
if (parse_unsigned(n, &hw_cfg->bclk_rate))
return -EINVAL;
hw_cfg->bclk_rate = atoi(val);
continue;
}
@ -1223,19 +1219,15 @@ int tplg_parse_hw_config(snd_tplg_t *tplg, snd_config_t *cfg,
if (strcmp(id, "fsync_freq") == 0 ||
strcmp(id, "fsync_rate") == 0) {
if (snd_config_get_string(n, &val) < 0)
if (parse_unsigned(n, &hw_cfg->fsync_rate))
return -EINVAL;
hw_cfg->fsync_rate = atoi(val);
continue;
}
if (strcmp(id, "mclk_freq") == 0 ||
strcmp(id, "mclk_rate") == 0) {
if (snd_config_get_string(n, &val) < 0)
if (parse_unsigned(n, &hw_cfg->mclk_rate))
return -EINVAL;
hw_cfg->mclk_rate = atoi(val);
continue;
}
@ -1275,50 +1267,38 @@ int tplg_parse_hw_config(snd_tplg_t *tplg, snd_config_t *cfg,
}
if (strcmp(id, "tdm_slots") == 0) {
if (snd_config_get_string(n, &val) < 0)
if (parse_unsigned(n, &hw_cfg->tdm_slots))
return -EINVAL;
hw_cfg->tdm_slots = atoi(val);
continue;
}
if (strcmp(id, "tdm_slot_width") == 0) {
if (snd_config_get_string(n, &val) < 0)
if (parse_unsigned(n, &hw_cfg->tdm_slot_width))
return -EINVAL;
hw_cfg->tdm_slot_width = atoi(val);
continue;
}
if (strcmp(id, "tx_slots") == 0) {
if (snd_config_get_string(n, &val) < 0)
if (parse_unsigned(n, &hw_cfg->tx_slots))
return -EINVAL;
hw_cfg->tx_slots = atoi(val);
continue;
}
if (strcmp(id, "rx_slots") == 0) {
if (snd_config_get_string(n, &val) < 0)
if (parse_unsigned(n, &hw_cfg->rx_slots))
return -EINVAL;
hw_cfg->rx_slots = atoi(val);
continue;
}
if (strcmp(id, "tx_channels") == 0) {
if (snd_config_get_string(n, &val) < 0)
if (parse_unsigned(n, &hw_cfg->tx_channels))
return -EINVAL;
hw_cfg->tx_channels = atoi(val);
continue;
}
if (strcmp(id, "rx_channels") == 0) {
if (snd_config_get_string(n, &val) < 0)
if (parse_unsigned(n, &hw_cfg->rx_channels))
return -EINVAL;
hw_cfg->rx_channels = atoi(val);
continue;
}

View file

@ -280,6 +280,8 @@ struct tplg_elem *tplg_elem_lookup(struct list_head *base,
struct tplg_elem* tplg_elem_new_common(snd_tplg_t *tplg,
snd_config_t *cfg, const char *name, enum snd_tplg_type type);
int tplg_get_integer(snd_config_t *n, int *val, int base);
int tplg_parse_channel(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
snd_config_t *cfg, void *private);