aboutsummaryrefslogtreecommitdiff
path: root/libstdc++-v3/src/filesystem
diff options
context:
space:
mode:
authorJonathan Wakely <jwakely@redhat.com>2021-07-30 13:56:14 +0100
committerJonathan Wakely <jwakely@redhat.com>2021-07-30 18:12:39 +0100
commit3dbd4d94bf380f3efa8bba9b203ce7d4c8f47fbb (patch)
tree6819bfe2d940de090ddf612866796f1c485f5773 /libstdc++-v3/src/filesystem
parent2065654435e3d97676366f82b939bc9273382dbe (diff)
downloadgcc-3dbd4d94bf380f3efa8bba9b203ce7d4c8f47fbb.zip
gcc-3dbd4d94bf380f3efa8bba9b203ce7d4c8f47fbb.tar.gz
gcc-3dbd4d94bf380f3efa8bba9b203ce7d4c8f47fbb.tar.bz2
libstdc++: Use secure_getenv for filesystem::temp_directory_path() [PR65018]
This adds a configure check for the GNU extension secure_getenv and then uses it for looking up TMPDIR and similar variables. Signed-off-by: Jonathan Wakely <jwakely@redhat.com> libstdc++-v3/ChangeLog: PR libstdc++/65018 * configure.ac: Check for secure_getenv. * config.h.in: Regenerate. * configure: Regenerate. * src/filesystem/ops-common.h (get_temp_directory_from_env): New helper function to obtain path from the environment. * src/c++17/fs_ops.cc (fs::temp_directory_path): Use new helper. * src/filesystem/ops.cc (fs::temp_directory_path): Likewise. * testsuite/27_io/filesystem/operations/temp_directory_path.cc: Print messages if test cannot be run. * testsuite/experimental/filesystem/operations/temp_directory_path.cc: Likewise. Fix incorrect condition. Use "TMP" to work with Windows as well as POSIX.
Diffstat (limited to 'libstdc++-v3/src/filesystem')
-rw-r--r--libstdc++-v3/src/filesystem/ops-common.h40
-rw-r--r--libstdc++-v3/src/filesystem/ops.cc32
2 files changed, 45 insertions, 27 deletions
diff --git a/libstdc++-v3/src/filesystem/ops-common.h b/libstdc++-v3/src/filesystem/ops-common.h
index 43311e6..b8bbf44 100644
--- a/libstdc++-v3/src/filesystem/ops-common.h
+++ b/libstdc++-v3/src/filesystem/ops-common.h
@@ -568,6 +568,46 @@ _GLIBCXX_BEGIN_NAMESPACE_FILESYSTEM
#endif // _GLIBCXX_HAVE_SYS_STAT_H
+ // Find OS-specific name of temporary directory from the environment,
+ // Caller must check that the path is an accessible directory.
+#ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
+ inline wstring
+ get_temp_directory_from_env()
+ {
+ unsigned len = 1024;
+ std::wstring buf;
+ do
+ {
+ buf.resize(len);
+ len = GetTempPathW(buf.size(), buf.data());
+ } while (len > buf.size());
+
+ if (len == 0)
+ {
+ ec.assign((int)GetLastError(), std::system_category());
+ return p;
+ }
+ buf.resize(len);
+ return buf;
+ }
+#else
+ inline const char*
+ get_temp_directory_from_env() noexcept
+ {
+ for (auto env : { "TMPDIR", "TMP", "TEMP", "TEMPDIR" })
+ {
+#if _GLIBCXX_HAVE_SECURE_GETENV
+ auto tmpdir = ::secure_getenv(env);
+#else
+ auto tmpdir = ::getenv(env);
+#endif
+ if (tmpdir)
+ return tmpdir;
+ }
+ return "/tmp";
+ }
+#endif
+
_GLIBCXX_END_NAMESPACE_FILESYSTEM
_GLIBCXX_END_NAMESPACE_VERSION
diff --git a/libstdc++-v3/src/filesystem/ops.cc b/libstdc++-v3/src/filesystem/ops.cc
index 7c5b164..3494cbd 100644
--- a/libstdc++-v3/src/filesystem/ops.cc
+++ b/libstdc++-v3/src/filesystem/ops.cc
@@ -1289,7 +1289,8 @@ fs::system_complete(const path& p, error_code& ec)
#endif
}
-fs::path fs::temp_directory_path()
+fs::path
+fs::temp_directory_path()
{
error_code ec;
path tmp = temp_directory_path(ec);
@@ -1298,31 +1299,10 @@ fs::path fs::temp_directory_path()
return tmp;
}
-fs::path fs::temp_directory_path(error_code& ec)
+fs::path
+fs::temp_directory_path(error_code& ec)
{
- path p;
-#ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
- unsigned len = 1024;
- std::wstring buf;
- do
- {
- buf.resize(len);
- len = GetTempPathW(buf.size(), buf.data());
- } while (len > buf.size());
-
- if (len == 0)
- {
- ec.assign((int)GetLastError(), std::system_category());
- return p;
- }
- buf.resize(len);
- p = std::move(buf);
-#else
- const char* tmpdir = nullptr;
- const char* env[] = { "TMPDIR", "TMP", "TEMP", "TEMPDIR", nullptr };
- for (auto e = env; tmpdir == nullptr && *e != nullptr; ++e)
- tmpdir = ::getenv(*e);
- p = tmpdir ? tmpdir : "/tmp";
+ path p = fs::get_temp_directory_from_env();
auto st = status(p, ec);
if (ec)
p.clear();
@@ -1331,7 +1311,5 @@ fs::path fs::temp_directory_path(error_code& ec)
p.clear();
ec = std::make_error_code(std::errc::not_a_directory);
}
-#endif
return p;
}
-