file(GENERATE) results in memory corruption/crashes
The file(GENERATE...)
API results in memory corruption/crashes when the arguments are passed out-of-order instead of failing gracefully. The following CMake script fails in the form of either a segfault or memory corruption while operating on uninitialized memory:
cmake_minimum_required(VERSION 3.19)
project(NotATeapot LANGUAGES CXX)
file(GENERATE OUTPUT "${CMAKE_BINARY_DIR}/output.cpp"
CONTENT "int number() { return 0; }")
add_library(fake_library OBJECT "${CMAKE_BINARY_DIR}/output.cpp")
file(GENERATE OUTPUT ${CMAKE_BINARY_DIR}/output.txt
TARGET fake_library
CONTENT "$<$<LINK_LANGUAGE:CXX>:I'm a teapot>")
Running it resulted in the following output:
$ cmake -G 'Ninja' ../
-- The CXX compiler identification is AppleClang 14.0.3.14030022
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (1.1s)
CMake Error in CMakeLists.txt:
Evaluation file "/tmp/reproducer/鰵?” cannot be read.
-- Generating done (0.0s)
CMake Generate step failed. Build files cannot be regenerated correctly.