public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/111250] New: __glibcxx_requires_subscript assertions are not checked during constant evaluation
@ 2023-08-30 23:55 redi at gcc dot gnu.org
  2024-01-17 11:53 ` [Bug libstdc++/111250] " redi at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: redi at gcc dot gnu.org @ 2023-08-30 23:55 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111250

            Bug ID: 111250
           Summary: __glibcxx_requires_subscript assertions are not
                    checked during constant evaluation
           Product: gcc
           Version: 13.2.1
            Status: UNCONFIRMED
          Keywords: accepts-invalid
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redi at gcc dot gnu.org
  Target Milestone: ---

This means the following example from Peter Dimov only fails with
-D_GLIBCXX_ASSERTIONS or -D_GLIBCXX_DEBUG:

#include <vector>

constexpr bool f()
{
    std::vector<int> v{ 1, 2, 3 };
    return &v[3] == &v.front();
}

constexpr bool b = f();


The __glibcxx_assert macro expands to a __glibcxx_constexpr_assert check that
is always checked during constant evaluation, even without
-D_GLIBCXX_ASSERTIONS. However, the __glibcxx_requires_subscript macro does not
use __glibcxx_assert and just expands to nothing. See <debug/assertions.h>:

#ifndef _GLIBCXX_ASSERTIONS
# define __glibcxx_requires_non_empty_range(_First,_Last)
# define __glibcxx_requires_nonempty()
# define __glibcxx_requires_subscript(_N)
#else

// Verify that [_First, _Last) forms a non-empty iterator range.
# define __glibcxx_requires_non_empty_range(_First,_Last)       \
  __glibcxx_assert(_First != _Last)
# define __glibcxx_requires_subscript(_N)       \
  __glibcxx_assert(_N < this->size())
// Verify that the container is nonempty
# define __glibcxx_requires_nonempty()          \
  __glibcxx_assert(!this->empty())
#endif


I think we should remove the #ifndef and just always expand those to
__glibcxx_assert expressions. That will mean they're checked during constant
evaluation.

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

* [Bug libstdc++/111250] __glibcxx_requires_subscript assertions are not checked during constant evaluation
  2023-08-30 23:55 [Bug libstdc++/111250] New: __glibcxx_requires_subscript assertions are not checked during constant evaluation redi at gcc dot gnu.org
@ 2024-01-17 11:53 ` redi at gcc dot gnu.org
  2024-01-17 12:01 ` redi at gcc dot gnu.org
  2024-02-08 12:33 ` redi at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: redi at gcc dot gnu.org @ 2024-01-17 11:53 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111250

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2024-01-17
   Target Milestone|---                         |15.0
     Ever confirmed|0                           |1

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

* [Bug libstdc++/111250] __glibcxx_requires_subscript assertions are not checked during constant evaluation
  2023-08-30 23:55 [Bug libstdc++/111250] New: __glibcxx_requires_subscript assertions are not checked during constant evaluation redi at gcc dot gnu.org
  2024-01-17 11:53 ` [Bug libstdc++/111250] " redi at gcc dot gnu.org
@ 2024-01-17 12:01 ` redi at gcc dot gnu.org
  2024-02-08 12:33 ` redi at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: redi at gcc dot gnu.org @ 2024-01-17 12:01 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111250

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Something like this (with new/improved tests):

--- a/libstdc++-v3/include/debug/assertions.h
+++ b/libstdc++-v3/include/debug/assertions.h
@@ -31,15 +31,16 @@

 #include <bits/c++config.h>

-#ifndef _GLIBCXX_ASSERTIONS
-# define __glibcxx_requires_non_empty_range(_First,_Last)
-# define __glibcxx_requires_nonempty()
-# define __glibcxx_requires_subscript(_N)
+#ifdef _GLIBCXX_DEBUG
+# define __glibcxx_requires_non_empty_range(_First,_Last) \
+  __glibcxx_check_non_empty_range(_First, _Last)
+# define __glibcxx_requires_nonempty() __glibcxx_check_nonempty()
+# define __glibcxx_requires_subscript(_N) __glibcxx_check_subscript(_N)
 #else
-
 // Verify that [_First, _Last) forms a non-empty iterator range.
 # define __glibcxx_requires_non_empty_range(_First,_Last)      \
   __glibcxx_assert(_First != _Last)
+// Verify that N is a valid index into *this.
 # define __glibcxx_requires_subscript(_N)      \
   __glibcxx_assert(_N < this->size())
 // Verify that the container is nonempty


This change would use the more verbose, user-friendly assertions for the full
debug mode, and unconditionally use __glibcxx_assert otherwise.

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

* [Bug libstdc++/111250] __glibcxx_requires_subscript assertions are not checked during constant evaluation
  2023-08-30 23:55 [Bug libstdc++/111250] New: __glibcxx_requires_subscript assertions are not checked during constant evaluation redi at gcc dot gnu.org
  2024-01-17 11:53 ` [Bug libstdc++/111250] " redi at gcc dot gnu.org
  2024-01-17 12:01 ` redi at gcc dot gnu.org
@ 2024-02-08 12:33 ` redi at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: redi at gcc dot gnu.org @ 2024-02-08 12:33 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111250

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=112314

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Bug 112314 comment 6 has a suggestion for making __glibcxx_requires_valid_range
do some checks without _GLIBCXX_DEBUG.

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

end of thread, other threads:[~2024-02-08 12:33 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-30 23:55 [Bug libstdc++/111250] New: __glibcxx_requires_subscript assertions are not checked during constant evaluation redi at gcc dot gnu.org
2024-01-17 11:53 ` [Bug libstdc++/111250] " redi at gcc dot gnu.org
2024-01-17 12:01 ` redi at gcc dot gnu.org
2024-02-08 12:33 ` 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).