OSDN Git Service

add snd_strlcpy() and use it everywhere
authorJaroslav Kysela <perex@perex.cz>
Mon, 25 Mar 2019 15:45:43 +0000 (16:45 +0100)
committerJaroslav Kysela <perex@perex.cz>
Mon, 25 Mar 2019 15:46:05 +0000 (16:46 +0100)
Signed-off-by: Jaroslav Kysela <perex@perex.cz>
14 files changed:
include/local.h
src/control/control.c
src/error.c
src/pcm/pcm_direct.c
src/pcm/pcm_ioplug.c
src/pcm/pcm_null.c
src/seq/seq.c
src/topology/ctl.c
src/topology/dapm.c
src/topology/data.c
src/topology/elem.c
src/topology/pcm.c
src/topology/text.c
src/topology/tplg_local.h

index e2cc291..5edad31 100644 (file)
@@ -236,6 +236,7 @@ int safe_strtol(const char *str, long *val);
 
 int snd_send_fd(int sock, void *data, size_t len, int fd);
 int snd_receive_fd(int sock, void *data, size_t len, int *fd);
+size_t snd_strlcpy(char *dst, const char *src, size_t size);
 
 /*
  * error messages
index 90fab87..c49b77a 100644 (file)
@@ -1805,7 +1805,7 @@ void snd_ctl_elem_id_set_subdevice(snd_ctl_elem_id_t *obj, unsigned int val)
 void snd_ctl_elem_id_set_name(snd_ctl_elem_id_t *obj, const char *val)
 {
        assert(obj);
-       strncpy((char *)obj->name, val, sizeof(obj->name));
+       snd_strlcpy((char *)obj->name, val, sizeof(obj->name));
 }
 
 /**
@@ -2723,7 +2723,7 @@ void snd_ctl_elem_info_set_subdevice(snd_ctl_elem_info_t *obj, unsigned int val)
 void snd_ctl_elem_info_set_name(snd_ctl_elem_info_t *obj, const char *val)
 {
        assert(obj);
-       strncpy((char *)obj->id.name, val, sizeof(obj->id.name));
+       snd_strlcpy((char *)obj->id.name, val, sizeof(obj->id.name));
 }
 
 /**
@@ -2945,7 +2945,7 @@ void snd_ctl_elem_value_set_subdevice(snd_ctl_elem_value_t *obj, unsigned int va
 void snd_ctl_elem_value_set_name(snd_ctl_elem_value_t *obj, const char *val)
 {
        assert(obj);
-       strncpy((char *)obj->id.name, val, sizeof(obj->id.name));
+       snd_strlcpy((char *)obj->id.name, val, sizeof(obj->id.name));
 }
 
 /**
index 628b7c8..2e617f8 100644 (file)
@@ -177,3 +177,25 @@ static void snd_err_msg_default(const char *file, int line, const char *function
 snd_lib_error_handler_t snd_err_msg = snd_err_msg_default;
 
 #endif
+
+/**
+ * \brief Copy a C-string into a sized buffer
+ * \param dst Where to copy the string to
+ * \param src Where to copy the string from
+ * \param size Size of destination buffer
+ * \retval The source string length
+ *
+ * The result is always a valid NUL-terminated string that fits
+ * in the buffer (unless, of course, the buffer size is zero).
+ * It does not pad out the result like strncpy() does.
+ */
+size_t snd_strlcpy(char *dst, const char *src, size_t size)
+{
+  size_t ret = strlen(src);
+  if (size) {
+    size_t len = ret >= size ? size - 1 : ret;
+    memcpy(dst, src, len);
+    dst[len] = '\0';
+  }
+  return ret;
+}
index 8998943..666a8ce 100644 (file)
@@ -766,9 +766,9 @@ int snd_pcm_direct_info(snd_pcm_t *pcm, snd_pcm_info_t * info)
        info->card = -1;
        /* FIXME: fill this with something more useful: we know the hardware name */
        if (pcm->name) {
-               strncpy((char *)info->id, pcm->name, sizeof(info->id));
-               strncpy((char *)info->name, pcm->name, sizeof(info->name));
-               strncpy((char *)info->subname, pcm->name, sizeof(info->subname));
+               snd_strlcpy((char *)info->id, pcm->name, sizeof(info->id));
+               snd_strlcpy((char *)info->name, pcm->name, sizeof(info->name));
+               snd_strlcpy((char *)info->subname, pcm->name, sizeof(info->subname));
        }
        info->subdevices_count = 1;
        return 0;
index 1e25190..a437ca3 100644 (file)
@@ -94,9 +94,9 @@ static int snd_pcm_ioplug_info(snd_pcm_t *pcm, snd_pcm_info_t *info)
        info->stream = pcm->stream;
        info->card = -1;
        if (pcm->name) {
-               strncpy((char *)info->id, pcm->name, sizeof(info->id));
-               strncpy((char *)info->name, pcm->name, sizeof(info->name));
-               strncpy((char *)info->subname, pcm->name, sizeof(info->subname));
+               snd_strlcpy((char *)info->id, pcm->name, sizeof(info->id));
+               snd_strlcpy((char *)info->name, pcm->name, sizeof(info->name));
+               snd_strlcpy((char *)info->subname, pcm->name, sizeof(info->subname));
        }
        info->subdevices_count = 1;
        return 0;
index 7afe158..ff61624 100644 (file)
@@ -71,9 +71,9 @@ static int snd_pcm_null_info(snd_pcm_t *pcm, snd_pcm_info_t * info)
        info->stream = pcm->stream;
        info->card = -1;
        if (pcm->name) {
-               strncpy((char *)info->id, pcm->name, sizeof(info->id));
-               strncpy((char *)info->name, pcm->name, sizeof(info->name));
-               strncpy((char *)info->subname, pcm->name, sizeof(info->subname));
+               snd_strlcpy((char *)info->id, pcm->name, sizeof(info->id));
+               snd_strlcpy((char *)info->name, pcm->name, sizeof(info->name));
+               snd_strlcpy((char *)info->subname, pcm->name, sizeof(info->subname));
        }
        info->subdevices_count = 1;
        return 0;
index c6a1ad1..afc8842 100644 (file)
@@ -1744,7 +1744,7 @@ void snd_seq_client_info_set_client(snd_seq_client_info_t *info, int client)
 void snd_seq_client_info_set_name(snd_seq_client_info_t *info, const char *name)
 {
        assert(info && name);
-       strncpy(info->name, name, sizeof(info->name));
+       snd_strlcpy(info->name, name, sizeof(info->name));
 }
 
 /**
@@ -2177,7 +2177,7 @@ void snd_seq_port_info_set_addr(snd_seq_port_info_t *info, const snd_seq_addr_t
 void snd_seq_port_info_set_name(snd_seq_port_info_t *info, const char *name)
 {
        assert(info && name);
-       strncpy(info->name, name, sizeof(info->name));
+       snd_strlcpy(info->name, name, sizeof(info->name));
 }
 
 /**
@@ -3122,7 +3122,7 @@ unsigned int snd_seq_queue_info_get_flags(const snd_seq_queue_info_t *info)
 void snd_seq_queue_info_set_name(snd_seq_queue_info_t *info, const char *name)
 {
        assert(info && name);
-       strncpy(info->name, name, sizeof(info->name));
+       snd_strlcpy(info->name, name, sizeof(info->name));
 }
 
 /**
@@ -3198,7 +3198,7 @@ int snd_seq_alloc_named_queue(snd_seq_t *seq, const char *name)
        memset(&info, 0, sizeof(info));
        info.locked = 1;
        if (name)
-               strncpy(info.name, name, sizeof(info.name) - 1);
+               snd_strlcpy(info.name, name, sizeof(info.name));
        return snd_seq_create_queue(seq, &info);
 }
 
@@ -3279,7 +3279,7 @@ int snd_seq_query_named_queue(snd_seq_t *seq, const char *name)
        int err;
        snd_seq_queue_info_t info;
        assert(seq && name);
-       strncpy(info.name, name, sizeof(info.name));
+       snd_strlcpy(info.name, name, sizeof(info.name));
        err = seq->ops->get_named_queue(seq, &info);
        if (err < 0)
                return err;
index 144bf51..9c13b12 100644 (file)
@@ -380,7 +380,7 @@ int tplg_parse_control_bytes(snd_tplg_t *tplg,
 
        be = elem->bytes_ext;
        be->size = elem->size;
-       elem_copy_text(be->hdr.name, elem->id, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
+       snd_strlcpy(be->hdr.name, elem->id, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
        be->hdr.type = SND_SOC_TPLG_TYPE_BYTES;
 
        tplg_dbg(" Control Bytes: %s\n", elem->id);
@@ -505,7 +505,7 @@ int tplg_parse_control_enum(snd_tplg_t *tplg, snd_config_t *cfg,
                return -ENOMEM;
 
        ec = elem->enum_ctrl;
-       elem_copy_text(ec->hdr.name, elem->id, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
+       snd_strlcpy(ec->hdr.name, elem->id, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
        ec->hdr.type = SND_SOC_TPLG_TYPE_ENUM;
        ec->size = elem->size;
        tplg->channel_idx = 0;
@@ -606,7 +606,7 @@ int tplg_parse_control_mixer(snd_tplg_t *tplg,
 
        /* init new mixer */
        mc = elem->mixer_ctrl;
-       elem_copy_text(mc->hdr.name, elem->id, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
+       snd_strlcpy(mc->hdr.name, elem->id, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
        mc->hdr.type = SND_SOC_TPLG_TYPE_MIXER;
        mc->size = elem->size;
        tplg->channel_idx = 0;
@@ -721,8 +721,7 @@ static int init_ctl_hdr(struct snd_soc_tplg_ctl_hdr *hdr,
        hdr->size = sizeof(struct snd_soc_tplg_ctl_hdr);
        hdr->type = t->type;
 
-       elem_copy_text(hdr->name, t->name,
-               SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
+       snd_strlcpy(hdr->name, t->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
 
        /* clean up access flag */
        if (t->access == 0)
index e5d473a..97c9695 100644 (file)
@@ -348,7 +348,7 @@ static int tplg_parse_line(const char *text,
        unsigned int len, i;
        const char *source = NULL, *sink = NULL, *control = NULL;
 
-       elem_copy_text(buf, text, LINE_SIZE);
+       snd_strlcpy(buf, text, LINE_SIZE);
 
        len = strlen(buf);
        if (len <= 2) {
@@ -488,7 +488,7 @@ int tplg_parse_dapm_widget(snd_tplg_t *tplg,
        tplg_dbg(" Widget: %s\n", elem->id);
 
        widget = elem->widget;
-       elem_copy_text(widget->name, elem->id, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
+       snd_strlcpy(widget->name, elem->id, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
        widget->size = elem->size;
 
        snd_config_for_each(i, next, cfg) {
@@ -523,7 +523,7 @@ int tplg_parse_dapm_widget(snd_tplg_t *tplg,
                        if (snd_config_get_string(n, &val) < 0)
                                return -EINVAL;
 
-                       elem_copy_text(widget->sname, val,
+                       snd_strlcpy(widget->sname, val,
                                       SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
                        tplg_dbg("\t%s: %s\n", id, val);
                        continue;
@@ -642,11 +642,11 @@ int tplg_add_route(snd_tplg_t *tplg, struct snd_tplg_graph_elem *t)
                return -ENOMEM;
 
        line = elem->route;
-       elem_copy_text(line->source, t->src, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
+       snd_strlcpy(line->source, t->src, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
        if (t->ctl)
-               elem_copy_text(line->control, t->ctl,
+               snd_strlcpy(line->control, t->ctl,
                        SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
-       elem_copy_text(line->sink, t->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
+       snd_strlcpy(line->sink, t->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
 
        return 0;
 }
@@ -684,9 +684,9 @@ int tplg_add_widget_object(snd_tplg_t *tplg, snd_tplg_obj_template_t *t)
        w->size = elem->size;
 
        w->id = wt->id;
-       elem_copy_text(w->name, wt->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
+       snd_strlcpy(w->name, wt->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
        if (wt->sname)
-               elem_copy_text(w->sname, wt->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
+               snd_strlcpy(w->sname, wt->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
        w->reg = wt->reg;
        w->shift = wt->shift;
        w->mask = wt->mask;
index 6b7c3f6..70a63d4 100644 (file)
@@ -473,7 +473,7 @@ static int copy_tuples(struct tplg_elem *elem,
                        case SND_SOC_TPLG_TUPLE_TYPE_STRING:
                                string = &array->string[j];
                                string->token = token_val;
-                               elem_copy_text(string->string, tuple->string,
+                               snd_strlcpy(string->string, tuple->string,
                                        SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
                                break;
 
@@ -587,7 +587,7 @@ static int parse_tuple_set(snd_config_t *cfg,
                        continue;
 
                tuple = &set->tuple[set->num_tuples];
-               elem_copy_text(tuple->token, id,
+               snd_strlcpy(tuple->token, id,
                                SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
 
                switch (type) {
@@ -597,7 +597,7 @@ static int parse_tuple_set(snd_config_t *cfg,
                        break;
 
                case SND_SOC_TPLG_TUPLE_TYPE_STRING:
-                       elem_copy_text(tuple->string, value,
+                       snd_strlcpy(tuple->string, value,
                                SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
                        tplg_dbg("\t\t%s = %s\n", tuple->token, tuple->string);
                        break;
@@ -818,7 +818,7 @@ int tplg_parse_tokens(snd_tplg_t *tplg, snd_config_t *cfg,
                if (snd_config_get_string(n, &value) < 0)
                        continue;
 
-               elem_copy_text(tokens->token[tokens->num_tokens].id, id,
+               snd_strlcpy(tokens->token[tokens->num_tokens].id, id,
                                SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
                tokens->token[tokens->num_tokens].value = atoi(value);
                tplg_dbg("\t\t %s : %d\n", tokens->token[tokens->num_tokens].id,
index 16ad442..a9d1d85 100644 (file)
@@ -47,7 +47,7 @@ int tplg_ref_add_elem(struct tplg_elem *elem, struct tplg_elem *elem_ref)
 
        ref->type = elem_ref->type;
        ref->elem = elem_ref;
-       elem_copy_text(ref->id,  elem_ref->id, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
+       snd_strlcpy(ref->id, elem_ref->id, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
 
        list_add_tail(&ref->list, &elem->ref_list);
        return 0;
@@ -169,7 +169,7 @@ struct tplg_elem* tplg_elem_new_common(snd_tplg_t *tplg,
        /* do we get name from cfg */
        if (cfg) {
                snd_config_get_id(cfg, &id);
-               elem_copy_text(elem->id, id, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
+               snd_strlcpy(elem->id, id, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
                elem->id[SNDRV_CTL_ELEM_ID_NAME_MAXLEN - 1] = 0;
                /* as we insert new elem based on the index value, move index
                   parsing here */
@@ -186,7 +186,7 @@ struct tplg_elem* tplg_elem_new_common(snd_tplg_t *tplg,
                        }
                }
        } else if (name != NULL)
-               elem_copy_text(elem->id, name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
+               snd_strlcpy(elem->id, name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
 
        switch (type) {
        case SND_TPLG_TYPE_DATA:
index 8ebfafd..5f586dc 100644 (file)
@@ -383,7 +383,7 @@ int tplg_parse_stream_caps(snd_tplg_t *tplg,
 
        sc = elem->stream_caps;
        sc->size = elem->size;
-       elem_copy_text(sc->name, elem->id, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
+       snd_strlcpy(sc->name, elem->id, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
 
        tplg_dbg(" PCM Capabilities: %s\n", elem->id);
 
@@ -562,7 +562,7 @@ static int tplg_parse_streams(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
                        /* store stream caps name, to find and merge
                         * the caps in building phase.
                         */
-                       elem_copy_text(caps[stream].name, value,
+                       snd_strlcpy(caps[stream].name, value,
                                SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
 
                        tplg_dbg("\t\t%s\n\t\t\t%s\n", id, value);
@@ -586,7 +586,7 @@ static int tplg_parse_fe_dai(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
 
        snd_config_get_id(cfg, &id);
        tplg_dbg("\t\tFE DAI %s:\n", id);
-       elem_copy_text(pcm->dai_name, id, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
+       snd_strlcpy(pcm->dai_name, id, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
 
        snd_config_for_each(i, next, cfg) {
 
@@ -653,7 +653,7 @@ int tplg_parse_pcm(snd_tplg_t *tplg,
 
        pcm = elem->pcm;
        pcm->size = elem->size;
-       elem_copy_text(pcm->pcm_name, elem->id, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
+       snd_strlcpy(pcm->pcm_name, elem->id, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
 
        tplg_dbg(" PCM: %s\n", elem->id);
 
@@ -754,7 +754,7 @@ int tplg_parse_dai(snd_tplg_t *tplg,
 
        dai = elem->dai;
        dai->size = elem->size;
-       elem_copy_text(dai->dai_name, elem->id,
+       snd_strlcpy(dai->dai_name, elem->id,
                SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
 
        tplg_dbg(" DAI: %s\n", elem->id);
@@ -920,7 +920,7 @@ int tplg_parse_link(snd_tplg_t *tplg,
 
        link = elem->link;
        link->size = elem->size;
-       elem_copy_text(link->name, elem->id, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
+       snd_strlcpy(link->name, elem->id, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
 
        tplg_dbg(" Link: %s\n", elem->id);
 
@@ -949,7 +949,7 @@ int tplg_parse_link(snd_tplg_t *tplg,
                        if (snd_config_get_string(n, &val) < 0)
                                return -EINVAL;
 
-                       elem_copy_text(link->stream_name, val,
+                       snd_strlcpy(link->stream_name, val,
                                       SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
                        tplg_dbg("\t%s: %s\n", id, val);
                        continue;
@@ -1319,7 +1319,7 @@ int tplg_parse_hw_config(snd_tplg_t *tplg, snd_config_t *cfg,
 static void tplg_add_stream_object(struct snd_soc_tplg_stream *strm,
                                struct snd_tplg_stream_template *strm_tpl)
 {
-       elem_copy_text(strm->name, strm_tpl->name,
+       snd_strlcpy(strm->name, strm_tpl->name,
                SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
        strm->format = strm_tpl->format;
        strm->rate = strm_tpl->rate;
@@ -1331,7 +1331,7 @@ static void tplg_add_stream_object(struct snd_soc_tplg_stream *strm,
 static void tplg_add_stream_caps(struct snd_soc_tplg_stream_caps *caps,
        struct snd_tplg_stream_caps_template *caps_tpl)
 {
-       elem_copy_text(caps->name, caps_tpl->name,
+       snd_strlcpy(caps->name, caps_tpl->name,
                SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
 
        caps->formats = caps_tpl->formats;
@@ -1370,9 +1370,9 @@ int tplg_add_pcm_object(snd_tplg_t *tplg, snd_tplg_obj_template_t *t)
        pcm = elem->pcm;
        pcm->size = elem->size;
 
-       elem_copy_text(pcm->pcm_name, pcm_tpl->pcm_name,
+       snd_strlcpy(pcm->pcm_name, pcm_tpl->pcm_name,
                SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
-       elem_copy_text(pcm->dai_name, pcm_tpl->dai_name,
+       snd_strlcpy(pcm->dai_name, pcm_tpl->dai_name,
                SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
        pcm->pcm_id = pcm_tpl->pcm_id;
        pcm->dai_id = pcm_tpl->dai_id;
@@ -1478,9 +1478,9 @@ int tplg_add_link_object(snd_tplg_t *tplg, snd_tplg_obj_template_t *t)
 
        /* ID and names */
        link->id = link_tpl->id;
-       elem_copy_text(link->name, link_tpl->name,
+       snd_strlcpy(link->name, link_tpl->name,
                       SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
-       elem_copy_text(link->stream_name, link_tpl->stream_name,
+       snd_strlcpy(link->stream_name, link_tpl->stream_name,
                       SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
 
        /* stream configs */
@@ -1540,7 +1540,7 @@ int tplg_add_dai_object(snd_tplg_t *tplg, snd_tplg_obj_template_t *t)
        dai = elem->dai;
        dai->size = elem->size;
 
-       elem_copy_text(dai->dai_name, dai_tpl->dai_name,
+       snd_strlcpy(dai->dai_name, dai_tpl->dai_name,
                SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
        dai->dai_id = dai_tpl->dai_id;
 
index cba6887..72647fe 100644 (file)
@@ -46,7 +46,7 @@ static int parse_text_values(snd_config_t *cfg, struct tplg_elem *elem)
                if (snd_config_get_string(n, &value) < 0)
                        continue;
 
-               elem_copy_text(&texts->items[j][0], value,
+               snd_strlcpy(&texts->items[j][0], value,
                        SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
                tplg_dbg("\t%s\n", &texts->items[j][0]);
 
index af59914..8d58b2a 100644 (file)
@@ -282,15 +282,6 @@ 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);
 
-static inline void elem_copy_text(char *dest, const char *src, int len)
-{
-       if (!dest || !src || !len)
-               return;
-
-       strncpy(dest, src, len);
-       dest[len - 1] = 0;
-}
-
 int tplg_parse_channel(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
        snd_config_t *cfg, void *private);