Merge pull request #11566 from hyperlogic/bug-fix/div-by-zero

div by zero fixes, detected by address sanitizer
This commit is contained in:
Andrew Meadows 2017-11-10 13:47:40 -08:00 committed by GitHub
commit 469b68011c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 5 deletions

View file

@ -54,7 +54,9 @@ void LODManager::autoAdjustLOD(float batchTime, float engineRunTime, float delta
float renderTime = batchTime + OVERLAY_AND_SWAP_TIME_BUDGET;
float maxTime = glm::max(renderTime, engineRunTime);
const float BLEND_TIMESCALE = 0.3f; // sec
float blend = BLEND_TIMESCALE / deltaTimeSec;
const float MIN_DELTA_TIME = 0.001f;
const float safeDeltaTime = glm::max(deltaTimeSec, MIN_DELTA_TIME);
float blend = BLEND_TIMESCALE / safeDeltaTime;
if (blend > 1.0f) {
blend = 1.0f;
}

View file

@ -39,7 +39,12 @@ static std::mutex rigRegistryMutex;
static bool isEqual(const glm::vec3& u, const glm::vec3& v) {
const float EPSILON = 0.0001f;
return glm::length(u - v) / glm::length(u) <= EPSILON;
float uLen = glm::length(u);
if (uLen == 0.0f) {
return glm::length(v) <= EPSILON;
} else {
return (glm::length(u - v) / uLen) <= EPSILON;
}
}
static bool isEqual(const glm::quat& p, const glm::quat& q) {

View file

@ -453,7 +453,9 @@ void Avatar::applyPositionDelta(const glm::vec3& delta) {
void Avatar::measureMotionDerivatives(float deltaTime) {
PerformanceTimer perfTimer("derivatives");
// linear
float invDeltaTime = 1.0f / deltaTime;
const float MIN_DELTA_TIME = 0.001f;
const float safeDeltaTime = glm::max(deltaTime, MIN_DELTA_TIME);
float invDeltaTime = 1.0f / safeDeltaTime;
// Floating point error prevents us from computing velocity in a naive way
// (e.g. vel = (pos - oldPos) / dt) so instead we use _positionOffsetAccumulator.
glm::vec3 velocity = _positionDeltaAccumulator * invDeltaTime;

View file

@ -59,8 +59,12 @@ public:
_max = other._max;
}
double totalSamples = _samples + other._samples;
_average = _average * ((double)_samples / totalSamples)
+ other._average * ((double)other._samples / totalSamples);
if (totalSamples > 0) {
_average = _average * ((double)_samples / totalSamples)
+ other._average * ((double)other._samples / totalSamples);
} else {
_average = 0.0f;
}
_samples += other._samples;
}