[WindowsDriver] Improve VSInstallPath check for IDE subdirectory
authorDimitry Andric <dimitry@andric.com>
Thu, 15 Dec 2022 20:38:07 +0000 (21:38 +0100)
committerDimitry Andric <dimitry@andric.com>
Thu, 15 Dec 2022 20:38:07 +0000 (21:38 +0100)
This avoids potential memory allocation failures, if VSInstallPath is
not empty, but also does not contain the string "\Common7\IDE".

Fixes: https://github.com/llvm/llvm-project/issues/59434

Reviewed By: mstorsjo

Differential Revision: https://reviews.llvm.org/D140019

llvm/lib/WindowsDriver/MSVCPaths.cpp

index fb89919..d8d656f 100644 (file)
@@ -705,8 +705,10 @@ bool findVCToolChainViaRegistry(std::string &Path, ToolsetLayout &VSLayout) {
       getSystemRegistryString(R"(SOFTWARE\Microsoft\VCExpress\$VERSION)",
                               "InstallDir", VSInstallPath, nullptr)) {
     if (!VSInstallPath.empty()) {
-      SmallString<256> VCPath(StringRef(VSInstallPath.c_str(),
-                                        VSInstallPath.find(R"(\Common7\IDE)")));
+      auto pos = VSInstallPath.find(R"(\Common7\IDE)");
+      if (pos == std::string::npos)
+        return false;
+      SmallString<256> VCPath(StringRef(VSInstallPath.c_str(), pos));
       sys::path::append(VCPath, "VC");
 
       Path = std::string(VCPath.str());