Boost logo

Boost Users :

From: shunsuke (pstade.mb_at_[hidden])
Date: 2008-01-12 16:55:10


Richard Turrin wrote:
> Hi,
>
> I've encountered a bug with ireplace_all from the Boost String Algorithms library for version 1.35. If I pass a character array for the Format argument, the function ignores the null terminator and instead uses the entire array minus the last character to replace the occurrences of the Search string in the Input parameter. However, if I provide a pointer to a character array, the string replacement is done as expected.
>
> When I stepped into the code, I came across the following lines (excerpt from /trunk/boost/range/as_literal.hpp):
>
> template< class Char, std::size_t sz >
> inline iterator_range<const Char*> as_literal( const Char (&arr)[sz] )
> {
> return boost::make_iterator_range( arr, arr + sz - 1 );
> }
>
> There is no check for a null terminator. Nor did I find one after this function returned. I think the author here assumed that a caller would always pass in a character array of the right size.

Character array seems to have three views.

1. array: SIZE("ab\0c") == 5;
2. literal: SIZE("ab\0c") == 4;
3. null-terminated: SIZE("ab\0c") == 2;

Both 2. and 3. is forced into boost::as_array.
"One name, two facilities" usually makes trouble.
IMO, `as_c_str` should have been added for 3.

Regards,

-- 
Shunsuke Sogame

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net