Use signed arithmetic in enumeration helper
authorKevin Boyd <kevin44boyd@gmail.com>
Wed, 8 Jul 2020 13:44:30 +0000 (13:44 +0000)
committerChristian Blau <cblau.mail@gmail.com>
Wed, 8 Jul 2020 13:44:30 +0000 (13:44 +0000)
commit106b2d8ded04dbc51031022e9b11d147dccb76a3
tree3062b42ddce821bcfccac526cc884c52e3f93521
parent9aa69be648e48966ca18bed61d6d84d37a65bd11
Use signed arithmetic in enumeration helper

With a signed enum and the unsigned step, implicit conversion to unsigned
could change the value in the subtraction operator. Note that errors could
also occur with negative results in pure unsigned operations.

A negative result isn't expected for increasing enumerators, but the boost
iterator implementation does an == comparison that invokes the diff operator
and compares it to 0, and this error actually occurs there in unit tests,
and is caught by UBSAN.

Added some SFINAE to disable accidental instantiation of non-enum
types - before C++ 20 std::underlying_type on a non-enum is undefined.
src/gromacs/utility/enumerationhelpers.h