tesseract-robotics / trajopt

Trajectory Optimization Motion Planner for ROS
381 stars 103 forks source link

Enum cast out of range #415

Open rjoomen opened 1 month ago

rjoomen commented 1 month ago

Clang-tidy did find one more thing:

/home/user/tesseract_ws/src/trajopt/trajopt/include/trajopt/problem_description.hpp:39:10: warning: The value '7' provided to the cast expression is not in the valid range of values for 'TermType' [clang-analyzer-optin.core.EnumCastOutOfRange]
   39 |   return TermType(static_cast<T>(lhs) | static_cast<T>(rhs));
      |          ^
/home/user/tesseract_ws/src/trajopt/trajopt/include/trajopt/problem_description.hpp:28:12: note: enum declared here
   28 | enum class TermType : char
      | ~~~~~~~~~~~^~~~~~~~~~~~~~~
   29 | {
      | ~
   30 |   TT_INVALID = 0,     // 0000 0000
      |   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   31 |   TT_COST = 0x1,      // 0000 0001
      |   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   32 |   TT_CNT = 0x2,       // 0000 0010
      |   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   33 |   TT_USE_TIME = 0x4,  // 0000 0100
      |   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   34 | };
      | ~
/home/user/tesseract_ws/src/trajopt/trajopt/test/joint_costs_unit.cpp:593:13: note: Calling 'make_shared<trajopt::JointVelTermInfo, >'
  593 |   auto jv = std::make_shared<JointVelTermInfo>();
      |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/bits/shared_ptr.h:1009:14: note: Calling constructor for 'shared_ptr<trajopt::JointVelTermInfo>'
 1009 |       return shared_ptr<_Tp>(_Sp_alloc_shared_tag<_Alloc>{__a},
      |              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 1010 |                              std::forward<_Args>(__args)...);
      |                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/bits/shared_ptr.h:464:4: note: Calling constructor for '__shared_ptr<trajopt::JointVelTermInfo, __gnu_cxx::_S_atomic>'
  464 |         : __shared_ptr<_Tp>(__tag, std::forward<_Args>(__args)...)
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/bits/shared_ptr_base.h:1712:14: note: Calling constructor for '__shared_count<__gnu_cxx::_S_atomic>'
 1712 |         : _M_ptr(), _M_refcount(_M_ptr, __tag, std::forward<_Args>(__args)...)
      |                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/bits/shared_ptr_base.h:972:6: note: Calling constructor for '_Sp_counted_ptr_inplace<trajopt::JointVelTermInfo, std::allocator<void>, __gnu_cxx::_S_atomic>'
  972 |             _Sp_cp_type(__a._M_a, std::forward<_Args>(__args)...);
      |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/bits/shared_ptr_base.h:604:4: note: Calling 'allocator_traits::construct'
  604 |           allocator_traits<_Alloc>::construct(__a, _M_ptr(),
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  605 |               std::forward<_Args>(__args)...); // might throw
      |               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/bits/alloc_traits.h:661:4: note: Calling '_Construct<trajopt::JointVelTermInfo, >'
  661 |         { std::_Construct(__p, std::forward<_Args>(__args)...); }
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/bits/stl_construct.h:119:25: note: Calling default constructor for 'JointVelTermInfo'
  119 |       ::new((void*)__p) _Tp(std::forward<_Args>(__args)...);
      |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/user/tesseract_ws/src/trajopt/trajopt/include/trajopt/problem_description.hpp:491:33: note: Calling 'operator|'
  491 |   JointVelTermInfo() : TermInfo(TermType::TT_COST | TermType::TT_CNT | TermType::TT_USE_TIME) {}
      |                                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/user/tesseract_ws/src/trajopt/trajopt/include/trajopt/problem_description.hpp:39:10: note: The value '7' provided to the cast expression is not in the valid range of values for 'TermType'
   39 |   return TermType(static_cast<T>(lhs) | static_cast<T>(rhs));

The problem is that a term_type is created by bitwise OR of two enum values (e.g. here), resulting in a number that is not in the enum.

Levi-Armstrong commented 1 month ago

I believe this is expected, but not sure how to handle this in regards to clang-tidy when using scoped enum for bitflags.