Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
yangql
googletest
Commits
c39ee9c4
Commit
c39ee9c4
authored
Aug 06, 2019
by
Antoine Pitrou
Browse files
Fix #2371: Redirect Windows CRT assertions to stderr
parent
11be5f53
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
71 additions
and
5 deletions
+71
-5
googletest/src/gtest.cc
googletest/src/gtest.cc
+15
-5
googletest/test/googletest-death-test-test.cc
googletest/test/googletest-death-test-test.cc
+56
-0
No files found.
googletest/src/gtest.cc
View file @
c39ee9c4
...
@@ -80,6 +80,11 @@
...
@@ -80,6 +80,11 @@
#elif GTEST_OS_WINDOWS // We are on Windows proper.
#elif GTEST_OS_WINDOWS // We are on Windows proper.
# include <windows.h> // NOLINT
# undef min
# include <crtdbg.h> // NOLINT
# include <debugapi.h> // NOLINT
# include <io.h> // NOLINT
# include <io.h> // NOLINT
# include <sys/timeb.h> // NOLINT
# include <sys/timeb.h> // NOLINT
# include <sys/types.h> // NOLINT
# include <sys/types.h> // NOLINT
...
@@ -91,11 +96,6 @@
...
@@ -91,11 +96,6 @@
# include <sys/time.h> // NOLINT
# include <sys/time.h> // NOLINT
# endif // GTEST_OS_WINDOWS_MINGW
# endif // GTEST_OS_WINDOWS_MINGW
// cpplint thinks that the header is already included, so we want to
// silence it.
# include <windows.h> // NOLINT
# undef min
#else
#else
// Assume other platforms have gettimeofday().
// Assume other platforms have gettimeofday().
...
@@ -4914,6 +4914,16 @@ int UnitTest::Run() {
...
@@ -4914,6 +4914,16 @@ int UnitTest::Run() {
0x0
,
// Clear the following flags:
0x0
,
// Clear the following flags:
_WRITE_ABORT_MSG
|
_CALL_REPORTFAULT
);
// pop-up window, core dump.
_WRITE_ABORT_MSG
|
_CALL_REPORTFAULT
);
// pop-up window, core dump.
# endif
# endif
// In debug mode, the Windows CRT can crash with an assertion over invalid
// input (e.g. passing an invalid file descriptor). The default handling
// for these assertions is to pop up a dialog and wait for user input.
// Instead ask the CRT to dump such assertions to stderr non-interactively.
if
(
!
IsDebuggerPresent
())
{
(
void
)
_CrtSetReportMode
(
_CRT_ASSERT
,
_CRTDBG_MODE_FILE
|
_CRTDBG_MODE_DEBUG
);
(
void
)
_CrtSetReportFile
(
_CRT_ASSERT
,
_CRTDBG_FILE_STDERR
);
}
}
}
#endif // GTEST_OS_WINDOWS
#endif // GTEST_OS_WINDOWS
...
...
googletest/test/googletest-death-test-test.cc
View file @
c39ee9c4
...
@@ -41,7 +41,9 @@ using testing::internal::AlwaysTrue;
...
@@ -41,7 +41,9 @@ using testing::internal::AlwaysTrue;
#if GTEST_HAS_DEATH_TEST
#if GTEST_HAS_DEATH_TEST
# if GTEST_OS_WINDOWS
# if GTEST_OS_WINDOWS
# include <fcntl.h> // For O_BINARY
# include <direct.h> // For chdir().
# include <direct.h> // For chdir().
# include <io.h>
# else
# else
# include <unistd.h>
# include <unistd.h>
# include <sys/wait.h> // For waitpid.
# include <sys/wait.h> // For waitpid.
...
@@ -202,6 +204,26 @@ int DieInDebugElse12(int* sideeffect) {
...
@@ -202,6 +204,26 @@ int DieInDebugElse12(int* sideeffect) {
return
12
;
return
12
;
}
}
# if GTEST_OS_WINDOWS
// Death in dbg due to Windows CRT assertion failure, not opt.
int
DieInCRTDebugElse12
(
int
*
sideeffect
)
{
if
(
sideeffect
)
*
sideeffect
=
12
;
// Create an invalid fd by closing a valid one
int
fdpipe
[
2
];
EXPECT_EQ
(
_pipe
(
fdpipe
,
256
,
O_BINARY
),
0
);
EXPECT_EQ
(
_close
(
fdpipe
[
0
]),
0
);
EXPECT_EQ
(
_close
(
fdpipe
[
1
]),
0
);
// _dup() should crash in debug mode
EXPECT_EQ
(
_dup
(
fdpipe
[
0
]),
-
1
);
return
12
;
}
#endif // GTEST_OS_WINDOWS
# if GTEST_OS_WINDOWS || GTEST_OS_FUCHSIA
# if GTEST_OS_WINDOWS || GTEST_OS_FUCHSIA
// Tests the ExitedWithCode predicate.
// Tests the ExitedWithCode predicate.
...
@@ -632,6 +654,40 @@ TEST_F(TestForDeathTest, TestExpectDebugDeath) {
...
@@ -632,6 +654,40 @@ TEST_F(TestForDeathTest, TestExpectDebugDeath) {
# endif
# endif
}
}
# if GTEST_OS_WINDOWS
// Tests that EXPECT_DEBUG_DEATH works as expected when in debug mode
// the Windows CRT crashes the process with an assertion failure.
// 1. Asserts on death.
// 2. Has no side effect (doesn't pop up a window or wait for user input).
//
// And in opt mode, it:
// 1. Has side effects but does not assert.
TEST_F
(
TestForDeathTest
,
CRTDebugDeath
)
{
int
sideeffect
=
0
;
// Put the regex in a local variable to make sure we don't get an "unused"
// warning in opt mode.
const
char
*
regex
=
"dup.* : Assertion failed"
;
EXPECT_DEBUG_DEATH
(
DieInCRTDebugElse12
(
&
sideeffect
),
regex
)
<<
"Must accept a streamed message"
;
# ifdef NDEBUG
// Checks that the assignment occurs in opt mode (sideeffect).
EXPECT_EQ
(
12
,
sideeffect
);
# else
// Checks that the assignment does not occur in dbg mode (no sideeffect).
EXPECT_EQ
(
0
,
sideeffect
);
# endif
}
# endif // GTEST_OS_WINDOWS
// Tests that ASSERT_DEBUG_DEATH works as expected, that is, you can stream a
// Tests that ASSERT_DEBUG_DEATH works as expected, that is, you can stream a
// message to it, and in debug mode it:
// message to it, and in debug mode it:
// 1. Asserts on death.
// 1. Asserts on death.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment