[wpimath] Fix invalid iterator access in TimeInterpolatableBuffer (#5138)

This commit is contained in:
Dustin Spicuzza
2023-02-26 19:42:24 -05:00
committed by GitHub
parent 099d048d9e
commit e5c4c6b1a7

View File

@@ -72,9 +72,11 @@ class TimeInterpolatableBuffer {
m_pastSnapshots.begin(), m_pastSnapshots.end(), time,
[](auto t, const auto& pair) { return t < pair.first; });
// Don't access this before ensuring first_after isn't first.
auto last_not_greater_than = first_after - 1;
if (last_not_greater_than == m_pastSnapshots.begin() ||
if (first_after == m_pastSnapshots.begin() ||
last_not_greater_than == m_pastSnapshots.begin() ||
last_not_greater_than->first < time) {
// Two cases handled together:
// 1. All entries come after the sample
@@ -125,6 +127,10 @@ class TimeInterpolatableBuffer {
m_pastSnapshots.begin(), m_pastSnapshots.end(), time,
[](const auto& pair, auto t) { return t > pair.first; });
if (upper_bound == m_pastSnapshots.begin()) {
return upper_bound->second;
}
auto lower_bound = upper_bound - 1;
double t = ((time - lower_bound->first) /