public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libc/27276] New: assert should not allow C++ scoped enums
@ 2021-01-29 13:27 jwakely.gcc at gmail dot com
  2021-01-29 17:38 ` [Bug libc/27276] " fweimer at redhat dot com
  2021-01-29 20:04 ` jwakely.gcc at gmail dot com
  0 siblings, 2 replies; 3+ messages in thread
From: jwakely.gcc at gmail dot com @ 2021-01-29 13:27 UTC (permalink / raw)
  To: glibc-bugs

https://sourceware.org/bugzilla/show_bug.cgi?id=27276

            Bug ID: 27276
           Summary: assert should not allow C++ scoped enums
           Product: glibc
           Version: 2.34
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: libc
          Assignee: unassigned at sourceware dot org
          Reporter: jwakely.gcc at gmail dot com
                CC: drepper.fsp at gmail dot com
  Target Milestone: ---

This should be ill-formed, because a scoped enum cannot be implicitly converted
to bool or compared to 0:

#include <assert.h>
enum class E { e1 = 1 };
int main()
{
  assert( E::e1 );
}

The current definition of assert(X) for C++ does static_cast<bool>(X) which
means the conversion to bool is explicit, which compiles.

The reason for using static_cast<bool>(X) is that (X) will suppress
-Wparentheses warnings about assignments used in truth values, i.e. so that
assert(a=b) will warn. This seems useful, but it would probably be better to
just teach GCC to warn about that directly, so that assert doesn't need to use
tricks to get a warning.

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=6906 suggests a way to use
__builtin_object_size combined with __attribute__((warning(""))) to detect
expressiomns with side-effects, though I'm not sure if it would work for
calling const vs non-const member functions of C++ objects.

I don't consider this accepts-invalid bug to be very important, I'm reporting
it just so the issue is recorded, not to request a change.

-- 
You are receiving this mail because:
You are on the CC list for the bug.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug libc/27276] assert should not allow C++ scoped enums
  2021-01-29 13:27 [Bug libc/27276] New: assert should not allow C++ scoped enums jwakely.gcc at gmail dot com
@ 2021-01-29 17:38 ` fweimer at redhat dot com
  2021-01-29 20:04 ` jwakely.gcc at gmail dot com
  1 sibling, 0 replies; 3+ messages in thread
From: fweimer at redhat dot com @ 2021-01-29 17:38 UTC (permalink / raw)
  To: glibc-bugs

https://sourceware.org/bugzilla/show_bug.cgi?id=27276

Florian Weimer <fweimer at redhat dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |fweimer at redhat dot com
              Flags|                            |security-

--- Comment #1 from Florian Weimer <fweimer at redhat dot com> ---
(In reply to Jonathan Wakely from comment #0)
> The reason for using static_cast<bool>(X) is that (X) will suppress
> -Wparentheses warnings about assignments used in truth values, i.e. so that
> assert(a=b) will warn. This seems useful, but it would probably be better to
> just teach GCC to warn about that directly, so that assert doesn't need to
> use tricks to get a warning.

On the other hand, assert is a macro that may call an implementation-defined
function (which could vary between C libraries), so teaching the compiler
directly about assert might be a bit of a maintenance burden.

Could we get a __builtin_identity (expr) that evaluates to the value of expr
(without parentheses)? Or is it possible to define that ourselves as a function
template?

-- 
You are receiving this mail because:
You are on the CC list for the bug.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug libc/27276] assert should not allow C++ scoped enums
  2021-01-29 13:27 [Bug libc/27276] New: assert should not allow C++ scoped enums jwakely.gcc at gmail dot com
  2021-01-29 17:38 ` [Bug libc/27276] " fweimer at redhat dot com
@ 2021-01-29 20:04 ` jwakely.gcc at gmail dot com
  1 sibling, 0 replies; 3+ messages in thread
From: jwakely.gcc at gmail dot com @ 2021-01-29 20:04 UTC (permalink / raw)
  To: glibc-bugs

https://sourceware.org/bugzilla/show_bug.cgi?id=27276

--- Comment #2 from Jonathan Wakely <jwakely.gcc at gmail dot com> ---
(In reply to Florian Weimer from comment #1)
> Could we get a __builtin_identity (expr) that evaluates to the value of expr
> (without parentheses)? Or is it possible to define that ourselves as a
> function template?

std::forward<decltype(expr)>(expr) but then you won't get a warning, because
a=b will no longer be used where a boolean is expected.

int a = 0;
forward<int>(a = 1) ? 1 : 0; // no warning

-- 
You are receiving this mail because:
You are on the CC list for the bug.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-01-29 20:04 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-29 13:27 [Bug libc/27276] New: assert should not allow C++ scoped enums jwakely.gcc at gmail dot com
2021-01-29 17:38 ` [Bug libc/27276] " fweimer at redhat dot com
2021-01-29 20:04 ` jwakely.gcc at gmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).