Ticket #4576: filesystem-buffer-size-problem.cpp

File filesystem-buffer-size-problem.cpp, 1.1 KB (added by Nate Silva <nate@…>, 12 years ago)

Test case showing the problem.

Line 
1#define BOOST_FILESYSTEM_VERSION 3
2
3#include <iostream>
4#include <cassert>
5#include <cstring>
6#include <algorithm>
7#include <boost/filesystem.hpp>
8
9int main()
10{
11 // EXAMPLE 1: buffer size known at compile time
12 char buffer1[255] = "./foo";
13 boost::filesystem::path p1(buffer1);
14
15 // size() should be 5 (for "./foo") but is actually 254 because
16 // the entire buffer except for the final character was added to
17 // the path!
18 //
19 // The expected behavior would be to stop at the first
20 // NULL-terminator.
21
22 std::cout << "p1.string().size() is " << p1.string().size() << std::endl;
23 // output: p1.string().size() is 254
24
25 // EXAMPLE 2: dynamically allocated buffer
26 char *buffer2 = new char[255];
27 std::fill(buffer2, buffer2 + 255, '\0');
28 strcpy(buffer2, "./foo");
29
30 // buffer2 is now identical to buffer1
31 assert(std::equal(buffer1, buffer1 + 255, buffer2));
32
33 // ...but boost::filesystem treats it differently.
34 boost::filesystem::path p2(buffer2);
35
36 std::cout << "p2.string().size() is " << p2.string().size() << std::endl;
37 // output: p1.string().size() is 5
38
39 delete [] buffer2;
40}