From 6eee8c9000cff8d5a5051b1bad28e611bc93db2d Mon Sep 17 00:00:00 2001 From: Brad King Date: Wed, 26 Oct 2022 11:10:40 -0400 Subject: VS: Fix crash finding vswhere on 32-bit Windows Since commit f85913fa08 (VS: Add support for enumerating VS instances with vswhere, 2022-04-11, v3.24.0-rc1~282^2), if the COM lookup does not report any VS instances, we fall back to finding vswhere. However, the `getenv` call returns nullptr if the `ProgramFiles(x86)` environment variable is not set. Update the logic to tolerate not-set environment variables. Also check the plain `ProgramFiles` environment variable. Fixes: #24090 --- Source/cmVSSetupHelper.cxx | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/Source/cmVSSetupHelper.cxx b/Source/cmVSSetupHelper.cxx index 1a3e72e..8764f21 100644 --- a/Source/cmVSSetupHelper.cxx +++ b/Source/cmVSSetupHelper.cxx @@ -300,13 +300,32 @@ bool cmVSSetupAPIHelper::IsEWDKEnabled() return false; } +#if !defined(CMAKE_BOOTSTRAP) +namespace { +std::string FindVsWhereCommand() +{ + std::string vswhere; + static const char* programFiles[] = { "ProgramFiles(x86)", "ProgramFiles" }; + for (const char* pf : programFiles) { + if (cmSystemTools::GetEnv(pf, vswhere)) { + vswhere += "/Microsoft Visual Studio/Installer/vswhere.exe"; + if (cmSystemTools::FileExists(vswhere)) { + return vswhere; + } + } + } + vswhere = "vswhere.exe"; + return vswhere; +} +} +#endif + bool cmVSSetupAPIHelper::EnumerateVSInstancesWithVswhere( std::vector& VSInstances) { #if !defined(CMAKE_BOOTSTRAP) // Construct vswhere command to get installed VS instances in JSON format - std::string vswhereExe = getenv("ProgramFiles(x86)") + - std::string(R"(\Microsoft Visual Studio\Installer\vswhere.exe)"); + std::string vswhereExe = FindVsWhereCommand(); std::vector vswhereCmd = { vswhereExe, "-format", "json" }; // Execute vswhere command and capture JSON output -- cgit v0.12