public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/106175] New: std::prev and next should check for equality with std::begin
@ 2022-07-03 15:10 deco33000 at yandex dot com
  2022-07-03 17:37 ` [Bug libstdc++/106175] " redi at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: deco33000 at yandex dot com @ 2022-07-03 15:10 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 106175
           Summary: std::prev and next should check for equality with
                    std::begin
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: deco33000 at yandex dot com
  Target Milestone: ---

Hi,

To get better defined algorithms, we should not have a segfault in that case:

int main() 
{
    std::vector<int> v;

    // if (v.empty()) {return;}; <- should not need that line

    for (auto i = begin(v); i != prev(end(v)); i += 1){

        std::cout << *i << std::endl;

    } 

    return 0;

}

std::prev should always check for equality with begin() or empty() for its
argument.
If equal, do nothing to the iterator. The loop won't be entered.

Same for std::next, and by extension std::advance.

if we need to do specific actions in case the vector is empty, that is an
opt-in decision.

There may be some good counter arguments but I don't see them..

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

* [Bug libstdc++/106175] std::prev and next should check for equality with std::begin
  2022-07-03 15:10 [Bug libstdc++/106175] New: std::prev and next should check for equality with std::begin deco33000 at yandex dot com
@ 2022-07-03 17:37 ` redi at gcc dot gnu.org
  2022-07-03 17:54 ` deco33000 at yandex dot com
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2022-07-03 17:37 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |WORKSFORME

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
How do you get the begin iterator from another iterator? That's impossible in
general. The std::prev function has no access to the vector, it can't call
begin on it.

For the debug mode, we already abort when you use prev on the begin iterator:


In function:
    gnu_debug::_Safe_iterator<_Iterator, _Sequence, 
    std::random_access_iterator_tag>& gnu_debug::_Safe_iterator<_Iterator, 
    _Sequence, std::random_access_iterator_tag>::operator+=(difference_type) 
    [with _Iterator = gnu_cxx::normal_iterator<int*, std::vector<int, 
    std::allocator<int> > >; _Sequence = std::debug::vector<int>; 
    difference_type = long int]

Error: attempt to advance a past-the-end iterator -1 steps, which falls 
outside its valid range.

Objects involved in the operation:
    iterator @ 0x7fff987ee7a0 {
      type = gnu_cxx::normal_iterator<int*, std::vector<int,
std::allocator<int> > > (mutable iterator);
      state = past-the-end;
      references sequence with type 'std::debug::vector<int,
std::allocator<int> >' @ 0x7fff987ee730
    }

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

* [Bug libstdc++/106175] std::prev and next should check for equality with std::begin
  2022-07-03 15:10 [Bug libstdc++/106175] New: std::prev and next should check for equality with std::begin deco33000 at yandex dot com
  2022-07-03 17:37 ` [Bug libstdc++/106175] " redi at gcc dot gnu.org
@ 2022-07-03 17:54 ` deco33000 at yandex dot com
  2022-07-03 18:00 ` deco33000 at yandex dot com
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: deco33000 at yandex dot com @ 2022-07-03 17:54 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from KL <deco33000 at yandex dot com> ---
During the parsing stage, the compiler has this information.

It knows that we want the end of v.
So it can inject a comparison into the resulting code.

A segfault here is not acceptable for a language like C++. There is no trap for
the compiler here.

That comparison is cheap.

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

* [Bug libstdc++/106175] std::prev and next should check for equality with std::begin
  2022-07-03 15:10 [Bug libstdc++/106175] New: std::prev and next should check for equality with std::begin deco33000 at yandex dot com
  2022-07-03 17:37 ` [Bug libstdc++/106175] " redi at gcc dot gnu.org
  2022-07-03 17:54 ` deco33000 at yandex dot com
@ 2022-07-03 18:00 ` deco33000 at yandex dot com
  2022-07-03 18:00 ` deco33000 at yandex dot com
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: deco33000 at yandex dot com @ 2022-07-03 18:00 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from KL <deco33000 at yandex dot com> ---
Please note, that comparison should only occur when you use next, prev or
advance.
It is useless for normal operations using end() directly. So no cost here.

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

* [Bug libstdc++/106175] std::prev and next should check for equality with std::begin
  2022-07-03 15:10 [Bug libstdc++/106175] New: std::prev and next should check for equality with std::begin deco33000 at yandex dot com
                   ` (2 preceding siblings ...)
  2022-07-03 18:00 ` deco33000 at yandex dot com
@ 2022-07-03 18:00 ` deco33000 at yandex dot com
  2022-07-03 19:04 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: deco33000 at yandex dot com @ 2022-07-03 18:00 UTC (permalink / raw)
  To: gcc-bugs

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

KL <deco33000 at yandex dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|WORKSFORME                  |---
             Status|RESOLVED                    |UNCONFIRMED

--- Comment #4 from KL <deco33000 at yandex dot com> ---
it is not resolved as is

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

* [Bug libstdc++/106175] std::prev and next should check for equality with std::begin
  2022-07-03 15:10 [Bug libstdc++/106175] New: std::prev and next should check for equality with std::begin deco33000 at yandex dot com
                   ` (3 preceding siblings ...)
  2022-07-03 18:00 ` deco33000 at yandex dot com
@ 2022-07-03 19:04 ` pinskia at gcc dot gnu.org
  2022-07-03 19:09 ` deco33000 at yandex dot com
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-07-03 19:04 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |WORKSFORME

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
> A segfault here is not acceptable for a language like C++.

Well no. This is how the language is defined. It is undefined which means you
should have a check before.

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

* [Bug libstdc++/106175] std::prev and next should check for equality with std::begin
  2022-07-03 15:10 [Bug libstdc++/106175] New: std::prev and next should check for equality with std::begin deco33000 at yandex dot com
                   ` (4 preceding siblings ...)
  2022-07-03 19:04 ` pinskia at gcc dot gnu.org
@ 2022-07-03 19:09 ` deco33000 at yandex dot com
  2022-07-03 20:46 ` pinskia at gcc dot gnu.org
  2022-07-04  6:28 ` deco33000 at yandex dot com
  7 siblings, 0 replies; 9+ messages in thread
From: deco33000 at yandex dot com @ 2022-07-03 19:09 UTC (permalink / raw)
  To: gcc-bugs

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

KL <deco33000 at yandex dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|WORKSFORME                  |---
             Status|RESOLVED                    |UNCONFIRMED

--- Comment #6 from KL <deco33000 at yandex dot com> ---
And this is not a normal situation for a language that aims to be safer.

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

* [Bug libstdc++/106175] std::prev and next should check for equality with std::begin
  2022-07-03 15:10 [Bug libstdc++/106175] New: std::prev and next should check for equality with std::begin deco33000 at yandex dot com
                   ` (5 preceding siblings ...)
  2022-07-03 19:09 ` deco33000 at yandex dot com
@ 2022-07-03 20:46 ` pinskia at gcc dot gnu.org
  2022-07-04  6:28 ` deco33000 at yandex dot com
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-07-03 20:46 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |WORKSFORME

--- Comment #7 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to KL from comment #6)
> And this is not a normal situation for a language that aims to be safer.

GCC's debug mode for libstdc++ detects it. If you don't want to use that while
debugging your program, then you should raise this up to the standards
committee ...

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

* [Bug libstdc++/106175] std::prev and next should check for equality with std::begin
  2022-07-03 15:10 [Bug libstdc++/106175] New: std::prev and next should check for equality with std::begin deco33000 at yandex dot com
                   ` (6 preceding siblings ...)
  2022-07-03 20:46 ` pinskia at gcc dot gnu.org
@ 2022-07-04  6:28 ` deco33000 at yandex dot com
  7 siblings, 0 replies; 9+ messages in thread
From: deco33000 at yandex dot com @ 2022-07-04  6:28 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from KL <deco33000 at yandex dot com> ---
Sending a message to the commitee is like sending a message to void.

Issue resolved.

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

end of thread, other threads:[~2022-07-04  6:28 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-03 15:10 [Bug libstdc++/106175] New: std::prev and next should check for equality with std::begin deco33000 at yandex dot com
2022-07-03 17:37 ` [Bug libstdc++/106175] " redi at gcc dot gnu.org
2022-07-03 17:54 ` deco33000 at yandex dot com
2022-07-03 18:00 ` deco33000 at yandex dot com
2022-07-03 18:00 ` deco33000 at yandex dot com
2022-07-03 19:04 ` pinskia at gcc dot gnu.org
2022-07-03 19:09 ` deco33000 at yandex dot com
2022-07-03 20:46 ` pinskia at gcc dot gnu.org
2022-07-04  6:28 ` deco33000 at yandex 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).