mirror of
https://github.com/transmission/transmission.git
synced 2026-04-19 08:20:22 +01:00
fix: env var leak in tr_spawn_async() (#2212)
* fix: env var leak in tr_spawn_async()
This commit is contained in:
@@ -87,14 +87,10 @@ TEST_P(SubprocessTest, SpawnAsyncMissingExec)
|
||||
{
|
||||
auto const missing_exe_path = std::string{ TR_IF_WIN32("C:\\", "/") "tr-missing-test-exe" TR_IF_WIN32(".exe", "") };
|
||||
|
||||
auto args = std::array<char*, 2>{
|
||||
// FIXME(ckerr): remove tr_strdup()s after https://github.com/transmission/transmission/issues/1384
|
||||
tr_strdup(missing_exe_path.data()),
|
||||
nullptr
|
||||
};
|
||||
auto args = std::array<char const*, 2>{ missing_exe_path.data(), nullptr };
|
||||
|
||||
tr_error* error = nullptr;
|
||||
auto const ret = tr_spawn_async(args.data(), nullptr, nullptr, &error);
|
||||
auto const ret = tr_spawn_async(std::data(args), {}, nullptr, &error);
|
||||
EXPECT_FALSE(ret);
|
||||
EXPECT_NE(nullptr, error);
|
||||
EXPECT_NE(0, error->code);
|
||||
@@ -113,20 +109,17 @@ TEST_P(SubprocessTest, SpawnAsyncArgs)
|
||||
auto const test_arg3 = std::string{};
|
||||
auto const test_arg4 = std::string{ "\"arg3'^! $PATH %PATH% \\" };
|
||||
|
||||
auto args = std::array<char*, 8>{
|
||||
// FIXME(ckerr): remove tr_strdup()s after https://github.com/transmission/transmission/issues/1384
|
||||
tr_strdup(self_path_.c_str()),
|
||||
tr_strdup(result_path.data()),
|
||||
tr_strdup(arg_dump_args_.data()),
|
||||
tr_strdup(test_arg1.data()),
|
||||
tr_strdup(test_arg2.data()),
|
||||
tr_strdup(test_arg3.data()),
|
||||
tr_strdup(allow_batch_metachars ? test_arg4.data() : nullptr),
|
||||
nullptr
|
||||
};
|
||||
auto const args = std::array<char const*, 8>{ self_path_.c_str(),
|
||||
result_path.data(),
|
||||
arg_dump_args_.data(),
|
||||
test_arg1.data(),
|
||||
test_arg2.data(),
|
||||
test_arg3.data(),
|
||||
allow_batch_metachars ? test_arg4.data() : nullptr,
|
||||
nullptr };
|
||||
|
||||
tr_error* error = nullptr;
|
||||
bool const ret = tr_spawn_async(args.data(), nullptr, nullptr, &error);
|
||||
bool const ret = tr_spawn_async(std::data(args), {}, nullptr, &error);
|
||||
EXPECT_TRUE(ret) << args[0] << ' ' << args[1];
|
||||
EXPECT_EQ(nullptr, error) << error->code << ", " << error->message;
|
||||
|
||||
@@ -183,34 +176,31 @@ TEST_P(SubprocessTest, SpawnAsyncEnv)
|
||||
auto const test_env_value4 = std::string{ "bar" };
|
||||
auto const test_env_value5 = std::string{ "jar" };
|
||||
|
||||
auto args = std::array<char*, 10>{
|
||||
// FIXME(ckerr): remove tr_strdup()s after https://github.com/transmission/transmission/issues/1384
|
||||
tr_strdup(self_path_.c_str()), //
|
||||
tr_strdup(result_path.data()), //
|
||||
tr_strdup(arg_dump_env_.data()), //
|
||||
tr_strdup(test_env_key1.data()), //
|
||||
tr_strdup(test_env_key2.data()), //
|
||||
tr_strdup(test_env_key3.data()), //
|
||||
tr_strdup(test_env_key4.data()), //
|
||||
tr_strdup(test_env_key5.data()), //
|
||||
tr_strdup(test_env_key6.data()), //
|
||||
auto args = std::array<char const*, 10>{
|
||||
self_path_.c_str(), //
|
||||
result_path.data(), //
|
||||
arg_dump_env_.data(), //
|
||||
test_env_key1.data(), //
|
||||
test_env_key2.data(), //
|
||||
test_env_key3.data(), //
|
||||
test_env_key4.data(), //
|
||||
test_env_key5.data(), //
|
||||
test_env_key6.data(), //
|
||||
nullptr, //
|
||||
};
|
||||
|
||||
auto env = std::array<char*, 5>{
|
||||
// FIXME(ckerr): remove tr_strdup()s after https://github.com/transmission/transmission/issues/1384
|
||||
tr_strdup_printf("%s=%s", test_env_key1.data(), test_env_value1.data()),
|
||||
tr_strdup_printf("%s=%s", test_env_key2.data(), test_env_value2.data()),
|
||||
tr_strdup_printf("%s=%s", test_env_key3.data(), test_env_value3.data()),
|
||||
tr_strdup_printf("%s=%s", test_env_key5.data(), test_env_value5.data()),
|
||||
nullptr,
|
||||
auto const env = std::map<std::string_view, std::string_view>{
|
||||
{ test_env_key1, test_env_value1 },
|
||||
{ test_env_key2, test_env_value2 },
|
||||
{ test_env_key3, test_env_value3 },
|
||||
{ test_env_key5, test_env_value5 },
|
||||
};
|
||||
|
||||
setenv("FOO", "bar", true); // inherited
|
||||
setenv("ZOO", "tar", true); // overridden
|
||||
|
||||
tr_error* error = nullptr;
|
||||
bool const ret = tr_spawn_async(args.data(), env.data(), nullptr, &error);
|
||||
bool const ret = tr_spawn_async(std::data(args), env, nullptr, &error);
|
||||
EXPECT_TRUE(ret);
|
||||
EXPECT_EQ(nullptr, error);
|
||||
|
||||
@@ -248,11 +238,6 @@ TEST_P(SubprocessTest, SpawnAsyncEnv)
|
||||
EXPECT_FALSE(tr_sys_file_read_line(fd, buffer.data(), buffer.size(), nullptr));
|
||||
|
||||
tr_sys_file_close(fd, nullptr);
|
||||
|
||||
for (auto& env_item : env)
|
||||
{
|
||||
tr_free(env_item);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_P(SubprocessTest, SpawnAsyncCwdExplicit)
|
||||
@@ -260,16 +245,10 @@ TEST_P(SubprocessTest, SpawnAsyncCwdExplicit)
|
||||
auto const test_dir = sandbox_.path();
|
||||
auto const result_path = buildSandboxPath("result.txt");
|
||||
|
||||
auto args = std::array<char*, 4>{
|
||||
// FIXME(ckerr): remove tr_strdup()s after https://github.com/transmission/transmission/issues/1384
|
||||
tr_strdup(self_path_.c_str()),
|
||||
tr_strdup(result_path.data()),
|
||||
tr_strdup(arg_dump_cwd_.data()),
|
||||
nullptr
|
||||
};
|
||||
auto const args = std::array<char const*, 4>{ self_path_.c_str(), result_path.data(), arg_dump_cwd_.data(), nullptr };
|
||||
|
||||
tr_error* error = nullptr;
|
||||
bool const ret = tr_spawn_async(args.data(), nullptr, test_dir.c_str(), &error);
|
||||
bool const ret = tr_spawn_async(std::data(args), {}, test_dir.c_str(), &error);
|
||||
EXPECT_TRUE(ret);
|
||||
EXPECT_EQ(nullptr, error);
|
||||
|
||||
@@ -294,16 +273,10 @@ TEST_P(SubprocessTest, SpawnAsyncCwdInherit)
|
||||
auto const result_path = buildSandboxPath("result.txt");
|
||||
auto const expected_cwd = nativeCwd();
|
||||
|
||||
auto args = std::array<char*, 4>{
|
||||
// FIXME(ckerr): remove tr_strdup()s after https://github.com/transmission/transmission/issues/1384
|
||||
tr_strdup(self_path_.c_str()),
|
||||
tr_strdup(result_path.data()),
|
||||
tr_strdup(arg_dump_cwd_.data()),
|
||||
nullptr
|
||||
};
|
||||
auto const args = std::array<char const*, 4>{ self_path_.c_str(), result_path.data(), arg_dump_cwd_.data(), nullptr };
|
||||
|
||||
tr_error* error = nullptr;
|
||||
auto const ret = tr_spawn_async(args.data(), nullptr, nullptr, &error);
|
||||
auto const ret = tr_spawn_async(std::data(args), {}, nullptr, &error);
|
||||
EXPECT_TRUE(ret);
|
||||
EXPECT_EQ(nullptr, error);
|
||||
|
||||
@@ -323,16 +296,10 @@ TEST_P(SubprocessTest, SpawnAsyncCwdMissing)
|
||||
{
|
||||
auto const result_path = buildSandboxPath("result.txt");
|
||||
|
||||
auto args = std::array<char*, 4>{
|
||||
// FIXME(ckerr): remove tr_strdup()s after https://github.com/transmission/transmission/issues/1384
|
||||
tr_strdup(self_path_.c_str()),
|
||||
tr_strdup(result_path.data()),
|
||||
tr_strdup(arg_dump_cwd_.data()),
|
||||
nullptr
|
||||
};
|
||||
auto const args = std::array<char const*, 4>{ self_path_.c_str(), result_path.data(), arg_dump_cwd_.data(), nullptr };
|
||||
|
||||
tr_error* error = nullptr;
|
||||
auto const ret = tr_spawn_async(args.data(), nullptr, TR_IF_WIN32("C:\\", "/") "tr-missing-test-work-dir", &error);
|
||||
auto const ret = tr_spawn_async(std::data(args), {}, TR_IF_WIN32("C:\\", "/") "tr-missing-test-work-dir", &error);
|
||||
EXPECT_FALSE(ret);
|
||||
EXPECT_NE(nullptr, error);
|
||||
EXPECT_NE(0, error->code);
|
||||
|
||||
Reference in New Issue
Block a user