Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2019-11-29 09:10:22


Andrey Semashev wrote:
> > void my_append( fixed_string<512> & s, std::string_view s1,
> > std::string_view s2 )
> > {
> > if( s.size() + s1.size() + s2.size() > s.max_size() ) throw
> > std::length_error( "" );
> >
> > s += s1;
> > s += s2;
> > }
> >
> > Is this correct? (Spoiler: no.)
>
> I think, appending N strings still requires N tests, unless you know that
> the string sizes combined don't overflow.
>
> size_t size_left = s.max_size() - s.size();
> if (s1.size() > size_left) throw_length_error();
> size_left -= s1.size();
> if (s2.size() > size_left) throw_length_error();

Exactly. And the easiest and most readable way to get it right is just to do
the same op+= already would:

if( s1.size() > s.max_size() - s.size() ) throw std::length_error( "" );
s += s1;

if( s2.size() > s.max_size() - s.size() ) throw std::length_error( "" );
s += s2;

Incidentally, all the checks in the library seem wrong for this reason (they
check `size() + m > max_size()` instead of `m > max_size() - size()`.)


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk