Skip to content

Commit aca3c55

Browse files
committed
audio: kpb: cast operands to size_t before width-widening multiplies
Six multiplications in the key-phrase buffer compute their product at 32-bit width and only then assign it to a wider size_t result. If the operands are large the overflow has already occurred before widening. The KPB sizing math is partly driven by externally-influenced values (cli->drain_req, the configured channel count, sampling frequency and container width), so this is a real overflow surface rather than a purely theoretical one. Cast the leading operand to size_t in each expression so the whole product is evaluated at the destination width: - kpb_micselect_copy16/32(): loop bound samples_per_chan * in_channels - kpb_init_draining(): drain_req and bytes_per_ms - adjust_drain_interval(): pipeline_period - validate_host_params(): bytes_per_ms No functional change on in-range inputs; only the intermediate arithmetic width changes. Found-by: CodeQL 2.24.2 (codeql/cpp-queries cpp-security-extended), rule cpp/integer-multiplication-cast-to-long. Run with database build-mode=none over sof/src (host clang cannot target the Xtensa production build), 867 files / 98 queries. Findings at kpb.c:1117,1148,1610,1619,1791,2397. AI-triaged: findings manually cross-referenced against clang-tidy bugprone-implicit-widening-of-multiplication-result and semgrep raptor-integer-truncation on the same surface, and confirmed the operand types (uint32_t / macro constants) against struct sof_kpb_config and struct kpb_client before fixing. Signed-off-by: Tomasz Leman <tomasz.m.leman@intel.com>
1 parent 0f9fa75 commit aca3c55

1 file changed

Lines changed: 6 additions & 6 deletions

File tree

src/audio/kpb.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,7 +1107,7 @@ static void kpb_micselect_copy16(struct comp_buffer *sink,
11071107

11081108
const int16_t *in_data;
11091109
int16_t *out_data;
1110-
const uint32_t samples_per_chan = size / (sizeof(uint16_t) * micsel_channels);
1110+
const size_t samples_per_chan = size / (sizeof(uint16_t) * micsel_channels);
11111111

11121112
for (ch = 0; ch < micsel_channels; ch++) {
11131113
out_samples = 0;
@@ -1138,7 +1138,7 @@ static void kpb_micselect_copy32(struct comp_buffer *sink,
11381138
uint16_t ch;
11391139
const int32_t *in_data;
11401140
int32_t *out_data;
1141-
const uint32_t samples_per_chan = size / (sizeof(uint32_t) * micsel_channels);
1141+
const size_t samples_per_chan = size / (sizeof(uint32_t) * micsel_channels);
11421142

11431143
for (ch = 0; ch < micsel_channels; ch++) {
11441144
out_samples = 0;
@@ -1607,7 +1607,7 @@ static void kpb_init_draining(struct comp_dev *dev, struct kpb_client *cli)
16071607
struct comp_data *kpb = comp_get_drvdata(dev);
16081608
bool is_sink_ready = (comp_buffer_get_sink_state(kpb->host_sink) == COMP_STATE_ACTIVE);
16091609
size_t sample_width = kpb->config.sampling_width;
1610-
size_t drain_req = cli->drain_req * kpb->config.channels *
1610+
size_t drain_req = (size_t)cli->drain_req * kpb->config.channels *
16111611
(kpb->config.sampling_freq / 1000) *
16121612
(KPB_SAMPLE_CONTAINER_SIZE(sample_width) / 8);
16131613
struct history_buffer *buff = kpb->hd.c_hb;
@@ -1616,7 +1616,7 @@ static void kpb_init_draining(struct comp_dev *dev, struct kpb_client *cli)
16161616
size_t local_buffered;
16171617
size_t drain_interval;
16181618
size_t host_period_size = kpb->host_period_size;
1619-
size_t bytes_per_ms = KPB_SAMPLES_PER_MS *
1619+
size_t bytes_per_ms = (size_t)KPB_SAMPLES_PER_MS *
16201620
(KPB_SAMPLE_CONTAINER_SIZE(sample_width) / 8) *
16211621
kpb->config.channels;
16221622
size_t period_bytes_limit;
@@ -1788,7 +1788,7 @@ static void adjust_drain_interval(struct comp_data *kpb, struct draining_data *d
17881788
/* average drained bytes per second */
17891789
actual_pace = (size_t)k_ms_to_cyc_ceil64(1000) / elapsed * drained;
17901790

1791-
pipeline_period = KPB_SAMPLES_PER_MS *
1791+
pipeline_period = (size_t)KPB_SAMPLES_PER_MS *
17921792
(KPB_SAMPLE_CONTAINER_SIZE(dd->sample_width) / 8) * kpb->config.channels;
17931793
/* desired draining pace in bytes per second */
17941794
optimal_pace = pipeline_period * KPB_DRAIN_NUM_OF_PPL_PERIODS_AT_ONCE * 1000;
@@ -2394,7 +2394,7 @@ static inline bool validate_host_params(struct comp_dev *dev,
23942394
*/
23952395
struct comp_data *kpb = comp_get_drvdata(dev);
23962396
size_t sample_width = kpb->config.sampling_width;
2397-
size_t bytes_per_ms = KPB_SAMPLES_PER_MS *
2397+
size_t bytes_per_ms = (size_t)KPB_SAMPLES_PER_MS *
23982398
(KPB_SAMPLE_CONTAINER_SIZE(sample_width) / 8) *
23992399
kpb->config.channels;
24002400
size_t pipeline_period_size = (dev->pipeline->period / 1000)

0 commit comments

Comments
 (0)