openvdb icon indicating copy to clipboard operation
openvdb copied to clipboard

[BUG] CheckNormGrad() will square FLT_MAX causing a FP overflow.

Open stolk opened this issue 3 months ago • 1 comments

Environment

Debian Trixie

Describe the bug

Running the unit tests with FP exceptions enabled showed that CheckNormGrad() will square FLT_MAX, causing a FP overflow.

To Reproduce

Enable FP exceptions (MR with toggle for the test will follow.)

Expected behavior

No FP overflow

Additional context

I have a fix, and will be creating an MR to address this issue.

stolk avatar Sep 25 '25 15:09 stolk

This would fix it, but I was asked to remove it from my MR.

Just leaving this here in case it is ever needed:

commit 5938a497b6fa2e25e1d7cb56f6d0ef8587301f63
Author: Bram Stolk <[email protected]>
Date:   Tue Sep 23 13:50:02 2025 -0700

    Fix FP overflow by not squaring the max float/double value.
    
    FIXES #2090
    
    Signed-off-by: Bram Stolk <[email protected]>

diff --git a/openvdb/openvdb/tools/Diagnostics.h b/openvdb/openvdb/tools/Diagnostics.h
index c603ddc7..fb680042 100644
--- a/openvdb/openvdb/tools/Diagnostics.h
+++ b/openvdb/openvdb/tools/Diagnostics.h
@@ -24,6 +24,7 @@
 #include <tbb/parallel_reduce.h>
 
 #include <cmath> // for std::isnan(), std::isfinite()
+#include <limits>
 #include <set>
 #include <sstream>
 #include <string>
@@ -441,7 +442,7 @@ struct CheckNormGrad
         : acc(grid.getConstAccessor())
         , invdx2(ValueType(1.0/math::Pow2(grid.voxelSize()[0])))
         , minVal2(_min*_min)
-        , maxVal2(_max*_max)
+        , maxVal2(_max < std::numeric_limits<ValueType>::max() ? _max*_max : _max)
     {
         if ( !grid.hasUniformVoxels() ) {
             OPENVDB_THROW(ValueError, "CheckNormGrad: The transform must have uniform scale");

stolk avatar Nov 11 '25 23:11 stolk