Invalid pointer dereference when calling `target_sources` after `install(TARGETS)`
This is clearly a case of a bad CMakeLists.txt, but this should at least warrant an error and not crash
Given the CMakeLists.txt
cmake_minimum_required(VERSION 3.24...3.24 FATAL_ERROR)
project(Issue
LANGUAGES CXX
)
add_library(MyLib a.cc)
install(TARGETS MyLib
EXPORT MyExportSet
)
target_sources(MyLib PUBLIC FILE_SET HEADERS BASE_DIRS
"${PROJECT_SOURCE_DIR}"
FILES
a.h
)
install(EXPORT MyExportSet
DESTINATION cmake
)
where a.cc
and a.h
are empty files, cmake
crashes.
I tried debugging this a bit and as far as I can tell, the crash happens because te->FileSetGenerators
is invalid in https://gitlab.kitware.com/cmake/cmake/-/blob/31cbe200f3f047a5e62fb851a2757f39dbf05c78/Source/cmExportInstallFileGenerator.cxx#L561. I am not familiar enough with the CMake codebase to debug further or attempt a patch, but I assume that the install(TARGETS MyLib EXPORT MyExportSet)
stores a reference to the (at that point empty FILE_SET HEADERS
), which gets invalidated afterwards by the target_sources
call. In the install(EXPORT)
the now invalid reference to the previous FILE_SET
presumably gets used even though it is no longer valid.