Skip to content

Commit

Permalink
Use _Exit instead of _exit in GoogleTest
Browse files Browse the repository at this point in the history
_Exit is standardized since C99, whereas _exit is POSIX-only.

Fixes: #4447
PiperOrigin-RevId: 605000352
Change-Id: Ibfa84edaa043bd003a21383e8148bf45be7217f6
  • Loading branch information
Abseil Team authored and copybara-github committed Feb 7, 2024
1 parent 64be1c7 commit 96519a4
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 45 deletions.
2 changes: 1 addition & 1 deletion googlemock/src/gmock-spec-builders.cc
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ class MockObjectRegistry {
#ifdef GTEST_OS_QURT
qurt_exception_raise_fatal();
#else
_exit(1); // We cannot call exit() as it is not reentrant and
_Exit(1); // We cannot call exit() as it is not reentrant and
// may already have been called.
#endif
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \
//
// exit status: The integer exit information in the format specified
// by wait(2)
// exit code: The integer code passed to exit(3), _exit(2), or
// exit code: The integer code passed to exit(3), _Exit(2), or
// returned from main()
class GTEST_API_ DeathTest {
public:
Expand Down
14 changes: 8 additions & 6 deletions googletest/src/gtest-death-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

#include "gtest/gtest-death-test.h"

#include <stdlib.h>

#include <functional>
#include <memory>
#include <sstream>
Expand Down Expand Up @@ -115,7 +117,7 @@ GTEST_DEFINE_string_(
GTEST_DEFINE_bool_(
death_test_use_fork,
testing::internal::BoolFromGTestEnv("death_test_use_fork", false),
"Instructs to use fork()/_exit() instead of clone() in death tests. "
"Instructs to use fork()/_Exit() instead of clone() in death tests. "
"Ignored and always uses fork() on POSIX systems where clone() is not "
"implemented. Useful when running under valgrind or similar tools if "
"those do not support clone(). Valgrind 3.3.1 will just fail if "
Expand Down Expand Up @@ -299,7 +301,7 @@ enum DeathTestOutcome { IN_PROGRESS, DIED, LIVED, RETURNED, THREW };
fputc(kDeathTestInternalError, parent);
fprintf(parent, "%s", message.c_str());
fflush(parent);
_exit(1);
_Exit(1);
} else {
fprintf(stderr, "%s", message.c_str());
fflush(stderr);
Expand Down Expand Up @@ -511,7 +513,7 @@ std::string DeathTestImpl::GetErrorLogs() { return GetCapturedStderr(); }
// Signals that the death test code which should have exited, didn't.
// Should be called only in a death test child process.
// Writes a status byte to the child's status file descriptor, then
// calls _exit(1).
// calls _Exit(1).
void DeathTestImpl::Abort(AbortReason reason) {
// The parent process considers the death test to be a failure if
// it finds any data in our pipe. So, here we write a single flag byte
Expand All @@ -523,13 +525,13 @@ void DeathTestImpl::Abort(AbortReason reason) {
GTEST_DEATH_TEST_CHECK_SYSCALL_(posix::Write(write_fd(), &status_ch, 1));
// We are leaking the descriptor here because on some platforms (i.e.,
// when built as Windows DLL), destructors of global objects will still
// run after calling _exit(). On such systems, write_fd_ will be
// run after calling _Exit(). On such systems, write_fd_ will be
// indirectly closed from the destructor of UnitTestImpl, causing double
// close if it is also closed here. On debug configurations, double close
// may assert. As there are no in-process buffers to flush here, we are
// relying on the OS to close the descriptor after the process terminates
// when the destructors are not run.
_exit(1); // Exits w/o any normal exit hooks (we were supposed to crash)
_Exit(1); // Exits w/o any normal exit hooks (we were supposed to crash)
}

// Returns an indented copy of stderr output for a death test.
Expand Down Expand Up @@ -1333,7 +1335,7 @@ static pid_t ExecDeathTestSpawnChild(char* const* argv, int close_fd) {
#endif // GTEST_HAS_CLONE

if (use_fork && (child_pid = fork()) == 0) {
_exit(ExecDeathTestChildMain(&args));
_Exit(ExecDeathTestChildMain(&args));
}
#endif // GTEST_OS_QNX
#ifdef GTEST_OS_LINUX
Expand Down
76 changes: 39 additions & 37 deletions googletest/test/googletest-death-test-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
//
// Tests for death tests.

#include <stdlib.h>

#include "gtest/gtest-death-test.h"
#include "gtest/gtest.h"
#include "gtest/internal/gtest-filepath.h"
Expand Down Expand Up @@ -111,15 +113,15 @@ void DieWithMessage(const ::std::string& message) {
fprintf(stderr, "%s", message.c_str());
fflush(stderr); // Make sure the text is printed before the process exits.

// We call _exit() instead of exit(), as the former is a direct
// We call _Exit() instead of exit(), as the former is a direct
// system call and thus safer in the presence of threads. exit()
// will invoke user-defined exit-hooks, which may do dangerous
// things that conflict with death tests.
//
// Some compilers can recognize that _exit() never returns and issue the
// Some compilers can recognize that _Exit() never returns and issue the
// 'unreachable code' warning for code following this function, unless
// fooled by a fake condition.
if (AlwaysTrue()) _exit(1);
if (AlwaysTrue()) _Exit(1);
}

void DieInside(const ::std::string& function) {
Expand Down Expand Up @@ -238,13 +240,13 @@ TEST(ExitStatusPredicateTest, ExitedWithCode) {

#else

// Returns the exit status of a process that calls _exit(2) with a
// Returns the exit status of a process that calls _Exit(2) with a
// given exit code. This is a helper function for the
// ExitStatusPredicateTest test suite.
static int NormalExitStatus(int exit_code) {
pid_t child_pid = fork();
if (child_pid == 0) {
_exit(exit_code);
_Exit(exit_code);
}
int status;
waitpid(child_pid, &status, 0);
Expand All @@ -260,7 +262,7 @@ static int KilledExitStatus(int signum) {
pid_t child_pid = fork();
if (child_pid == 0) {
raise(signum);
_exit(1);
_Exit(1);
}
int status;
waitpid(child_pid, &status, 0);
Expand Down Expand Up @@ -313,7 +315,7 @@ TEST_F(TestForDeathTest, SingleStatement) {
ASSERT_DEATH(return, "");

if (AlwaysTrue())
EXPECT_DEATH(_exit(1), "");
EXPECT_DEATH(_Exit(1), "");
else
// This empty "else" branch is meant to ensure that EXPECT_DEATH
// doesn't expand into an "if" statement without an "else"
Expand All @@ -324,7 +326,7 @@ TEST_F(TestForDeathTest, SingleStatement) {
if (AlwaysFalse())
;
else
EXPECT_DEATH(_exit(1), "") << 1 << 2 << 3;
EXPECT_DEATH(_Exit(1), "") << 1 << 2 << 3;
}
#ifdef __GNUC__
#pragma GCC diagnostic pop
Expand All @@ -339,11 +341,11 @@ TEST_F(TestForDeathTest, SwitchStatement) {

switch (0)
default:
ASSERT_DEATH(_exit(1), "") << "exit in default switch handler";
ASSERT_DEATH(_Exit(1), "") << "exit in default switch handler";

switch (0)
case 0:
EXPECT_DEATH(_exit(1), "") << "exit in switch case";
EXPECT_DEATH(_Exit(1), "") << "exit in switch case";

GTEST_DISABLE_MSC_WARNINGS_POP_()
}
Expand Down Expand Up @@ -371,10 +373,10 @@ TEST_F(TestForDeathTest, FastDeathTestInChangedDir) {
GTEST_FLAG_SET(death_test_style, "fast");

ChangeToRootDir();
EXPECT_EXIT(_exit(1), testing::ExitedWithCode(1), "");
EXPECT_EXIT(_Exit(1), testing::ExitedWithCode(1), "");

ChangeToRootDir();
ASSERT_DEATH(_exit(1), "");
ASSERT_DEATH(_Exit(1), "");
}

#ifdef GTEST_OS_LINUX
Expand Down Expand Up @@ -416,7 +418,7 @@ void DisableSigprofActionAndTimer(struct sigaction* old_signal_action) {
TEST_F(TestForDeathTest, FastSigprofActionSet) {
GTEST_FLAG_SET(death_test_style, "fast");
SetSigprofActionAndTimer();
EXPECT_DEATH(_exit(1), "");
EXPECT_DEATH(_Exit(1), "");
struct sigaction old_signal_action;
DisableSigprofActionAndTimer(&old_signal_action);
EXPECT_TRUE(old_signal_action.sa_sigaction == SigprofAction);
Expand All @@ -425,7 +427,7 @@ TEST_F(TestForDeathTest, FastSigprofActionSet) {
TEST_F(TestForDeathTest, ThreadSafeSigprofActionSet) {
GTEST_FLAG_SET(death_test_style, "threadsafe");
SetSigprofActionAndTimer();
EXPECT_DEATH(_exit(1), "");
EXPECT_DEATH(_Exit(1), "");
struct sigaction old_signal_action;
DisableSigprofActionAndTimer(&old_signal_action);
EXPECT_TRUE(old_signal_action.sa_sigaction == SigprofAction);
Expand All @@ -449,24 +451,24 @@ TEST_F(TestForDeathTest, ThreadsafeDeathTestInLoop) {
GTEST_FLAG_SET(death_test_style, "threadsafe");

for (int i = 0; i < 3; ++i)
EXPECT_EXIT(_exit(i), testing::ExitedWithCode(i), "") << ": i = " << i;
EXPECT_EXIT(_Exit(i), testing::ExitedWithCode(i), "") << ": i = " << i;
}

TEST_F(TestForDeathTest, ThreadsafeDeathTestInChangedDir) {
GTEST_FLAG_SET(death_test_style, "threadsafe");

ChangeToRootDir();
EXPECT_EXIT(_exit(1), testing::ExitedWithCode(1), "");
EXPECT_EXIT(_Exit(1), testing::ExitedWithCode(1), "");

ChangeToRootDir();
ASSERT_DEATH(_exit(1), "");
ASSERT_DEATH(_Exit(1), "");
}

TEST_F(TestForDeathTest, MixedStyles) {
GTEST_FLAG_SET(death_test_style, "threadsafe");
EXPECT_DEATH(_exit(1), "");
EXPECT_DEATH(_Exit(1), "");
GTEST_FLAG_SET(death_test_style, "fast");
EXPECT_DEATH(_exit(1), "");
EXPECT_DEATH(_Exit(1), "");
}

#if GTEST_HAS_CLONE && GTEST_HAS_PTHREAD
Expand All @@ -480,7 +482,7 @@ TEST_F(TestForDeathTest, DoesNotExecuteAtforkHooks) {
GTEST_FLAG_SET(death_test_style, "threadsafe");
pthread_flag = false;
ASSERT_EQ(0, pthread_atfork(&SetPthreadFlag, nullptr, nullptr));
ASSERT_DEATH(_exit(1), "");
ASSERT_DEATH(_Exit(1), "");
ASSERT_FALSE(pthread_flag);
}
}
Expand Down Expand Up @@ -805,8 +807,8 @@ TEST_F(TestForDeathTest, AssertDebugDeathAborts10) {

// Tests the *_EXIT family of macros, using a variety of predicates.
static void TestExitMacros() {
EXPECT_EXIT(_exit(1), testing::ExitedWithCode(1), "");
ASSERT_EXIT(_exit(42), testing::ExitedWithCode(42), "");
EXPECT_EXIT(_Exit(1), testing::ExitedWithCode(1), "");
ASSERT_EXIT(_Exit(42), testing::ExitedWithCode(42), "");

#ifdef GTEST_OS_WINDOWS

Expand All @@ -823,7 +825,7 @@ static void TestExitMacros() {

EXPECT_FATAL_FAILURE(
{ // NOLINT
ASSERT_EXIT(_exit(0), testing::KilledBySignal(SIGSEGV), "")
ASSERT_EXIT(_Exit(0), testing::KilledBySignal(SIGSEGV), "")
<< "This failure is expected, too.";
},
"This failure is expected, too.");
Expand All @@ -849,7 +851,7 @@ TEST_F(TestForDeathTest, InvalidStyle) {
GTEST_FLAG_SET(death_test_style, "rococo");
EXPECT_NONFATAL_FAILURE(
{ // NOLINT
EXPECT_DEATH(_exit(0), "") << "This failure is expected.";
EXPECT_DEATH(_Exit(0), "") << "This failure is expected.";
},
"This failure is expected.");
}
Expand Down Expand Up @@ -1140,7 +1142,7 @@ TEST_F(MacroLogicDeathTest, ChildDoesNotDie) {
// This time there are two calls to Abort: one since the test didn't
// die, and another from the ReturnSentinel when it's destroyed. The
// sentinel normally isn't destroyed if a test doesn't die, since
// _exit(2) is called in that case by ForkingDeathTest, but not by
// _Exit(2) is called in that case by ForkingDeathTest, but not by
// our MockDeathTest.
ASSERT_EQ(2U, factory_->AbortCalls());
EXPECT_EQ(DeathTest::TEST_DID_NOT_DIE, factory_->AbortArgument(0));
Expand All @@ -1152,21 +1154,21 @@ TEST_F(MacroLogicDeathTest, ChildDoesNotDie) {
// Tests that a successful death test does not register a successful
// test part.
TEST(SuccessRegistrationDeathTest, NoSuccessPart) {
EXPECT_DEATH(_exit(1), "");
EXPECT_DEATH(_Exit(1), "");
EXPECT_EQ(0, GetUnitTestImpl()->current_test_result()->total_part_count());
}

TEST(StreamingAssertionsDeathTest, DeathTest) {
EXPECT_DEATH(_exit(1), "") << "unexpected failure";
ASSERT_DEATH(_exit(1), "") << "unexpected failure";
EXPECT_DEATH(_Exit(1), "") << "unexpected failure";
ASSERT_DEATH(_Exit(1), "") << "unexpected failure";
EXPECT_NONFATAL_FAILURE(
{ // NOLINT
EXPECT_DEATH(_exit(0), "") << "expected failure";
EXPECT_DEATH(_Exit(0), "") << "expected failure";
},
"expected failure");
EXPECT_FATAL_FAILURE(
{ // NOLINT
ASSERT_DEATH(_exit(0), "") << "expected failure";
ASSERT_DEATH(_Exit(0), "") << "expected failure";
},
"expected failure");
}
Expand Down Expand Up @@ -1330,7 +1332,7 @@ TEST(InDeathTestChildDeathTest, ReportsDeathTestCorrectlyInFastStyle) {
{
fprintf(stderr, InDeathTestChild() ? "Inside" : "Outside");
fflush(stderr);
_exit(1);
_Exit(1);
},
"Inside");
}
Expand All @@ -1342,15 +1344,15 @@ TEST(InDeathTestChildDeathTest, ReportsDeathTestCorrectlyInThreadSafeStyle) {
{
fprintf(stderr, InDeathTestChild() ? "Inside" : "Outside");
fflush(stderr);
_exit(1);
_Exit(1);
},
"Inside");
}

void DieWithMessage(const char* message) {
fputs(message, stderr);
fflush(stderr); // Make sure the text is printed before the process exits.
_exit(1);
_Exit(1);
}

TEST(MatcherDeathTest, DoesNotBreakBareRegexMatching) {
Expand Down Expand Up @@ -1466,7 +1468,7 @@ TEST(ConditionalDeathMacrosSyntaxDeathTest, SingleStatement) {
ASSERT_DEATH_IF_SUPPORTED(return, "");

if (AlwaysTrue())
EXPECT_DEATH_IF_SUPPORTED(_exit(1), "");
EXPECT_DEATH_IF_SUPPORTED(_Exit(1), "");
else
// This empty "else" branch is meant to ensure that EXPECT_DEATH
// doesn't expand into an "if" statement without an "else"
Expand All @@ -1477,7 +1479,7 @@ TEST(ConditionalDeathMacrosSyntaxDeathTest, SingleStatement) {
if (AlwaysFalse())
; // NOLINT
else
EXPECT_DEATH_IF_SUPPORTED(_exit(1), "") << 1 << 2 << 3;
EXPECT_DEATH_IF_SUPPORTED(_Exit(1), "") << 1 << 2 << 3;
}
#ifdef __GNUC__
#pragma GCC diagnostic pop
Expand All @@ -1492,11 +1494,11 @@ TEST(ConditionalDeathMacrosSyntaxDeathTest, SwitchStatement) {

switch (0)
default:
ASSERT_DEATH_IF_SUPPORTED(_exit(1), "") << "exit in default switch handler";
ASSERT_DEATH_IF_SUPPORTED(_Exit(1), "") << "exit in default switch handler";

switch (0)
case 0:
EXPECT_DEATH_IF_SUPPORTED(_exit(1), "") << "exit in switch case";
EXPECT_DEATH_IF_SUPPORTED(_Exit(1), "") << "exit in switch case";

GTEST_DISABLE_MSC_WARNINGS_POP_()
}
Expand Down

0 comments on commit 96519a4

Please sign in to comment.