public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/56158] New: bad enum values computed by operator~ in ios_base.h
@ 2013-01-30 23:38 richard-gccbugzilla at metafoo dot co.uk
  2013-01-31 10:05 ` [Bug libstdc++/56158] " redi at gcc dot gnu.org
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: richard-gccbugzilla at metafoo dot co.uk @ 2013-01-30 23:38 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56158

             Bug #: 56158
           Summary: bad enum values computed by operator~ in ios_base.h
    Classification: Unclassified
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: richard-gccbugzilla@metafoo.co.uk


The overloaded operator~s defined for the enumerations in ios_base.h have the
following form:

  Enum operator~(Enum e) { return Enum(~static_cast<int>(e)); }

The ~ creates values outside the range of values of the enumeration type, so
the cast back to the Enum type has an unspecified value (see
[expr.static.cast]p10), and in practice it produces an Enum value outside the
range of representable values for the Enum type, so behavior is undefined.

Fix:

--- include/bits/ios_base.h
+++ include/bits/ios_base.h
@@ -87,7 +87,7 @@

   inline _GLIBCXX_CONSTEXPR _Ios_Fmtflags
   operator~(_Ios_Fmtflags __a)
-  { return _Ios_Fmtflags(~static_cast<int>(__a)); }
+  { return _Ios_Fmtflags(static_cast<int>(__a) ^
static_cast<int>(_S_ios_fmtflags_end - 1)); }

   inline const _Ios_Fmtflags&
   operator|=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b)
@@ -127,7 +127,7 @@

   inline _GLIBCXX_CONSTEXPR _Ios_Openmode
   operator~(_Ios_Openmode __a)
-  { return _Ios_Openmode(~static_cast<int>(__a)); }
+  { return _Ios_Openmode(static_cast<int>(__a) ^
static_cast<int>(_S_ios_openmode_end - 1)); }

   inline const _Ios_Openmode&
   operator|=(_Ios_Openmode& __a, _Ios_Openmode __b)
@@ -165,7 +165,7 @@

   inline _GLIBCXX_CONSTEXPR _Ios_Iostate
   operator~(_Ios_Iostate __a)
-  { return _Ios_Iostate(~static_cast<int>(__a)); }
+  { return _Ios_Iostate(static_cast<int>(__a) ^
static_cast<int>(_S_ios_iostate_end - 1)); }

   inline const _Ios_Iostate&
   operator|=(_Ios_Iostate& __a, _Ios_Iostate __b)


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

end of thread, other threads:[~2015-09-18 14:23 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-01-30 23:38 [Bug libstdc++/56158] New: bad enum values computed by operator~ in ios_base.h richard-gccbugzilla at metafoo dot co.uk
2013-01-31 10:05 ` [Bug libstdc++/56158] " redi at gcc dot gnu.org
2013-01-31 10:09 ` paolo.carlini at oracle dot com
2013-02-06  9:51 ` paolo.carlini at oracle dot com
2013-02-06 15:30 ` paolo.carlini at oracle dot com
2013-02-06 16:00 ` redi at gcc dot gnu.org
2013-02-06 16:40 ` paolo.carlini at oracle dot com
2013-02-07 11:23 ` paolo.carlini at oracle dot com
2013-02-07 11:40 ` redi at gcc dot gnu.org
2013-02-07 11:50 ` paolo.carlini at oracle dot com
2015-09-18 14:13 ` trippels at gcc dot gnu.org
2015-09-18 14:23 ` redi at gcc dot gnu.org

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).