Boost logo

Boost Users :

From: Early Ehlinger (early_at_[hidden])
Date: 2007-03-28 12:46:31


Using MS Visual C++ 2005, Express Edition, the following snippet causes an
assertion failure due to a NULL pointer in the bowels of boost::format:

  boost::format f( "test %1%\n" );
  f % "";

I traced it down to boost/format/feed_args.hpp:

    template<class Ch, class Tr, class Alloc>
    void mk_str( std::basic_string<Ch,Tr, Alloc> & res,
                 const Ch * beg,
                 typename std::basic_string<Ch,Tr,Alloc>::size_type size,
                 std::streamsize w,
                 const Ch fill_char,
                 std::ios_base::fmtflags f,
                 const Ch prefix_space, // 0 if no space-padding
                 bool center)
    // applies centered/left/right padding to the string [beg, beg+size[
    // Effects : the result is placed in res.
    {
        typedef typename std::basic_string<Ch,Tr,Alloc>::size_type
size_type;
        res.resize(0);
        if( ( w<=0 || static_cast<size_type>(w) <=size)) {
            // no need to pad.
            res.reserve(size + !!prefix_space);
            if(prefix_space)
              res.append(1, prefix_space);
            res.append(beg, size); <<--- crashes, because beg==NULL and
size==0
...
My suggested fix looks like this:
    template<class Ch, class Tr, class Alloc>
    void mk_str( std::basic_string<Ch,Tr, Alloc> & res,
                 const Ch * beg,
                 typename std::basic_string<Ch,Tr,Alloc>::size_type size,
                 std::streamsize w,
                 const Ch fill_char,
                 std::ios_base::fmtflags f,
                 const Ch prefix_space, // 0 if no space-padding
                 bool center)
    // applies centered/left/right padding to the string [beg, beg+size[
    // Effects : the result is placed in res.
    {
        typedef typename std::basic_string<Ch,Tr,Alloc>::size_type
size_type;
        res.resize(0);
        if(size && ( w<=0 || static_cast<size_type>(w) <=size)) {
            // no need to pad.
            res.reserve(size + !!prefix_space);
            if(prefix_space)
              res.append(1, prefix_space);
            res.append(beg, size);
        }
        else {
            std::streamsize
n=static_cast<std::streamsize>(w-size-!!prefix_space);
            std::streamsize n_after = 0, n_before = 0;
            res.reserve(w); // allocate once for the 2 inserts
            if(center)
                n_after = n/2, n_before = n - n_after;
            else
                if(f & std::ios_base::left)
                    n_after = n;
                else
                    n_before = n;
            // now make the res string :
            if(n_before) res.append(n_before, fill_char);
            if(prefix_space)
              res.append(1, prefix_space);
            if ( size )
              res.append(beg, size);
            if(n_after) res.append(n_after, fill_char);
        }
    } // -mk_str(..)

-- Early Ehlinger, President, ResPower, Inc.


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