This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [patch] Make vector::at() assertion message more useful (try #2)
- From: Daniel Krügler <daniel dot kruegler at gmail dot com>
- To: Paul Pluzhnikov <ppluzhnikov at google dot com>
- Cc: gcc-patches List <gcc-patches at gcc dot gnu dot org>, "libstdc++" <libstdc++ at gcc dot gnu dot org>
- Date: Wed, 4 Sep 2013 23:10:16 +0200
- Subject: Re: [patch] Make vector::at() assertion message more useful (try #2)
- Authentication-results: sourceware.org; auth=none
- References: <ye6qhae0qpf8 dot fsf at elbrus2 dot mtv dot corp dot google dot com>
2013/9/4 Paul Pluzhnikov <ppluzhnikov@google.com>:
> Greetings,
>
> This is a followup to:
> http://gcc.gnu.org/ml/libstdc++/2013-08/msg00096.html
>
> Without this patch, the user on vector::at out of bounds sees:
>
> terminate called after throwing an instance of 'std::out_of_range'
> what(): vector::_M_range_check
> Aborted (core dumped)
>
> With the patch:
>
> terminate called after throwing an instance of 'std::out_of_range'
> what(): vector::_M_range_check: __n (which is 3) >= this->size() (which is 2)
> Aborted (core dumped)
>
>
> I am not at all sure the names I choose here are good ones. Suggestions
> welcome.
>
> I also shudder at the idea of repeating _M_range_check code in
> e.g. string::at(), and elsewhere. Perhaps we need a snprintf_lite, that
> only understands '%zu' and literal characters, e.g.:
I expect that this kind of index violation is a rather often occurring
pattern and should be isolated. IMO the _M_range
_check now pessimisms the normal, non-violating case. Why not simply
replacing it by something along the lines of
_M_range_check(size_type __n) const
{
if (__n >= this->size())
__throw_out_of_range(__index_out_of_range_msg(__N("vector::_M_range_check"),
__n, this->size()));
}
where __index_out_of_range_msg is a reusable function that uses
something like snprintf_lite internally?
- Daniel