cmake-gui: Fix crash when selecting a preset with a differing build dir

QCMake::setPreset() bound a reference to the selected preset's expanded
data inside CMakePresetsGraph.ConfigurePresets and then called
setBinaryDirectory().  When the selected preset's build directory holds a
cache whose CMAKE_HOME_DIRECTORY differs from the current source
directory (e.g. because the source path is reached through a symbolic
link, since browsing keeps the link unresolved while the cache stores the
real path), setBinaryDirectory() loads that cache and re-sets the source
directory.  That re-reads the presets graph via loadPresets(), which
calls ClearPresets() and destroys the map entry, leaving the reference
dangling.  Subsequent access crashed with a use-after-free.

Copy the expanded preset instead of holding a reference into the graph so
it stays valid across setBinaryDirectory().

Fixes: #27927
This commit is contained in:
Joerg Bornemann
2026-07-09 14:00:00 +02:00
committed by Brad King
parent bb780342ef
commit 6862249daa

View File

@@ -164,7 +164,7 @@ void QCMake::setPreset(QString const& name, bool setBinary)
if (!name.isNull()) {
std::string presetName(name.toStdString());
auto const& expandedPreset =
auto const expandedPreset =
this->CMakePresetsGraph.ConfigurePresets[presetName].Expanded;
if (expandedPreset) {
if (setBinary && !expandedPreset->BinaryDir.empty()) {