[PATCH v5 2/6] platform/chrome: cros_ec_sensorhub: Fix TS filter history shift index

From: Satyam Gupta

Date: Wed Oct 14 2026 - 03:46:19 EST


From: Scott Collyer <scollyer@xxxxxxxxxx>

When propagating timestamp filter history entries in
cros_ec_sensor_ring_ts_filter_update(), starting the shift loop at
state->history_len - 1 skips shifting x_history[0] into x_history[1]
when history_len < CROS_EC_SENSORHUB_TS_HISTORY_SIZE. Conversely,
starting unconditionally at state->history_len would overrun the array
once the buffer is full.

Bound the loop starting index with min_t(int, state->history_len,
CROS_EC_SENSORHUB_TS_HISTORY_SIZE - 1) so new entries shift existing
elements properly while dropping the oldest element when full.

Signed-off-by: Scott Collyer <scollyer@xxxxxxxxxx>
Signed-off-by: Satyam Gupta <sgsatyam@xxxxxxxxxx>
---
drivers/platform/chrome/cros_ec_sensorhub_ring.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/drivers/platform/chrome/cros_ec_sensorhub_ring.c b/drivers/platform/chrome/cros_ec_sensorhub_ring.c
index cc038af59218..6975592c6b05 100644
--- a/drivers/platform/chrome/cros_ec_sensorhub_ring.c
+++ b/drivers/platform/chrome/cros_ec_sensorhub_ring.c
@@ -259,7 +259,7 @@ cros_ec_sensor_ring_ts_filter_update(struct cros_ec_sensors_ts_filter_state
s64 m; /* stored as *M_PRECISION */
s64 *m_history_copy = state->temp_buf;
s64 *error = state->temp_buf;
- int i;
+ int i, start;

/* we trust b the most, that'll be our independent variable */
x = b;
@@ -277,7 +277,9 @@ cros_ec_sensor_ring_ts_filter_update(struct cros_ec_sensors_ts_filter_state
state->history_len = 0;

/* Move everything over, also update offset to all absolute coords .*/
- for (i = state->history_len - 1; i >= 1; i--) {
+ start = min_t(int, state->history_len,
+ CROS_EC_SENSORHUB_TS_HISTORY_SIZE - 1);
+ for (i = start; i >= 1; i--) {
state->x_history[i] = state->x_history[i - 1] + dx;
state->y_history[i] = state->y_history[i - 1] + dy;

--
2.56.0.385.gd3acb90ef8-goog