Boost logo

Boost :

Subject: [boost] [filesystem][v3] Fixed-size char array constructor
From: Richard Hazlewood (boost_at_[hidden])
Date: 2010-09-09 03:09:01


The v3 path has a constructor/converter that takes a fixed-size char array.
It uses the size of the array to determine the characters to use in the path.

This can have unintended consequences for use cases like the following (common
with Windows system calls):

namespace bfs = boost::filesystem;

bfs::path GetTemporaryPath()
{
    // (Okay, it may be tidier to use allocated char* buffers for
    // these type of calls, but this approach is not unusual.)
    char buf[MAX_PATH];
    GetTempPath(sizeof(buf) / sizeof(buf[0]), buf);
    return bfs::path(buf);
    //return bfs::path(&buf[0]); //work-around
}

The path that is returned contains all of the MAX_PATH characters, even though
the system call does not fill all of these.

It appears that the constructor assumes the array is null-terminated because
it subtracts one. I presume this is to work with cases such as:

   char const buf[] = "mypath/myfile";
   return bfs::path(buf);

However, the null-terminator may not be the last char, as in the case given.


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