Boost C++ Libraries: Ticket #2324: Use of tmpnam may produce spurious test results https://svn.boost.org/trac10/ticket/2324 <p> As described in <a class="ext-link" href="http://www.dwheeler.com/secure-programs/Secure-Programs-HOWTO/avoid-race.html"><span class="icon">​</span>http://www.dwheeler.com/secure-programs/Secure-Programs-HOWTO/avoid-race.html</a>, tmpnam is subject to race conditions, which makes it especially bad for use in testing when testers are exploiting parallelism via threads or processes. Several files in the library are using tmpnam when they should use mkstemp. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/2324 Trac 1.4.3 Robert Ramey Tue, 16 Sep 2008 19:25:35 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/2324#comment:1 https://svn.boost.org/trac10/ticket/2324#comment:1 <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">wontfix</span> </li> </ul> <p> This is used in the serialization test programs - none of which exploit parallelism via threads nor processes. I don't remember now, but I found mkstemp unsuitable for the purpose at hand. </p> <p> Robert Ramey </p> Ticket Dave Abrahams Tue, 16 Sep 2008 21:49:48 GMT status changed; resolution deleted https://svn.boost.org/trac10/ticket/2324#comment:2 https://svn.boost.org/trac10/ticket/2324#comment:2 <ul> <li><strong>status</strong> <span class="trac-field-old">closed</span> → <span class="trac-field-new">reopened</span> </li> <li><strong>resolution</strong> <span class="trac-field-deleted">wontfix</span> </li> </ul> <p> Yes, I know it's used for testing. Whether or not tests are run in parallel is not under your control as the author of the library and its tests. A tester may run bjam with -jN and several tests will run simultaneously, and that may be a feature of the testing system or simply how the tester configures the test machine. The ability to exploit all the resources of the testing machines is extremely important to reducing our test turnaround time. </p> Ticket Robert Ramey Fri, 19 Sep 2008 23:12:56 GMT <link>https://svn.boost.org/trac10/ticket/2324#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2324#comment:3</guid> <description> <p> when I looked into using mktemp I couldn't figure out how to use it to open temporary C++ stream as on fstream(?). </p> <p> mkstemp returns a file handle, and I couldn't figure out how to initialize a stream with the handle to an already open file. I saw no portable way to avoid using tmpname. </p> <p> Robert Ramey </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Steven Watanabe</dc:creator> <pubDate>Wed, 03 Jun 2009 00:44:04 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/2324#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2324#comment:4</guid> <description> <p> See <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/590" title="#590: Feature Requests: mkstemp (closed: fixed)">#590</a>. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Robert Ramey</dc:creator> <pubDate>Sat, 06 Jun 2009 02:10:00 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/2324#comment:5 https://svn.boost.org/trac10/ticket/2324#comment:5 <ul> <li><strong>status</strong> <span class="trac-field-old">reopened</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">wontfix</span> </li> </ul> Ticket Steven Watanabe Mon, 14 Jun 2010 18:01:07 GMT status changed; resolution deleted https://svn.boost.org/trac10/ticket/2324#comment:6 https://svn.boost.org/trac10/ticket/2324#comment:6 <ul> <li><strong>status</strong> <span class="trac-field-old">closed</span> → <span class="trac-field-new">reopened</span> </li> <li><strong>resolution</strong> <span class="trac-field-deleted">wontfix</span> </li> </ul> <p> Boost.Filesystem v3 contains a unique_path function which should behave better than tmpnam, since it uses CryptGenRandom and /dev/urandom to generate the file name. Once unique_path is stable, can you switch to it? </p> Ticket Dean Michael Berris Sun, 28 Nov 2010 23:15:20 GMT <link>https://svn.boost.org/trac10/ticket/2324#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2324#comment:7</guid> <description> <p> Actually, you can use mkstemp to create the file while providing a pattern for the temporary file, and be sure that the file is there when the call returns. This means you don't need to use the file descriptor it returns and just need to use the generated filename of the temporary file afterwards. </p> <p> You can even close the file descriptor returned by mkstemp immediately to avoid leaking open file descriptors. </p> <p> I'll try to come up with the patch to illustrate what I mean by this. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Sun, 28 Nov 2010 23:58:14 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/2324#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2324#comment:8</guid> <description> <p> Scratch that, it looks like it's easier to just use Boost.Filesystem v3's unique_path. I'll work on a patch to get that done. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Robert Ramey</dc:creator> <pubDate>Mon, 29 Nov 2010 20:38:00 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/2324#comment:9 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2324#comment:9</guid> <description> <p> This looks pretty good, I'll try it next time I stick my fingers in here. </p> <p> Robert Ramey </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Robert Ramey</dc:creator> <pubDate>Mon, 06 Dec 2010 20:30:32 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/2324#comment:10 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2324#comment:10</guid> <description> <p> Did you actually test this yourself? </p> <p> When I compile with my vc 7.1 system I get </p> <p> Compiling... test_array.cpp Linking to lib file: boost_filesystem-vc71-mt-gd-1_45.lib Linking to lib file: boost_system-vc71-mt-gd-1_45.lib c:\<a class="missing wiki">BoostRelease</a>\libs\serialization\test\test_tools.hpp(96) : error C2039: 'str' : is not a member of 'boost::filesystem3::path' </p> <blockquote> <p> c:\<a class="missing wiki">BoostRelease</a>\libs\serialization\vc7ide\..\..\..\boost\filesystem\v3\path.hpp(54) : see declaration of 'boost::filesystem3::path' </p> </blockquote> <p> c:\<a class="missing wiki">BoostRelease</a>\libs\serialization\test\test_tools.hpp(96) : error C2228: left of '.c_str' must have class/struct/union type Linking to lib file: boost_serialization-vc71-mt-gd-1_45.lib Linking to lib file: boost_serialization-vc71-mt-gd-1_45.lib </p> <p> Robert Ramey </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Dean Michael Berris</dc:creator> <pubDate>Tue, 07 Dec 2010 02:20:14 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/2324#comment:11 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2324#comment:11</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/2324#comment:10" title="Comment 10">ramey</a>: </p> <blockquote class="citation"> <p> Did you actually test this yourself? </p> </blockquote> <p> Yes, but only on Linux. I'll fix the patch for windows to address the issue, please expect an update in a few minutes. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Dean Michael Berris</dc:creator> <pubDate>Tue, 07 Dec 2010 02:23:08 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/2324 https://svn.boost.org/trac10/ticket/2324 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">boost-serialization-test-avoid-tmpnam-issue2324.diff</span> </li> </ul> <p> Updated patch to fix compile failures on Windows with MSVC. </p> Ticket Robert Ramey Tue, 07 Dec 2010 16:56:32 GMT <link>https://svn.boost.org/trac10/ticket/2324#comment:12 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2324#comment:12</guid> <description> <p> I ran this with bjam and things seemed to work fine. </p> <p> I do have a few questions however. </p> <p> a) I didn't find any documentation in the boost/filesystem regarding unique_path. This concerned me somewhat but not all that much. </p> <p> b) I couldn't figure out in which directory the temporary files are created. For the windows platform, there is special code to be sure that they are created in the $TMP or $TEMP directory. But, for *nix, I relied upon the fact that tmpnam creates it's files in these temporary directories. It seems that this fix looses that that. So does this guarantee that these files are created in the $TMP directory and if not, can it be modified to guarantee this? </p> <p> Robert Ramey </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Dean Michael Berris</dc:creator> <pubDate>Wed, 08 Dec 2010 02:06:01 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/2324#comment:13 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2324#comment:13</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/2324#comment:12" title="Comment 12">ramey</a>: </p> <blockquote class="citation"> <p> I ran this with bjam and things seemed to work fine. </p> </blockquote> <p> Cool. </p> <blockquote class="citation"> <p> I do have a few questions however. </p> <p> a) I didn't find any documentation in the boost/filesystem regarding unique_path. This concerned me somewhat but not all that much. </p> </blockquote> <p> <a href="http://www.boost.org/doc/libs/1_45_0/libs/filesystem/v3/doc/reference.html#unique_path">http://www.boost.org/doc/libs/1_45_0/libs/filesystem/v3/doc/reference.html#unique_path</a> </p> <p> This is in Boost.Filesystem V3. </p> <blockquote class="citation"> <p> b) I couldn't figure out in which directory the temporary files are created. For the windows platform, there is special code to be sure that they are created in the $TMP or $TEMP directory. But, for *nix, I relied upon the fact that tmpnam creates it's files in these temporary directories. It seems that this fix looses that that. So does this guarantee that these files are created in the $TMP directory and if not, can it be modified to guarantee this? </p> </blockquote> <p> On Unix, the path in the patch would just create the file where the binary was being run. I was thinking since this was really a temporary file, that the tests would actually delete the file once they're done. </p> <p> The answer would be yes, that the pattern can be modified to create a path which actually uses the $TMP environment variable as part of the pattern, or have it hard-coded to be '/tmp/serialization-%%%%%%%%%%%%%%%%'. </p> <p> I'll check to see whether I can find a portable UNIX way of at least getting the correct temporary directory path. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Dean Michael Berris</dc:creator> <pubDate>Wed, 08 Dec 2010 03:04:49 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/2324 https://svn.boost.org/trac10/ticket/2324 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">boost-serialization-test-avoid-tmpnam-issue2324.2.diff</span> </li> </ul> <p> Updated patch to use boost::archive::tmpdir to get the correct directory for temporary storage on Unix. </p> Ticket Robert Ramey Sat, 18 Dec 2010 19:28:39 GMT status, type changed; resolution set https://svn.boost.org/trac10/ticket/2324#comment:14 https://svn.boost.org/trac10/ticket/2324#comment:14 <ul> <li><strong>status</strong> <span class="trac-field-old">reopened</span> → <span class="trac-field-new">closed</span> </li> <li><strong>type</strong> <span class="trac-field-old">Bugs</span> → <span class="trac-field-new">Feature Requests</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> Uploaded to trunk </p> Ticket