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>
Takashi Sakamoto authored 6 years ago
Takashi Iwai committed 6 years ago
125 | 125 | static int allocate_int_elem_set_tlv(struct elem_set_trial *trial, |
126 | 126 | unsigned int **tlv) |
127 | 127 | { |
128 | unsigned int len, pos; | |
128 | unsigned int count, pos; | |
129 | 129 | unsigned int i, j; |
130 | 130 | struct chmap_entry *entry; |
131 | 131 | |
132 | 132 | /* Calculate size of TLV packet for channel-mapping information. */ |
133 | len = 0; | |
133 | count = 0; | |
134 | 134 | for (i = 1; i <= 25; ++i) { |
135 | len += sizeof(struct chmap_entry); | |
136 | len += i * sizeof(unsigned int); | |
137 | } | |
138 | ||
139 | *tlv = malloc(len); | |
140 | if (*tlv == NULL) | |
135 | count += 2; /* sizeof(struct chmap_entry). */ | |
136 | count += i; /* struct chmap_entry.maps. */ | |
137 | } | |
138 | ||
139 | *tlv = malloc((2 + count) * sizeof(unsigned int)); | |
140 | if (!*tlv) | |
141 | 141 | return -ENOMEM; |
142 | 142 | |
143 | 143 | /* |
144 | 144 | * Emulate channel-mapping information in in-kernel implementation. |
145 | 145 | * Here, 25 entries are for each different channel. |
146 | 146 | */ |
147 | pos = 0; | |
148 | for (i = 1; i <= 25 && pos < len; ++i) { | |
147 | (*tlv)[0] = SNDRV_CTL_TLVT_CONTAINER; | |
148 | (*tlv)[1] = count * sizeof(unsigned int); | |
149 | pos = 2; | |
150 | ||
151 | for (i = 1; i <= 25 && pos < count; ++i) { | |
149 | 152 | entry = (struct chmap_entry *)&(*tlv)[pos]; |
153 | ||
150 | 154 | entry->type = SNDRV_CTL_TLVT_CHMAP_FIXED; |
151 | 155 | entry->length = i * sizeof(unsigned int); |
156 | pos += 2; | |
157 | ||
152 | 158 | for (j = 0; j < i; ++j) |
153 | 159 | entry->maps[j] = SND_CHMAP_MONO + j; |
154 | pos += sizeof(struct chmap_entry) + i * sizeof(unsigned int); | |
160 | pos += i; | |
155 | 161 | } |
156 | 162 | |
157 | 163 | return 0; |