From 6f2701abf60bc8c0aeed14e09adf28d59023da87 Mon Sep 17 00:00:00 2001 From: Brad King Date: Tue, 10 Apr 2018 13:14:02 -0400 Subject: 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 --- Source/CPack/cpack.cxx | 8 ++++---- Tests/RunCMake/CMakeLists.txt | 1 + Tests/RunCMake/CPackCommandLine/NotAGenerator-result.txt | 1 + Tests/RunCMake/CPackCommandLine/NotAGenerator-stderr.txt | 1 + Tests/RunCMake/CPackCommandLine/RunCMakeTest.cmake | 10 ++++++++++ 5 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 Tests/RunCMake/CPackCommandLine/NotAGenerator-result.txt create mode 100644 Tests/RunCMake/CPackCommandLine/NotAGenerator-stderr.txt create mode 100644 Tests/RunCMake/CPackCommandLine/RunCMakeTest.cmake diff --git a/Source/CPack/cpack.cxx b/Source/CPack/cpack.cxx index 507a10c..b6ff38b 100644 --- a/Source/CPack/cpack.cxx +++ b/Source/CPack/cpack.cxx @@ -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 diff --git a/Tests/RunCMake/CMakeLists.txt b/Tests/RunCMake/CMakeLists.txt index d5bd297..c52f44e 100644 --- a/Tests/RunCMake/CMakeLists.txt +++ b/Tests/RunCMake/CMakeLists.txt @@ -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) diff --git a/Tests/RunCMake/CPackCommandLine/NotAGenerator-result.txt b/Tests/RunCMake/CPackCommandLine/NotAGenerator-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/CPackCommandLine/NotAGenerator-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/CPackCommandLine/NotAGenerator-stderr.txt b/Tests/RunCMake/CPackCommandLine/NotAGenerator-stderr.txt new file mode 100644 index 0000000..fe4e455 --- /dev/null +++ b/Tests/RunCMake/CPackCommandLine/NotAGenerator-stderr.txt @@ -0,0 +1 @@ +^CPack Error: Cannot initialize CPack generator: NotAGenerator diff --git a/Tests/RunCMake/CPackCommandLine/RunCMakeTest.cmake b/Tests/RunCMake/CPackCommandLine/RunCMakeTest.cmake new file mode 100644 index 0000000..991146c --- /dev/null +++ b/Tests/RunCMake/CPackCommandLine/RunCMakeTest.cmake @@ -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) -- cgit v0.12