summaryrefslogtreecommitdiffstats
path: root/googletest/src
diff options
context:
space:
mode:
authorAbseil Team <absl-team@google.com>2023-02-15 23:21:19 (GMT)
committerCopybara-Service <copybara-worker@google.com>2023-02-15 23:22:10 (GMT)
commitf063cd25c90cbd4089a0ff96f5991df4f2721338 (patch)
tree5bad6a1e75e225fbac4d79058c13fe05ef78e151 /googletest/src
parent2057566e4e16c88f1fea4d6c96b2e2bfb87507a6 (diff)
downloadgoogletest-f063cd25c90cbd4089a0ff96f5991df4f2721338.zip
googletest-f063cd25c90cbd4089a0ff96f5991df4f2721338.tar.gz
googletest-f063cd25c90cbd4089a0ff96f5991df4f2721338.tar.bz2
Remove strdup usage
PiperOrigin-RevId: 509947007 Change-Id: I31e1274afa889776829c877c40c9af589298dcf2
Diffstat (limited to 'googletest/src')
-rw-r--r--googletest/src/gtest-death-test.cc94
-rw-r--r--googletest/src/gtest-port.cc42
2 files changed, 42 insertions, 94 deletions
diff --git a/googletest/src/gtest-death-test.cc b/googletest/src/gtest-death-test.cc
index b6968a9..7fcf9bf 100644
--- a/googletest/src/gtest-death-test.cc
+++ b/googletest/src/gtest-death-test.cc
@@ -33,7 +33,9 @@
#include "gtest/gtest-death-test.h"
#include <functional>
+#include <memory>
#include <utility>
+#include <vector>
#include "gtest/internal/custom/gtest.h"
#include "gtest/internal/gtest-port.h"
@@ -621,6 +623,17 @@ bool DeathTestImpl::Passed(bool status_ok) {
return success;
}
+// Note: The return value points into args, so the return value's lifetime is
+// bound to that of args.
+std::unique_ptr<char*[]> CreateArgvFromArgs(std::vector<std::string>& args) {
+ auto result = std::make_unique<char*[]>(args.size() + 1);
+ for (size_t i = 0; i < args.size(); ++i) {
+ result[i] = &args[i][0];
+ }
+ result[args.size()] = nullptr; // extra null terminator
+ return result;
+}
+
#if GTEST_OS_WINDOWS
// WindowsDeathTest implements death tests on Windows. Due to the
// specifics of starting new processes on Windows, death tests there are
@@ -836,36 +849,6 @@ class FuchsiaDeathTest : public DeathTestImpl {
zx::socket stderr_socket_;
};
-// Utility class for accumulating command-line arguments.
-class Arguments {
- public:
- Arguments() { args_.push_back(nullptr); }
-
- ~Arguments() {
- for (std::vector<char*>::iterator i = args_.begin(); i != args_.end();
- ++i) {
- free(*i);
- }
- }
- void AddArgument(const char* argument) {
- args_.insert(args_.end() - 1, posix::StrDup(argument));
- }
-
- template <typename Str>
- void AddArguments(const ::std::vector<Str>& arguments) {
- for (typename ::std::vector<Str>::const_iterator i = arguments.begin();
- i != arguments.end(); ++i) {
- args_.insert(args_.end() - 1, posix::StrDup(i->c_str()));
- }
- }
- char* const* Argv() { return &args_[0]; }
-
- int size() { return static_cast<int>(args_.size()) - 1; }
-
- private:
- std::vector<char*> args_;
-};
-
// Waits for the child in a death test to exit, returning its exit
// status, or 0 if no child process exists. As a side effect, sets the
// outcome data member.
@@ -986,10 +969,10 @@ DeathTest::TestRole FuchsiaDeathTest::AssumeRole() {
kInternalRunDeathTestFlag + "=" + file_ +
"|" + StreamableToString(line_) + "|" +
StreamableToString(death_test_index);
- Arguments args;
- args.AddArguments(GetInjectableArgvs());
- args.AddArgument(filter_flag.c_str());
- args.AddArgument(internal_flag.c_str());
+
+ std::vector<std::string> args = GetInjectableArgvs();
+ args.push_back(filter_flag);
+ args.push_back(internal_flag);
// Build the pipe for communication with the child.
zx_status_t status;
@@ -1041,8 +1024,9 @@ DeathTest::TestRole FuchsiaDeathTest::AssumeRole() {
GTEST_DEATH_TEST_CHECK_(status == ZX_OK);
// Spawn the child process.
- status = fdio_spawn_etc(child_job, FDIO_SPAWN_CLONE_ALL, args.Argv()[0],
- args.Argv(), nullptr, 2, spawn_actions,
+ std::unique_ptr<char*[]> argv = CreateArgvFromArgs(args);
+ status = fdio_spawn_etc(child_job, FDIO_SPAWN_CLONE_ALL, argv[0], argv.get(),
+ nullptr, 2, spawn_actions,
child_process_.reset_and_get_address(), nullptr);
GTEST_DEATH_TEST_CHECK_(status == ZX_OK);
@@ -1173,34 +1157,6 @@ class ExecDeathTest : public ForkingDeathTest {
const int line_;
};
-// Utility class for accumulating command-line arguments.
-class Arguments {
- public:
- Arguments() { args_.push_back(nullptr); }
-
- ~Arguments() {
- for (std::vector<char*>::iterator i = args_.begin(); i != args_.end();
- ++i) {
- free(*i);
- }
- }
- void AddArgument(const char* argument) {
- args_.insert(args_.end() - 1, posix::StrDup(argument));
- }
-
- template <typename Str>
- void AddArguments(const ::std::vector<Str>& arguments) {
- for (typename ::std::vector<Str>::const_iterator i = arguments.begin();
- i != arguments.end(); ++i) {
- args_.insert(args_.end() - 1, posix::StrDup(i->c_str()));
- }
- }
- char* const* Argv() { return &args_[0]; }
-
- private:
- std::vector<char*> args_;
-};
-
// A struct that encompasses the arguments to the child process of a
// threadsafe-style death test process.
struct ExecDeathTestArgs {
@@ -1410,10 +1366,9 @@ DeathTest::TestRole ExecDeathTest::AssumeRole() {
StreamableToString(line_) + "|" +
StreamableToString(death_test_index) + "|" +
StreamableToString(pipe_fd[1]);
- Arguments args;
- args.AddArguments(GetArgvsForDeathTestChildProcess());
- args.AddArgument(filter_flag.c_str());
- args.AddArgument(internal_flag.c_str());
+ std::vector<std::string> args = GetArgvsForDeathTestChildProcess();
+ args.push_back(filter_flag);
+ args.push_back(internal_flag);
DeathTest::set_last_death_test_message("");
@@ -1422,7 +1377,8 @@ DeathTest::TestRole ExecDeathTest::AssumeRole() {
// is necessary.
FlushInfoLog();
- const pid_t child_pid = ExecDeathTestSpawnChild(args.Argv(), pipe_fd[0]);
+ std::unique_ptr<char*[]> argv = CreateArgvFromArgs(args);
+ const pid_t child_pid = ExecDeathTestSpawnChild(argv.get(), pipe_fd[0]);
GTEST_DEATH_TEST_CHECK_SYSCALL_(close(pipe_fd[1]));
set_child_pid(child_pid);
set_read_fd(pipe_fd[0]);
diff --git a/googletest/src/gtest-port.cc b/googletest/src/gtest-port.cc
index d797fe4..105a3ec 100644
--- a/googletest/src/gtest-port.cc
+++ b/googletest/src/gtest-port.cc
@@ -668,7 +668,6 @@ RE::~RE() {
regfree(&partial_regex_);
regfree(&full_regex_);
}
- free(const_cast<char*>(pattern_));
}
// Returns true if and only if regular expression re matches the entire str.
@@ -690,7 +689,7 @@ bool RE::PartialMatch(const char* str, const RE& re) {
// Initializes an RE from its string representation.
void RE::Init(const char* regex) {
- pattern_ = posix::StrDup(regex);
+ pattern_ = regex;
// Reserves enough bytes to hold the regular expression used for a
// full match.
@@ -920,27 +919,26 @@ bool MatchRegexAnywhere(const char* regex, const char* str) {
// Implements the RE class.
-RE::~RE() {
- free(const_cast<char*>(pattern_));
- free(const_cast<char*>(full_pattern_));
-}
+RE::~RE() = default;
// Returns true if and only if regular expression re matches the entire str.
bool RE::FullMatch(const char* str, const RE& re) {
- return re.is_valid_ && MatchRegexAnywhere(re.full_pattern_, str);
+ return re.is_valid_ && MatchRegexAnywhere(re.full_pattern_.c_str(), str);
}
// Returns true if and only if regular expression re matches a substring of
// str (including str itself).
bool RE::PartialMatch(const char* str, const RE& re) {
- return re.is_valid_ && MatchRegexAnywhere(re.pattern_, str);
+ return re.is_valid_ && MatchRegexAnywhere(re.pattern_.c_str(), str);
}
// Initializes an RE from its string representation.
void RE::Init(const char* regex) {
- pattern_ = full_pattern_ = nullptr;
+ full_pattern_.clear();
+ pattern_.clear();
+
if (regex != nullptr) {
- pattern_ = posix::StrDup(regex);
+ pattern_ = regex;
}
is_valid_ = ValidateRegex(regex);
@@ -949,25 +947,19 @@ void RE::Init(const char* regex) {
return;
}
- const size_t len = strlen(regex);
// Reserves enough bytes to hold the regular expression used for a
- // full match: we need space to prepend a '^', append a '$', and
- // terminate the string with '\0'.
- char* buffer = static_cast<char*>(malloc(len + 3));
- full_pattern_ = buffer;
+ // full match: we need space to prepend a '^' and append a '$'.
+ full_pattern_.reserve(pattern_.size() + 2);
- if (*regex != '^')
- *buffer++ = '^'; // Makes sure full_pattern_ starts with '^'.
-
- // We don't use snprintf or strncpy, as they trigger a warning when
- // compiled with VC++ 8.0.
- memcpy(buffer, regex, len);
- buffer += len;
+ if (pattern_.empty() || pattern_.front() != '^') {
+ full_pattern_.push_back('^'); // Makes sure full_pattern_ starts with '^'.
+ }
- if (len == 0 || regex[len - 1] != '$')
- *buffer++ = '$'; // Makes sure full_pattern_ ends with '$'.
+ full_pattern_.append(pattern_);
- *buffer = '\0';
+ if (pattern_.empty() || pattern_.back() != '$') {
+ full_pattern_.push_back('$'); // Makes sure full_pattern_ ends with '$'.
+ }
}
#endif // GTEST_USES_POSIX_RE