From d844fa23a0bc0630e3588a48154c0c51228d39c0 Mon Sep 17 00:00:00 2001 From: Hoyong Lee Date: Tue, 20 Jan 2026 17:35:23 +0900 Subject: [PATCH] Fix null pointer crash in ScopedPrematureExitFile when file creation fails When TEST_PREMATURE_EXIT_FILE is set and posix::FOpen fails, fwrite and fclose are called on nullptr without null check. Add null check before fwrite and fclose to ignore file creation failures as intended by the existing comment. --- googletest/src/gtest.cc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/googletest/src/gtest.cc b/googletest/src/gtest.cc index 193f880be..5619668d4 100644 --- a/googletest/src/gtest.cc +++ b/googletest/src/gtest.cc @@ -5146,8 +5146,10 @@ class ScopedPrematureExitFile { // errors are ignored as there's nothing better we can do and we // don't want to fail the test because of this. FILE* pfile = posix::FOpen(premature_exit_filepath_.c_str(), "w"); - fwrite("0", 1, 1, pfile); - fclose(pfile); + if (pfile != nullptr) { + fwrite("0", 1, 1, pfile); + fclose(pfile); + } } }