CPack: Fix crash on invalid generator name

In commit v3.11.0-rc1~68^2 (CPack: accept --trace and --trace-expand,
2017-12-09) a nullptr dereference was added that occurs when
`cpack -G NotAGenerator` is invoked.  Add the needed condition.

Fixes: #17900
This commit is contained in:
Brad King 2018-04-10 13:14:02 -04:00
parent 61fd4c7420
commit 6f2701abf6
5 changed files with 17 additions and 4 deletions

View File

@ -350,16 +350,16 @@ int main(int argc, char const* const* argv)
}
if (parsed) {
cpackGenerator = generators.NewGenerator(gen);
if (!cpackGenerator) {
if (cpackGenerator) {
cpackGenerator->SetTrace(trace);
cpackGenerator->SetTraceExpand(traceExpand);
} else {
cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
"Cannot initialize CPack generator: " << gen
<< std::endl);
parsed = 0;
}
cpackGenerator->SetTrace(trace);
cpackGenerator->SetTraceExpand(traceExpand);
if (parsed && !cpackGenerator->Initialize(gen, mf)) {
cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
"Cannot initialize the generator " << gen

View File

@ -335,6 +335,7 @@ add_RunCMake_test(CommandLine -DCMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME})
add_RunCMake_test(CommandLineTar)
add_RunCMake_test(install)
add_RunCMake_test(CPackCommandLine)
add_RunCMake_test(CPackConfig)
add_RunCMake_test(CPackInstallProperties)
add_RunCMake_test(ExternalProject)

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1 @@
^CPack Error: Cannot initialize CPack generator: NotAGenerator

View File

@ -0,0 +1,10 @@
include(RunCMake)
set(RunCMake_TEST_TIMEOUT 60)
file(WRITE "${RunCMake_BINARY_DIR}/NotAGenerator-build/CPackConfig.cmake" [[
set(CPACK_PACKAGE_NAME "Test")
set(CPACK_PACKAGE_VERSION "1")
]])
set(RunCMake_TEST_NO_CLEAN 1)
run_cmake_command(NotAGenerator ${CMAKE_CPACK_COMMAND} -G NotAGenerator)
unset(RunCMake_TEST_NO_CLEAN)