Skip to content

Commit 86874ce

Browse files
tonyliaosscopybara-github
authored andcommitted
Remove calls to tmpnam() in protobuf/testing/googletest.cc.
This came up as a pull request here: #18239. |tmpnam| is ugly and would be nice to avoid. We don't necessarily want to substitute out GetTemporaryDirectoryName with |std::filesystem::temp_directory_path| -- the current setup creates a subdirectory under the tmp directory and uses TempDirDeleter to clean it up. We want to preserve that behaviour: the current setup plays nicely with bazel without polluting /tmp with unbounded growth. #test-continuous PiperOrigin-RevId: 677843997
1 parent 6f5b35b commit 86874ce

File tree

1 file changed

+3
-38
lines changed

1 file changed

+3
-38
lines changed

src/google/protobuf/testing/googletest.cc

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include "absl/strings/str_replace.h"
2323
#include "google/protobuf/io/io_win32.h"
2424
#include "google/protobuf/testing/file.h"
25+
#include <gtest/gtest.h>
26+
2527
#ifdef _MSC_VER
2628
// #include <direct.h>
2729
#else
@@ -93,45 +95,8 @@ std::string TestSourceDir() {
9395
namespace {
9496

9597
std::string GetTemporaryDirectoryName() {
96-
// Tests run under Bazel "should not" use /tmp. Bazel sets this environment
97-
// variable for tests to use instead.
98-
char* from_environment = getenv("TEST_TMPDIR");
99-
if (from_environment != nullptr && from_environment[0] != '\0') {
100-
return absl::StrCat(from_environment, "/protobuf_tmpdir");
101-
}
102-
103-
// tmpnam() is generally not considered safe but we're only using it for
104-
// testing. We cannot use tmpfile() or mkstemp() since we're creating a
105-
// directory.
106-
char b[L_tmpnam + 1]; // HPUX multithread return 0 if s is 0
107-
#pragma GCC diagnostic push
108-
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
109-
std::string result = tmpnam(b);
110-
#pragma GCC diagnostic pop
98+
std::string result = absl::StrCat(testing::TempDir(), "protobuf_tempdir");
11199
#ifdef _WIN32
112-
// Avoid a trailing dot by changing it to an underscore. On Win32 the names of
113-
// files and directories can, but should not, end with dot.
114-
//
115-
// In MS-DOS and FAT16 filesystem the filenames were 8dot3 style so it didn't
116-
// make sense to have a name ending in dot without an extension, so the shell
117-
// silently ignored trailing dots. To this day the Win32 API still maintains
118-
// this behavior and silently ignores trailing dots in path arguments of
119-
// functions such as CreateFile{A,W}. Even POSIX API function implementations
120-
// seem to wrap the Win32 API functions (e.g. CreateDirectoryA) and behave
121-
// this way.
122-
// It's possible to avoid this behavior and create files / directories with
123-
// trailing dots (using CreateFileW / CreateDirectoryW and prefixing the path
124-
// with "\\?\") but these will be degenerate in the sense that you cannot
125-
// chdir into such directories (or navigate into them with Windows Explorer)
126-
// nor can you open such files with some programs (e.g. Notepad).
127-
if (result[result.size() - 1] == '.') {
128-
result[result.size() - 1] = '_';
129-
}
130-
// On Win32, tmpnam() returns a file prefixed with '\', but which is supposed
131-
// to be used in the current working directory. WTF?
132-
if (absl::StartsWith(result, "\\")) {
133-
result.erase(0, 1);
134-
}
135100
// The Win32 API accepts forward slashes as a path delimiter as long as the
136101
// path doesn't use the "\\?\" prefix.
137102
// Let's avoid confusion and use only forward slashes.

0 commit comments

Comments
 (0)