Skip to content

Buggy GCC 14.3.0 bounds-check #1812

@Green-Sky

Description

@Green-Sky

I updated my system (nixos) and am now unable to build.

[  0%] Building CXX object _deps/jolt-build/CMakeFiles/Jolt.dir/__/Jolt/Physics/Body/MassProperties.cpp.o
In file included from build/_deps/jolt-src/Build/../Jolt/Math/Matrix.h:7,
                 from build/_deps/jolt-src/Jolt/Physics/Body/MassProperties.cpp:8:
In member function ‘float& JPH::Vector<Rows>::operator[](JPH::uint) [with unsigned int Rows = 3]’,
    inlined from ‘bool JPH::MassProperties::DecomposePrincipalMomentsOfInertia(JPH::Mat44&, JPH::Vec3&) const’ at build/_deps/jolt-src/Jolt/Physics/Body/MassProperties.cpp:44:51:
build/_deps/jolt-src/Build/../Jolt/Math/Vector.h:48:28: error: array subscript 3 is above array bounds of ‘float [3]’ [-Werror=array-bounds=]
   48 |                 return mF32[inCoordinate];
      |                        ~~~~^
build/_deps/jolt-src/Build/../Jolt/Math/Vector.h: In member function ‘bool JPH::MassProperties::DecomposePrincipalMomentsOfInertia(JPH::Mat44&, JPH::Vec3&) const’:
build/_deps/jolt-src/Build/../Jolt/Math/Vector.h:208:57: note: while referencing ‘JPH::Vector<3>::mF32’
  208 |         float                                           mF32[Rows];
      |                                                         ^~~~

I solved the static analysis issue locally by adding a manual bounds check, but this is clearly a GCC bug.

diff --git a/Jolt/Physics/Body/MassProperties.cpp b/Jolt/Physics/Body/MassProperties.cpp
index 91df6b0d..eaeeba86 100644
--- a/Jolt/Physics/Body/MassProperties.cpp
+++ b/Jolt/Physics/Body/MassProperties.cpp
@@ -40,6 +40,8 @@ bool MassProperties::DecomposePrincipalMomentsOfInertia(Mat44 &outRotation, Vec3
        outRotation = Mat44::sIdentity();
        for (int i = 0; i < 3; ++i)
        {
+               if (indices[i] >= 3 || indices[i] < 0)
+                       continue; // unreachable, but gcc bounds checking is buggy
                outRotation.SetColumn3(i, Vec3(reinterpret_cast<Float3 &>(eigen_vec.GetColumn(indices[i]))));
                outDiagonal.SetComponent(i, eigen_val[indices[i]]);
        }

I am not sure how to proceed, but decided to post this here anyway, in case someone else comes across this issue too.

edit: Also I could have sworn I saw this bug in a c++ talk video recently...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions