Skip to content

Commit

Permalink
Fixed memory corruption in VisualStudioFinder (#1425)
Browse files Browse the repository at this point in the history
Fixed memory corruption in VisualStudioFinder

VisualStudioFinder.GetLatestPath() uses COM to iterate over the
install locations for Visual Studio but somehow, once the method
returns to the caller (GetLatestVisualStudioPath), none of the
methods can be relied on to return sane values.

Therefore, the easiest solution is to track the values we care about
and just return them to our caller who can then avoid needing to re-make
the same COM invocations a second time.
  • Loading branch information
jstedfast authored Mar 31, 2023
1 parent 49d6407 commit 1a0fdfa
Showing 1 changed file with 14 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,35 +44,32 @@ public void Configure(WorkspaceOptions options)

private (string? Path, int? Version) GetLatestVisualStudioPath(string? suppliedPath)
{
var latest = GetLatestPath(suppliedPath);
var latest = GetLatestVisualStudio(suppliedPath);

if (latest is null)
if (latest.InstallPath is null)
{
_logger.LogWarning("Did not find a Visual Studio instance");
return default;
}

var version = Version.Parse(latest.GetInstallationVersion());
var installation = latest.GetInstallationPath();

if (Directory.Exists(installation))
if (Directory.Exists(latest.InstallPath))
{
_logger.LogDebug("Using Visual Studio v{VsVersion} [{VsPath}]", version, installation);
_logger.LogDebug("Using Visual Studio v{VsVersion} [{VsPath}]", latest.Version, latest.InstallPath);

return (installation, version?.Major);
return (latest.InstallPath, latest.Version.Major);
}
else
{
_logger.LogWarning("Found Visual Studio {VsVersion}, but directory '{VsPath}' does not exist.", version, installation);
_logger.LogWarning("Found Visual Studio {VsVersion}, but directory '{VsPath}' does not exist.", latest.Version, latest.InstallPath);

return default;
}
}

private ISetupInstance2? GetLatestPath(string? suppliedPath)
private (string? InstallPath, Version Version) GetLatestVisualStudio(string? suppliedPath)
{
var result = default(ISetupInstance2);
var resultVersion = new Version(0, 0);
string? resultPath = null;

try
{
Expand Down Expand Up @@ -116,17 +113,19 @@ public void Configure(WorkspaceOptions options)

if (instanceHasMSBuild && instance is not null)
{
if (suppliedPath is not null && string.Equals(suppliedPath, instance.GetInstallationPath(), StringComparison.OrdinalIgnoreCase))
var installPath = instance.GetInstallationPath();

if (suppliedPath is not null && string.Equals(suppliedPath, installPath, StringComparison.OrdinalIgnoreCase))
{
_logger.LogTrace("Identified supplied path for Visual Studio v{Version} [{Path}]", version, instance.GetInstallationPath());

return instance;
return (installPath, version);
}
else if (version > resultVersion)
{
_logger.LogTrace("Found Visual Studio v{Version} [{Path}]", version, instance.GetInstallationPath());

result = instance;
resultPath = installPath;
resultVersion = version;
}
}
Expand All @@ -142,7 +141,7 @@ public void Configure(WorkspaceOptions options)
// This is OK, VS "15" or greater likely not installed.
}

return result;
return (resultPath, resultVersion);
}

private static ISetupConfiguration GetQuery()
Expand Down

0 comments on commit 1a0fdfa

Please sign in to comment.