From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 103086 invoked by alias); 28 Aug 2018 18:54:51 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 102304 invoked by uid 89); 28 Aug 2018 18:54:51 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-25.3 required=5.0 tests=AWL,BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=otoh, OTOH X-HELO: mx1.redhat.com Received: from mx3-rdu2.redhat.com (HELO mx1.redhat.com) (66.187.233.73) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 28 Aug 2018 18:54:49 +0000 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 55D3240241C9; Tue, 28 Aug 2018 18:54:48 +0000 (UTC) Received: from [127.0.0.1] (ovpn04.gateway.prod.ext.ams2.redhat.com [10.39.146.4]) by smtp.corp.redhat.com (Postfix) with ESMTP id A6C9C2166B41; Tue, 28 Aug 2018 18:54:47 +0000 (UTC) Subject: Re: [PATCH 2/9] Use unsigned as base type for some enums To: Simon Marchi , Tom Tromey References: <20180827145620.11055-1-tom@tromey.com> <20180827145620.11055-3-tom@tromey.com> <86e5c223-9fcc-ad6c-7c29-63037f4bdb74@ericsson.com> <87r2ijy2zm.fsf@tromey.com> Cc: gdb-patches@sourceware.org From: Pedro Alves Message-ID: Date: Tue, 28 Aug 2018 18:54:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-SW-Source: 2018-08/txt/msg00708.txt.bz2 On 08/27/2018 10:26 PM, Simon Marchi wrote: > On 2018-08-27 04:21 PM, Tom Tromey wrote: >>>>>>> "Simon" == Simon Marchi writes: >> >> Simon> Can you give an example of how the error manifests itself (I'm not really >> Simon> familiar with -fsanitize=undefined). Is the error reported at compile-time >> Simon> or run-time? I'm not able to make a synthetic standalone example to reproduce >> Simon> the error. >> >> You will get an error at runtime, and with the flags added by the last >> patch in the series, a crash. >> >> The error looks somewhat like the error from the expression dumping >> patch: >> >> runtime error: load of value 2887952, which is not a valid value for type 'exp_opcode' >> >> (I don't have an exact error handy, this was just taken from the other >> patch.) >> >> Simon> In any case, that LGTM if that makes the compiler happy. If the error reported >> Simon> by -fsanitize=undefined is at run-time, could we add a static assert in there >> Simon> to make sure the underlying types of types used with DEF_ENUM_FLAGS_TYPE are >> Simon> unsigned, to get a compilation error? >> >> With the final patch, any UB will cause gdb to crash (in development >> mode), presumably leading to a test suite failure. I think it isn't >> necessary to require unsigned as the underlying type -- any type will >> do. However I don't know how to assert that. > > Indeed, it's only necessary if the ~ operator is used. OTOH, it doesn't really > make sense to use a signed type for flags, so we wouldn't lose anything by enforcing > unsigned types. If I understand correctly, the errors come from code like this, when > making a bit mask to clear some bits: > > btinfo->flags &= ~(BTHR_MOVE | BTHR_STOP); > > Doing a static assert like this: > > diff --git a/gdb/common/enum-flags.h b/gdb/common/enum-flags.h > index 82568a5..c82970c 100644 > --- a/gdb/common/enum-flags.h > +++ b/gdb/common/enum-flags.h > @@ -92,6 +92,7 @@ class enum_flags > public: > typedef E enum_type; > typedef typename enum_underlying_type::type underlying_type; > + gdb_static_assert (std::is_unsigned::value); > Yeah. I've been meaning to find time to repost by enum-flags v2 fixes, but, it's never happened... One of the things that I wanted to look at is exactly this issue, seeing about coming up with some solution that is compatible with C++98/C++03, given that the GCC folks expressed interest in sharing the enum-flags.h header. Off hand, all I can think of is to introduce a "clear(unsigned mask)" method that would be used instead of &= and ~... But maybe really best is to ignore that (C++98), expecting that GCC will come around to requiring C++11 in some reasonable timeframe... > private: > /* Private type used to support initializing flag types with zero: > > > would enforce it at compile time, which is preferable than finding it at > runtime. Yes, I think that we should add that. Thanks, Pedro Alves