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
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ 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] 7+ 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
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ 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] 7+ 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
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ 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] 7+ 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
  2024-06-18 20:11 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ 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] 7+ 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
                   ` (2 preceding siblings ...)
  2024-02-08 12:33 ` redi at gcc dot gnu.org
@ 2024-06-18 20:11 ` redi at gcc dot gnu.org
  2024-06-27  8:42 ` cvs-commit at gcc dot gnu.org
  2024-06-27  9:03 ` redi at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2024-06-18 20:11 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|NEW                         |ASSIGNED
           Assignee|unassigned at gcc dot gnu.org      |redi at gcc dot gnu.org

^ permalink raw reply	[flat|nested] 7+ 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
                   ` (3 preceding siblings ...)
  2024-06-18 20:11 ` redi at gcc dot gnu.org
@ 2024-06-27  8:42 ` cvs-commit at gcc dot gnu.org
  2024-06-27  9:03 ` redi at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-06-27  8:42 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jonathan Wakely <redi@gcc.gnu.org>:

https://gcc.gnu.org/g:cfc9fa3bdddc1af59b7854937b99516067fd8c63

commit r15-1688-gcfc9fa3bdddc1af59b7854937b99516067fd8c63
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Tue Jun 18 20:57:13 2024 +0100

    libstdc++: Enable more debug assertions during constant evaluation
[PR111250]

    Some of our debug assertions expand to nothing unless
    _GLIBCXX_ASSERTIONS is defined, which means they are not checked during
    constant evaluation. By making them unconditionally expand to a
    __glibcxx_assert expression they will be checked during constant
    evaluation. This allows us to diagnose more instances of undefined
    behaviour at compile-time, such as accessing a vector past-the-end.

    libstdc++-v3/ChangeLog:

            PR libstdc++/111250
            * include/debug/assertions.h (__glibcxx_requires_non_empty_range)
            (__glibcxx_requires_nonempty, __glibcxx_requires_subscript):
            Define to __glibcxx_assert expressions or to debug mode
            __glibcxx_check_xxx expressions.
            * testsuite/23_containers/array/element_access/constexpr_c++17.cc:
            Add checks for out-of-bounds accesses in constant expressions.
            * testsuite/23_containers/vector/element_access/constexpr.cc:
            Likewise.

^ permalink raw reply	[flat|nested] 7+ 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
                   ` (4 preceding siblings ...)
  2024-06-27  8:42 ` cvs-commit at gcc dot gnu.org
@ 2024-06-27  9:03 ` redi at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2024-06-27  9:03 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Fixed on trunk. I think this would be good to backport so that we diagnose more
errors during constant evaluation.

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

end of thread, other threads:[~2024-06-27  9:03 UTC | newest]

Thread overview: 7+ 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
2024-06-18 20:11 ` redi at gcc dot gnu.org
2024-06-27  8:42 ` cvs-commit at gcc dot gnu.org
2024-06-27  9:03 ` 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).