Add a bounds check to protect against an empty vector from GetArgs(), which

can cause an out of bounds access in GetCurrentExecutableName(). One way this
can happen is if the user forgets to call InitGoogleTest().

PiperOrigin-RevId: 647740658
Change-Id: Id87692aa3d515b8ae0836e474be477d2aafa3871
This commit is contained in:
Abseil Team 2024-06-28 11:12:00 -07:00 committed by Copybara-Service
parent 1d17ea141d
commit 34ad51b3dc

View File

@ -661,11 +661,14 @@ static ::std::vector<std::string> g_argvs;
FilePath GetCurrentExecutableName() {
FilePath result;
auto args = GetArgvs();
if (!args.empty()) {
#if defined(GTEST_OS_WINDOWS) || defined(GTEST_OS_OS2)
result.Set(FilePath(GetArgvs()[0]).RemoveExtension("exe"));
result.Set(FilePath(args[0]).RemoveExtension("exe"));
#else
result.Set(FilePath(GetArgvs()[0]));
result.Set(FilePath(args[0]));
#endif // GTEST_OS_WINDOWS
}
return result.RemoveDirectoryName();
}