FxFoliageReplicator: array index out of bounds
by Aleksander · in Torque Game Engine · 03/09/2006 (6:28 pm) · 0 replies
Let's see on this code (engine/game/fx/fxFoliageReplicator.cc, line 1398):
-----------------------------------------------
// Yes, so calculate Global Sway Offset.
SwayOffsetX = mFieldData.mSwayMagnitudeSide * mCosTable[(U32)mGlobalSwayPhase];
SwayOffsetY = mFieldData.mSwayMagnitudeFront * mSinTable[(U32)mGlobalSwayPhase];
// Animate Global Sway Phase (Modulus).
mGlobalSwayPhase = mGlobalSwayPhase + (mGlobalSwayTimeRatio * ElapsedTime);
if (mGlobalSwayPhase >= 720.0f) mGlobalSwayPhase -= 720.0f;
-----------------------------------------------
This logic causes array index out of bounds:
mSinTable[(U32)mGlobalSwayPhase]
if (mGlobalSwayPhase >= 720.0f) mGlobalSwayPhase -= 720.0f;
because before "if (mGlobalSwayPhase >= 720.0f)" mGlobalSwayPhase may be more then 1440.0, so after "mGlobalSwayPhase -= 720.0f;" it may be more then 720, so using it in "mSinTable[(U32)mGlobalSwayPhase]" brings the error. So, sometimes foliages render incorrectly, somtimes a game crashes.
P.S. There are some similar code blocks in fxFoliageReplicator.cc which can cause array index out of bounds.
-----------------------------------------------
// Yes, so calculate Global Sway Offset.
SwayOffsetX = mFieldData.mSwayMagnitudeSide * mCosTable[(U32)mGlobalSwayPhase];
SwayOffsetY = mFieldData.mSwayMagnitudeFront * mSinTable[(U32)mGlobalSwayPhase];
// Animate Global Sway Phase (Modulus).
mGlobalSwayPhase = mGlobalSwayPhase + (mGlobalSwayTimeRatio * ElapsedTime);
if (mGlobalSwayPhase >= 720.0f) mGlobalSwayPhase -= 720.0f;
-----------------------------------------------
This logic causes array index out of bounds:
mSinTable[(U32)mGlobalSwayPhase]
if (mGlobalSwayPhase >= 720.0f) mGlobalSwayPhase -= 720.0f;
because before "if (mGlobalSwayPhase >= 720.0f)" mGlobalSwayPhase may be more then 1440.0, so after "mGlobalSwayPhase -= 720.0f;" it may be more then 720, so using it in "mSinTable[(U32)mGlobalSwayPhase]" brings the error. So, sometimes foliages render incorrectly, somtimes a game crashes.
P.S. There are some similar code blocks in fxFoliageReplicator.cc which can cause array index out of bounds.