test: correct emulation for channel-map TLV

Current implementation of channel-map TLV on test program is not valid.
Furthermore, it brings buffer-over-run due to byte counting.

This commit fixes it.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
This commit is contained in:
Takashi Sakamoto 2017-11-24 21:10:51 +09:00 committed by Takashi Iwai
parent 6f52b3d643
commit 38a39091e3

View file

@ -126,33 +126,39 @@ static void change_int_elem_members(struct elem_set_trial *trial,
static int allocate_int_elem_set_tlv(struct elem_set_trial *trial, static int allocate_int_elem_set_tlv(struct elem_set_trial *trial,
unsigned int **tlv) unsigned int **tlv)
{ {
unsigned int len, pos; unsigned int count, pos;
unsigned int i, j; unsigned int i, j;
struct chmap_entry *entry; struct chmap_entry *entry;
/* Calculate size of TLV packet for channel-mapping information. */ /* Calculate size of TLV packet for channel-mapping information. */
len = 0; count = 0;
for (i = 1; i <= 25; ++i) { for (i = 1; i <= 25; ++i) {
len += sizeof(struct chmap_entry); count += 2; /* sizeof(struct chmap_entry). */
len += i * sizeof(unsigned int); count += i; /* struct chmap_entry.maps. */
} }
*tlv = malloc(len); *tlv = malloc((2 + count) * sizeof(unsigned int));
if (*tlv == NULL) if (!*tlv)
return -ENOMEM; return -ENOMEM;
/* /*
* Emulate channel-mapping information in in-kernel implementation. * Emulate channel-mapping information in in-kernel implementation.
* Here, 25 entries are for each different channel. * Here, 25 entries are for each different channel.
*/ */
pos = 0; (*tlv)[0] = SNDRV_CTL_TLVT_CONTAINER;
for (i = 1; i <= 25 && pos < len; ++i) { (*tlv)[1] = count * sizeof(unsigned int);
pos = 2;
for (i = 1; i <= 25 && pos < count; ++i) {
entry = (struct chmap_entry *)&(*tlv)[pos]; entry = (struct chmap_entry *)&(*tlv)[pos];
entry->type = SNDRV_CTL_TLVT_CHMAP_FIXED; entry->type = SNDRV_CTL_TLVT_CHMAP_FIXED;
entry->length = i * sizeof(unsigned int); entry->length = i * sizeof(unsigned int);
pos += 2;
for (j = 0; j < i; ++j) for (j = 0; j < i; ++j)
entry->maps[j] = SND_CHMAP_MONO + j; entry->maps[j] = SND_CHMAP_MONO + j;
pos += sizeof(struct chmap_entry) + i * sizeof(unsigned int); pos += i;
} }
return 0; return 0;