New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
messages: fix out of range assertion #11345
Conversation
@jcsp /usr/srcs/Ceph/work/ceph/src/messages/MMgrReport.h:39:17: warning: comparison of constant 255 with expression of type 'const enum perfcounter_type_d' is always true |
Gaaah! |
I don't really want to remove the assert, because there's no guarantee the enums are always going to be char-sized :-/ |
If that still triggers a clang warning we can just change it to 254, which should be just as effective. |
or we can declare the enum class perfcounter_type_d : uint8_t {
// ...
} |
@wjwithagen yeah, less code churn if unscoped version is used, or instead of keeping the casts, we can add template<typename E,
typename std::enable_if<std::is_enum<E>{}>::type* = nullptr>
void encode(E e, bufferlist& bl) {
encode(static_cast<typename std::underlying_type<E>::type>(e), bl);
} to delegate the |
Updated this to always use uint8_t for the enum, and just assert on the size of the resulting type (the assertion is still strictly always true, so let's see if clang complains about that too!) |
@@ -36,7 +36,7 @@ class PerfCounterType | |||
::encode(path, bl); | |||
::encode(description, bl); | |||
::encode(nick, bl); | |||
assert(type < 256); | |||
assert(sizeof(type) == 1); // enum type declared as uint8_t |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit, could be static_assert()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could be
static_assert (sizeof(type) == 1 , "enum type declared as uint8_t");
static_assert ( bool_constexpr ) // (since C++17)
When clang uses an 8 bit type for the enum, it complains (out of range) if comparing <256, and complains (tautological) if comparing <=256. Avoid this by explicitly making the enum an uint8_t, and just asserting that that it has that size at the point that we assume so for the encoding (in case someone modified the type definition without checking how it was used). Signed-off-by: John Spray <john.spray@redhat.com>
lgtm. |
clang detects that this is invalid (presumably
using an 8 bit type for the enum)
Signed-off-by: John Spray john.spray@redhat.com