Opened 14 years ago

Closed 14 years ago

#2403 closed Bugs (wontfix)

Conversion of fixed size char array by lexical_cast

Reported by: Chard Owned by: nasonov
Milestone: Component: lexical_cast
Version: Boost 1.34.1 Severity: Problem
Keywords: lexical_cast fixed char array Cc:

Description

I can't say if this counts as a bug, since it is very contrived, but it does have a possibly unintended side-effect.

If lexical_cast is used to convert a fixed size char array, it will happily compile but, it will be treated as a null-terminated char array.

This means it will run off the end of the fixed length array.

E.g.

struct Data
{
    Data()
    {
        fixedLengthField[0] = '0';
        fixedLengthField[1] = '1';
        fixedLengthField[2] = '2';
        fixedLengthField[3] = '3';

        moreData[0] = 'X';
        moreData[1] = '\0';
    }

    // pack(1)
    char fixedLengthField[4];
    char moreData[2];
};

void
test()
{
    Data d;
    std::string x = boost::lexical_cast<std::string>(d.fixedLengthField);
    assert(x.size() == sizeof(d.fixedLengthField));
}

Change History (1)

comment:1 by nasonov, 14 years ago

Resolution: wontfix
Status: newclosed
Note: See TracTickets for help on using tickets.