From ad69edbb4b230582ecd1863e68d0c2044f5ad901 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Thu, 26 Jul 2018 19:52:47 -0600 Subject: Use unsigned as base type for some enums -fsanitize=undefined complains about using operator~ on various enum types that are used with DEF_ENUM_FLAGS_TYPE. This patch fixes these problems by explicitly setting the base type for these enums to unsigned. It also adds a static assert to enum_flags to ensure that future enums used this way have an unsigned underlying type. gdb/ChangeLog 2018-10-03 Tom Tromey * common/enum-flags.h (enum_flags::operator~): Add static assert. * symfile-add-flags.h (enum symfile_add_flag): Use unsigned as base type. * objfile-flags.h (enum objfile_flag): Use unsigned as base type. * gdbtypes.h (enum type_instance_flag_value): Use unsigned as base type. * c-lang.h (enum c_string_type_values): Use unsigned as base type. * btrace.h (enum btrace_thread_flag): Use unsigned as base type. --- gdb/common/enum-flags.h | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'gdb/common') diff --git a/gdb/common/enum-flags.h b/gdb/common/enum-flags.h index 82568a5..f7ca8d8 100644 --- a/gdb/common/enum-flags.h +++ b/gdb/common/enum-flags.h @@ -164,6 +164,12 @@ public: } enum_flags operator~ () const { + // We only the underlying type to be unsigned when actually using + // operator~ -- if it were not unsigned, undefined behavior could + // result. However, asserting this in the class itself would + // require too many unnecessary changes to otherwise ok enum + // types. + gdb_static_assert (std::is_unsigned::value); return (enum_type) ~underlying_value (); } -- cgit v1.1