Boost C++ Libraries: Ticket #4576: Filesystem v3 does not respect NULL-terminated C strings whose size is known at compile time https://svn.boost.org/trac10/ticket/4576 <p> If a <code>boost::filesystem</code> version 3 <code>path</code> is constructed from a <code>char*</code> (or <code>wchar_t*</code>) buffer that has a size known at compile time, the entire buffer, except for the very last character, is appended to the path. </p> <p> If, however, the buffer is dynamically allocated, then the NULL-terminator is respected. </p> <p> If you construct a <code>path</code> from a string literal, it will work, however it is not clear whether it works correctly by design, or because the final character (the NULL-terminator) is dropped: </p> <pre class="wiki">boost::filesystem::path("./foo"); </pre><p> If you construct it from a buffer whose size is known at compile time, the path unexpectedly contains <em>the entire buffer</em> (minus the final character) -- the NULL-terminator is ignored: </p> <pre class="wiki">char buffer[255] = "./foo"; boost::filesystem::path p(buffer); assert(p.string().size() == 254); // should be 5! </pre><p> If you construct it from a buffer which is dynamically allocated, the NULL-terminator is respected: </p> <pre class="wiki">char *buffer2 = new char[255]; std::fill(buffer2, buffer2 + 255, '\0'); strcpy(buffer2, "./foo"); // N.B: This buffer is now identical to the buffer in the example above, // but boost::filesystem treats it differently. boost::filesystem::path p2(buffer2); assert(p2.string().size() == 5); delete [] buffer2; </pre><p> This is a very serious problem, as it's common to use a fixed-length buffer to retrieve paths from OS APIs (e.g. <code>MAX_PATH</code> and <code>SHGetFolderPath()</code> in Windows). If a programmer instantiates a <code>filesystem::path</code> from that buffer, they will get unexpected results. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/4576 Trac 1.4.3 Nate Silva <nate@…> Thu, 19 Aug 2010 23:44:14 GMT attachment set https://svn.boost.org/trac10/ticket/4576 https://svn.boost.org/trac10/ticket/4576 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">filesystem-buffer-size-problem.cpp</span> </li> </ul> <p> Test case showing the problem. </p> Ticket Nate Silva <nate@…> Thu, 19 Aug 2010 23:44:57 GMT <link>https://svn.boost.org/trac10/ticket/4576#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4576#comment:1</guid> <description> <p> Also should mention that I’ve tested this on Visual C++ 10 (Windows 7) and on gcc 4.2.1 (OS X 10.6). </p> </description> <category>Ticket</category> </item> <item> <author>Nate Silva <nate@…></author> <pubDate>Fri, 20 Aug 2010 00:06:07 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/4576 https://svn.boost.org/trac10/ticket/4576 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">unexpected-results.cpp</span> </li> </ul> <p> Example showing unexpected results (Windows-specific) </p> Ticket Beman Dawes Fri, 20 Aug 2010 01:22:01 GMT <link>https://svn.boost.org/trac10/ticket/4576#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4576#comment:2</guid> <description> <p> I suspect this is the problem fixed by changeset 64928. </p> <p> See <a class="ext-link" href="http://svn.boost.org/trac/boost/changeset/64928"><span class="icon">​</span>http://svn.boost.org/trac/boost/changeset/64928</a> </p> <p> Does the change to path_traits.hpp solve your problem? </p> <p> --Beman </p> </description> <category>Ticket</category> </item> <item> <author>Nate Silva <nate@…></author> <pubDate>Fri, 20 Aug 2010 03:41:40 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4576#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4576#comment:3</guid> <description> <p> Yes, that does solve the problem on OS X. I will test with Windows tomorrow. </p> <p> Regards, Nate </p> </description> <category>Ticket</category> </item> <item> <author>Nate Silva <nate@…></author> <pubDate>Fri, 20 Aug 2010 15:40:44 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4576#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4576#comment:4</guid> <description> <p> And, this solves the problem on Windows as well. </p> <p> Problem is fixed as far as I'm concerned. </p> <p> Regards,<br /> Nate </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Beman Dawes</dc:creator> <pubDate>Tue, 24 Aug 2010 15:35:54 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/4576#comment:5 https://svn.boost.org/trac10/ticket/4576#comment:5 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> Ticket