Skip to content

Commit 218f57c

Browse files
committed
tools: ctl: bound csv data write against abi header size
The ascii csv branch of read_setup() advances the write index past the 32-byte abi header for -r (no_abi) input but still bounds each write with n < n_max, while the binary branch stops at n_max - abi_size. A csv tuning file with ctrl_size/4 values then writes sizeof(struct sof_abi_hdr) bytes past the end of the tlv buffer. ctrl_size is not guaranteed to be a multiple of sizeof(uint32_t) either, so bounding the byte count alone still lets the final 4-byte store run up to 3 bytes past the buffer. Count the csv values and stop after the last whole uint32_t that fits in the data area, which also keeps the loop from mixing a size in bytes with an index into a uint32_t array. Signed-off-by: Syed Mohammed Nayyar <jmestwa@gmail.com>
1 parent 6f1170c commit 218f57c

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

tools/ctl/ctl.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ static int read_setup(struct ctl_data *ctl_data)
121121
int n = 0;
122122
FILE *fh;
123123
int data_start_int_index = 0;
124-
int data_int_index;
124+
int val_index = 0;
125+
int val_max;
125126

126127
/* open input file */
127128
fh = fdopen(ctl_data->in_fd, mode);
@@ -144,22 +145,25 @@ static int read_setup(struct ctl_data *ctl_data)
144145
}
145146

146147
/* reading for ASCII CSV txt */
147-
data_int_index = data_start_int_index;
148+
/* whole uint32_t values the data area can hold, the trailing bytes of an
149+
* unaligned control size are unusable
150+
*/
151+
val_max = (n_max - abi_size) / (int)sizeof(uint32_t);
148152
while (fscanf(fh, "%u", &x) != EOF) {
149-
if (n < n_max)
150-
ctl_data->buffer[data_int_index] = x;
153+
if (val_index < val_max)
154+
ctl_data->buffer[data_start_int_index + val_index] = x;
151155

152-
if (n > 0)
156+
if (val_index > 0)
153157
fprintf(stdout, ",");
154158

155159
fprintf(stdout, "%u", x);
156160
separator = fgetc(fh);
157161
while (separator != ',' && separator != EOF)
158162
separator = fgetc(fh);
159-
data_int_index++;
160-
n += sizeof(uint32_t);
163+
val_index++;
161164
}
162165

166+
n = val_index * (int)sizeof(uint32_t);
163167
fprintf(stdout, "\n");
164168

165169
read_done:

0 commit comments

Comments
 (0)