summaryrefslogtreecommitdiff
path: root/common/motion_sense_fifo.c
diff options
context:
space:
mode:
authorYuval Peress <peress@chromium.org>2020-01-14 11:02:34 -0700
committerCommit Bot <commit-bot@chromium.org>2020-02-14 01:06:36 +0000
commite8b5147f37bdb347ba25780bc4b969b37fd6b239 (patch)
treee6098efccad4da3009a0b9ac738f6a3d7014d7b5 /common/motion_sense_fifo.c
parent73abf0fc5fe3c55c6374a5c779569136e675cf0a (diff)
downloadchrome-ec-e8b5147f37bdb347ba25780bc4b969b37fd6b239.tar.gz
common: motion_sense_fifo: fix iteration bug
Fix potential bug when iterating over FIFO entries for commit. This bug can manifest when non-timestamp/non-data entries are inserted into the FIFO. TEST=Added unit tests BRANCH=None BUG=b:138303429,chromium:1023858 Change-Id: I64855db2141f7ad6aafb3f3a84632a1dabef11f4 Signed-off-by: Yuval Peress <peress@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2012846 Reviewed-by: Gwendal Grignou <gwendal@chromium.org>
Diffstat (limited to 'common/motion_sense_fifo.c')
-rw-r--r--common/motion_sense_fifo.c26
1 files changed, 24 insertions, 2 deletions
diff --git a/common/motion_sense_fifo.c b/common/motion_sense_fifo.c
index 3ae4317ed2..ee2ab70a32 100644
--- a/common/motion_sense_fifo.c
+++ b/common/motion_sense_fifo.c
@@ -13,6 +13,7 @@
#include "util.h"
#include "math_util.h"
#include "online_calibration.h"
+#include "stdbool.h"
#define CPRINTS(format, args...) cprints(CC_MOTION_SENSE, format, ## args)
@@ -79,6 +80,18 @@ static inline int is_timestamp(
}
/**
+ * Check whether or not a given sensor data entry contains sensor data or not.
+ *
+ * @param data The data entry to check.
+ * @return True if the entry contains data, false otherwise.
+ */
+static inline bool is_data(const struct ec_response_motion_sensor_data *data)
+{
+ return (data->flags & (MOTIONSENSE_SENSOR_FLAG_TIMESTAMP |
+ MOTIONSENSE_SENSOR_FLAG_ODR)) == 0;
+}
+
+/**
* Convenience function to get the head of the fifo. This function makes no
* guarantee on whether or not the entry is valid.
*
@@ -468,14 +481,23 @@ commit_data_end:
if (data->flags & MOTIONSENSE_SENSOR_FLAG_WAKEUP)
wake_up_needed = 1;
- /* Skip timestamp, we don't know the sensor number yet. */
- if (is_timestamp(data))
+ /*
+ * Skip non-data entries, we don't know the sensor number yet.
+ */
+ if (!is_data(data))
continue;
/* Get the sensor number and point to the timestamp entry. */
sensor_num = data->sensor_num;
data = peek_fifo_staged(i - 1);
+ /* Verify we're pointing at a timestamp. */
+ if (!is_timestamp(data)) {
+ CPRINTS("FIFO entries out of order,"
+ " expected timestamp");
+ continue;
+ }
+
/*
* If this is the first time we're seeing a timestamp for this
* sensor or the timestamp is after our computed next, skip