Fix security warning generated by std::tmpnam #106027
|
@ -17,6 +17,7 @@
|
|||
#include "BLI_string.h"
|
||||
#include "BLI_string_utf8.h"
|
||||
#include "BLI_string_utils.h"
|
||||
#include "BLI_tempfile.h"
|
||||
#include "BLI_utildefines.h"
|
||||
|
||||
#include "BKE_appdir.h" /* own include */
|
||||
|
@ -1089,7 +1090,7 @@ void BKE_appdir_app_templates(ListBase *templates)
|
|||
* Also make sure the temp dir has a trailing slash
|
||||
*
|
||||
* \param tempdir: The full path to the temporary temp directory.
|
||||
* \param tempdir_len: The size of the \a tempdir buffer.
|
||||
* \param tempdir_maxlen: The size of the \a tempdir buffer.
|
||||
* \param userdir: Directory specified in user preferences (may be NULL).
|
||||
* note that by default this is an empty string, only use when non-empty.
|
||||
*/
|
||||
|
@ -1098,37 +1099,14 @@ static void where_is_temp(char *tempdir, const size_t tempdir_maxlen, const char
|
|||
|
||||
tempdir[0] = '\0';
|
||||
|
||||
if (userdir && BLI_is_dir(userdir)) {
|
||||
if (userdir && userdir[0] != '\0' && BLI_is_dir(userdir)) {
|
||||
BLI_strncpy(tempdir, userdir, tempdir_maxlen);
|
||||
}
|
||||
|
||||
if (tempdir[0] == '\0') {
|
||||
const char *env_vars[] = {
|
||||
#ifdef WIN32
|
||||
"TEMP",
|
||||
#else
|
||||
/* Non standard (could be removed). */
|
||||
"TMP",
|
||||
/* Posix standard. */
|
||||
"TMPDIR",
|
||||
#endif
|
||||
};
|
||||
for (int i = 0; i < ARRAY_SIZE(env_vars); i++) {
|
||||
const char *tmp = BLI_getenv(env_vars[i]);
|
||||
if (tmp && (tmp[0] != '\0') && BLI_is_dir(tmp)) {
|
||||
BLI_strncpy(tempdir, tmp, tempdir_maxlen);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (tempdir[0] == '\0') {
|
||||
BLI_strncpy(tempdir, "/tmp/", tempdir_maxlen);
|
||||
}
|
||||
else {
|
||||
/* add a trailing slash if needed */
|
||||
/* Add a trailing slash if needed. */
|
||||
BLI_path_slash_ensure(tempdir, tempdir_maxlen);
|
||||
return;
|
||||
}
|
||||
|
||||
BLI_temp_directory_path_get(tempdir, tempdir_maxlen);
|
||||
}
|
||||
|
||||
static void tempdir_session_create(char *tempdir_session,
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-or-later
|
||||
* Copyright 2023 Blender Foundation. */
|
||||
|
||||
/** \file
|
||||
* \ingroup bli
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "BLI_sys_types.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Get the path to a directory suitable for temporary files.
|
||||
*
|
||||
* The return path is guaranteed to exist and to be a directory, as well as to contain a trailing
|
||||
* directory separator.
|
||||
*
|
||||
* At maximum the buffer_size number of characters is written to the temp_directory. The directory
|
||||
* path is always null-terminated. */
|
||||
void BLI_temp_directory_path_get(char *temp_directory, const size_t buffer_size);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
|
@ -147,6 +147,7 @@ set(SRC
|
|||
intern/task_pool.cc
|
||||
intern/task_range.cc
|
||||
intern/task_scheduler.cc
|
||||
intern/tempfile.c
|
||||
intern/threads.cc
|
||||
intern/time.c
|
||||
intern/timecode.c
|
||||
|
@ -347,6 +348,7 @@ set(SRC
|
|||
BLI_system.h
|
||||
BLI_task.h
|
||||
BLI_task.hh
|
||||
BLI_tempfile.h
|
||||
BLI_threads.h
|
||||
BLI_timecode.h
|
||||
BLI_timeit.hh
|
||||
|
@ -533,6 +535,7 @@ if(WITH_GTESTS)
|
|||
tests/BLI_string_utf8_test.cc
|
||||
tests/BLI_task_graph_test.cc
|
||||
tests/BLI_task_test.cc
|
||||
tests/BLI_tempfile_test.cc
|
||||
tests/BLI_uuid_test.cc
|
||||
tests/BLI_vector_set_test.cc
|
||||
tests/BLI_vector_test.cc
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-or-later
|
||||
* Copyright 2023 Blender Foundation. */
|
||||
|
||||
#include "BLI_tempfile.h"
|
||||
|
||||
#include "BLI_fileops.h"
|
||||
#include "BLI_path_util.h"
|
||||
#include "BLI_string.h"
|
||||
|
||||
void BLI_temp_directory_path_get(char *temp_directory, const size_t buffer_size)
|
||||
{
|
||||
temp_directory[0] = '\0';
|
||||
|
||||
const char *env_vars[] = {
|
||||
#ifdef WIN32
|
||||
"TEMP",
|
||||
#else
|
||||
/* Non standard (could be removed). */
|
||||
"TMP",
|
||||
/* Posix standard. */
|
||||
"TMPDIR",
|
||||
#endif
|
||||
};
|
||||
|
||||
for (int i = 0; i < ARRAY_SIZE(env_vars); i++) {
|
||||
const char *tmp = BLI_getenv(env_vars[i]);
|
||||
if (tmp && (tmp[0] != '\0') && BLI_is_dir(tmp)) {
|
||||
BLI_strncpy(temp_directory, tmp, buffer_size);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (temp_directory[0] == '\0') {
|
||||
BLI_strncpy(temp_directory, "/tmp/", buffer_size);
|
||||
}
|
||||
else {
|
||||
/* Add a trailing slash if needed. */
|
||||
BLI_path_slash_ensure(temp_directory, buffer_size);
|
||||
}
|
||||
|
||||
BLI_dir_create_recursive(temp_directory);
|
||||
}
|
|
@ -1,11 +1,16 @@
|
|||
/* SPDX-License-Identifier: Apache-2.0 */
|
||||
|
||||
#include "testing/testing.h"
|
||||
|
||||
#include "BLI_fileops.hh"
|
||||
#include "BLI_path_util.h"
|
||||
#include "BLI_string.h"
|
||||
#include "BLI_system.h"
|
||||
#include "BLI_tempfile.h"
|
||||
#include "BLI_threads.h"
|
||||
|
||||
#include BLI_SYSTEM_PID_H
|
||||
|
||||
namespace blender::tests {
|
||||
|
||||
class ChangeWorkingDirectoryTest : public testing::Test {
|
||||
|
@ -26,6 +31,20 @@ class ChangeWorkingDirectoryTest : public testing::Test {
|
|||
|
||||
BLI_threadapi_exit();
|
||||
}
|
||||
|
||||
/* Make a pseudo-unique file name file within the temp directory in a cross-platform manner. */
|
||||
static std::string make_pseudo_unique_temp_filename()
|
||||
{
|
||||
char temp_dir[FILE_MAX];
|
||||
BLI_temp_directory_path_get(temp_dir, sizeof(temp_dir));
|
||||
|
||||
const std::string directory_name = "blender_test_" + std::to_string(getpid());
|
||||
|
||||
char filepath[FILE_MAX];
|
||||
BLI_path_join(filepath, sizeof(filepath), temp_dir, directory_name.c_str());
|
||||
|
||||
return filepath;
|
||||
}
|
||||
};
|
||||
|
||||
TEST(fileops, fstream_open_string_filename)
|
||||
|
@ -71,7 +90,7 @@ TEST_F(ChangeWorkingDirectoryTest, change_working_directory)
|
|||
ASSERT_TRUE(original_cwd == original_cwd_buff)
|
||||
<< "Returned CWD path unexpectedly different than given char buffer.";
|
||||
|
||||
std::string temp_file_name(std::tmpnam(nullptr));
|
||||
std::string temp_file_name = make_pseudo_unique_temp_filename();
|
||||
test_temp_dir = temp_file_name + "_новый";
|
||||
|
||||
if (BLI_exists(test_temp_dir.c_str())) {
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
/* SPDX-License-Identifier: Apache-2.0 */
|
||||
|
||||
#include "BLI_tempfile.h"
|
||||
|
||||
#include "BLI_fileops.h"
|
||||
#include "BLI_path_util.h"
|
||||
|
||||
#include "testing/testing.h"
|
||||
|
||||
namespace blender::tests {
|
||||
|
||||
TEST(BLI_tempfile, BLI_temp_directory_path_get)
|
||||
{
|
||||
char temp_dir[FILE_MAX];
|
||||
BLI_temp_directory_path_get(temp_dir, sizeof(temp_dir));
|
||||
|
||||
ASSERT_STRNE(temp_dir, "");
|
||||
|
||||
EXPECT_EQ(temp_dir[strlen(temp_dir) - 1], SEP);
|
||||
|
||||
EXPECT_TRUE(BLI_exists(temp_dir));
|
||||
EXPECT_TRUE(BLI_is_dir(temp_dir));
|
||||
|
||||
EXPECT_TRUE(BLI_path_is_abs_from_cwd(temp_dir));
|
||||
}
|
||||
|
||||
} // namespace blender::tests
|
Loading…
Reference in New Issue