openage icon indicating copy to clipboard operation
openage copied to clipboard

Time Invariant Violation and Hint-Dependent Search Inversion in `KeyframeContainer::last_before`

Open codeCraft-Ritik opened this issue 1 month ago • 1 comments


  • Subsystem: Gamestate Simulation & Curves (libopenage/curve)
  • Affected File: libopenage/curve/keyframe_container.h, lines 401–417
  • Severity: High (Simulation Logic Error / Gamestate Inconsistency)

Description & Root Cause

In openage's event-driven architecture, gamestate is stored in time-indexed curves backed by KeyframeContainer<T>. The method last_before is specified to return:

"The last element in the curve which is before the given time (i.e. elem->time < time). Given a hint where to start the search."

However, in libopenage/curve/keyframe_container.h:

template <typename T>
typename KeyframeContainer<T>::elem_ptr
KeyframeContainer<T>::last_before(const time::time_t &time,
                                  const KeyframeContainer<T>::elem_ptr &hint) const {
	elem_ptr at = hint;
	const elem_ptr end = this->container.size();

	if (at != end and this->container.at(at).time() < time) {
		// walk to the right until the time is larget than the searched
		while (at != end and this->container.at(at).time() <= time) {
			++at;
		}
		// go one back, because we want the last element that is <= time
		--at;
	}
	else { // idx == end or idx->time > time
		// walk to the left until the element time is smaller than the searched time
		while (at > 0 and (at == end or this->container.at(at).time() >= time)) {
			--at;
		}
	}

	return at;
}

Notice lines 403–407: When hint points to an element before time (this->container.at(at).time() < time), which is the standard scenario as simulation time advances forward, the loop condition checks: while (at != end and this->container.at(at).time() <= time)

Because <= is used, if an element exists exactly at time, the loop advances past it and then executes --at;. It stops on and returns the element where time() == time, violating the < time postcondition!

The comment on line 406 directly reveals the defect: // go one back, because we want the last element that is <= time The implementation was copied from last() (which searches for $\le t$) and was never updated to strict inequality ($< t$).

In contrast, if hint is greater than or equal to time, the else branch executes: while (at > 0 and (at == end or this->container.at(at).time() >= time)) { --at; } This correctly decrements until time() < time.

Therefore, the return value of last_before depends entirely on whether hint was before or after time:

  • Forward query (hint < time): Returns keyframe at time() == time (Incorrect).
  • Backward query (hint >= time): Returns keyframe at time() < time (Correct).

Steps to Reproduce

  1. Initialize a KeyframeContainer<int> with keyframes at timestamps [-INF, 0, 10, 20].
  2. Query c.last_before(10, hint=1) (hint at time 0 < 10). Result: index 2 (timestamp 10).
  3. Query c.last_before(10, hint=3) (hint at time 20 >= 10). Result: index 1 (timestamp 0).
  4. Verified in automated test suite test_bug2_keyframe_container_last_before_logic_simulation in test_openage_bugs.py.

Expected Behavior

last_before(10) must always return index 1 (timestamp 0), regardless of the hint provided.

Suggested Fix

Update line 403 to use < time:

--- a/libopenage/curve/keyframe_container.h
+++ b/libopenage/curve/keyframe_container.h
@@ -400,8 +400,8 @@ KeyframeContainer<T>::last_before(const time::time_t &time,
 	if (at != end and this->container.at(at).time() < time) {
-		// walk to the right until the time is larget than the searched
-		while (at != end and this->container.at(at).time() <= time) {
+		// walk to the right until the time is at or greater than the searched
+		while (at != end and this->container.at(at).time() < time) {
 			++at;
 		}
-		// go one back, because we want the last element that is <= time
+		// go one back, because we want the last element that is < time
 		--at;

codeCraft-Ritik avatar Oct 28 '26 04:10 codeCraft-Ritik

thanks! can you please submit your fix as PR?

TheJJ avatar Nov 01 '26 23:11 TheJJ