universal7885: fm-aidl: MiddleState: Bounds checking

we need at least 2 elements on the vector. If it doesn't meet the requirements, return nullptr to keep it as-is unmodified.
This commit is contained in:
roynatech2544 2022-10-22 17:32:33 +09:00
commit ec1db378bd

View file

@ -3,6 +3,12 @@
middlestate_t *saveMiddleState(const int value, const std::vector<int> &vec) middlestate_t *saveMiddleState(const int value, const std::vector<int> &vec)
{ {
unsigned int i; unsigned int i;
// Bounds checking, we need at least 2 elements on the vector.
// If it doesn't meet the requirements, return nullptr to keep it
// as-is.
if (vec.size() <= 1) return nullptr;
for (i = 0; i < vec.size() - 1; i++) { for (i = 0; i < vec.size() - 1; i++) {
const int first = vec[i] - value; const int first = vec[i] - value;
const int second = vec[i + 1] - value; const int second = vec[i + 1] - value;