Ticket #2324: boost-serialization-test-avoid-tmpnam-issue2324.2.diff

File boost-serialization-test-avoid-tmpnam-issue2324.2.diff, 2.6 KB (added by Dean Michael Berris, 12 years ago)

Updated patch to use boost::archive::tmpdir to get the correct directory for temporary storage on Unix.

  • test_tools.hpp

     
    11#ifndef BOOST_SERIALIZATION_TEST_TOOLS_HPP
    22#define BOOST_SERIALIZATION_TEST_TOOLS_HPP
    33
     4#define BOOST_FILESYSTEM_VERSION 3
     5#include <boost/filesystem.hpp>
     6
    47// MS compatible compilers support #pragma once
    58#if defined(_MSC_VER) && (_MSC_VER >= 1020)
    69# pragma once
     
    1619
    1720//  See http://www.boost.org for updates, documentation, and revision history.
    1821
    19 #include <cstdio> // remove, tmpnam
    2022#include <cstddef> // size_t
    2123#ifndef BOOST_NO_EXCEPTION_STD_NAMESPACE
    2224    #include <exception>
     
    8789            char old_dir[256];
    8890            _getcwd(old_dir, sizeof(old_dir) - 1);
    8991            chdir(dir);
    90             std::tmpnam(ibuffer + i);
     92            // (C) Copyright 2010 Dean Michael Berris. <mikhailberis@gmail.com>
     93            // Instead of using std::tmpnam, we use Boost.Filesystem's unique_path
     94            boost::filesystem::path tmp_path =
     95                boost::filesystem::unique_path("%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
     96            std::strcat(ibuffer, tmp_path.string().c_str());
    9197            chdir(old_dir);
    9298        }
    9399        else{
     
    118124 
    119125namespace boost {
    120126namespace archive {
    121     using ::tempnam;
     127    using ::tmpnam;
    122128} // archive
    123129} // boost
    124130
    125131#else // defined(__hpux)
    126132
     133// (C) Copyright 2010 Dean Michael Berris.
     134// Instead of using the potentially dangrous tempnam function that's part
     135// of the C standard library, on Unix/Linux we use the more portable and
     136// "safe" unique_path function provided in the Boost.Filesystem library.
     137
     138#include <boost/archive/tmpdir.hpp>
     139
    127140namespace boost {
    128141namespace archive {
    129     using std::tmpnam;
     142    char const * tmpnam(char * buffer) {
     143        static char name[512] = {0};
     144        if (name[0] == 0) {
     145            boost::filesystem::path tempdir(tmpdir());
     146            boost::filesystem::path tempfilename =
     147                boost::filesystem::unique_path("serialization-%%%%%%%%%%%%%%%%");
     148            boost::filesystem::path temp = tempdir / tempfilename;
     149            std::strcat(name, temp.string().c_str());
     150        }
     151        if (buffer != 0) std::strcpy(buffer, name);
     152        return name;
     153    }
    130154} // archive
    131155} // boost
    132156
  • Jamfile.v2

     
    88
    99project libs/serialization/test
    1010    : id serialization_test
     11    : requirements <source>/boost/filesystem
    1112    ;
    1213
    1314# import rules from the boost serialization test