public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: cauldwell.thomas@gmail.com
Cc: "libstdc++" <libstdc++@gcc.gnu.org>
Subject: Re: string::iterator should have more error checking
Date: Thu, 23 Jun 2022 22:26:27 +0100	[thread overview]
Message-ID: <CACb0b4=do5BegpLUt4pr2jcfi1D+PEFreEYDMazy5Fs0dThm6w@mail.gmail.com> (raw)
In-Reply-To: <CALtZhhNKysaytQeW_nwbZa2-Sft9CV0dsBhHQAa96EPb+uRthw@mail.gmail.com>

On Thu, 23 Jun 2022 at 22:05, Frederick Virchanza Gotham via Libstdc++
<libstdc++@gcc.gnu.org> wrote:
>
> If a program is compiled with "-D_GLIBCXX_DEBUG", I would expect it at
> runtime to catch the error on the last line in the following program:
>
> #include <iostream>
> #include <string>
> #include <string_view>
> #include <type_traits>
>
> using namespace std;
>
> int main(void)
> {
>     cout << "string::const_iterator is "
>          << (is_same_v< string::const_iterator, char const * > ? "just
> a raw pointer" : "NOT a simple pointer") << endl;
>
>     cout << "string_view::const_iterator is "
>          << (is_same_v< string_view::const_iterator, char const * > ?
> "just a raw pointer" : "NOT a simple pointer") << endl;
>
>     string s("brush");
>
>     cout << string_view( &*(s.cbegin() + 1u), &*(s.cend() + 876u) ) << endl;
> }
>
>
> string::iterator is NOT a simple pointer -- it is a class and so we
> can overload the following operators to catch errors:
>
> (1) unary operator*
> (2) binary operator+
> (3) binary operator-
>
> The error on the last line of the above program would be caught at
> runtime if the iterator were written as follows:
>
> class string {
>
>     class iterator {
>
>         char const *const p_min, *const p_max;  // initialised in constructor
>
>         char *p;
>
>     public:
>
>         iterator &operator+(ptrdiff_t const n)
>         {
>             assert( p+n >= p_min );
>             assert( p+n <= p_max );
>
>             // more code here
>         }
>     };
> };

I don't think it would be a good idea to do it like this when we
already have an entire Debug Mode framework for handling iterator
validity. You can see how that handles your exmples by repalcing
std::string with __gnu_debug::string (defined in <debug/string>).


> Similarly the unary operator* could be overloaded to catch the error
> when "end()" gets dereferenced.

The lack of iterator checking is documented at
https://gcc.gnu.org/onlinedocs/libstdc++/manual/debug_mode_semantics.html
and unlikely to change now. IIRC the main reasons for not adding
iterator checking to std::string were that it would hurt performance,
and that most std::string operations aren't done with iterators
anyway. As soon as you use c_str() or data() to get a raw pointer,
there's nothing that checked iterators can do.

AddressSanitizer does give an error for your example though.


  reply	other threads:[~2022-06-23 21:26 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-23 21:05 Frederick Virchanza Gotham
2022-06-23 21:26 ` Jonathan Wakely [this message]
2022-06-23 22:01   ` Frederick Virchanza Gotham
2022-06-23 22:35     ` Jonathan Wakely
2022-06-24  9:06       ` Frederick Virchanza Gotham
2022-06-24  9:35         ` Jonathan Wakely
2022-06-24 10:10           ` Frederick Virchanza Gotham
2022-06-24 10:28             ` Jonathan Wakely
2022-06-24 11:14               ` Frederick Virchanza Gotham

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CACb0b4=do5BegpLUt4pr2jcfi1D+PEFreEYDMazy5Fs0dThm6w@mail.gmail.com' \
    --to=jwakely@redhat.com \
    --cc=cauldwell.thomas@gmail.com \
    --cc=libstdc++@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).