mirror of
https://github.com/Kitware/CMake.git
synced 2026-01-04 12:49:36 -06:00
file: Change REMOVE to ignore empty names
Previously code like
file(REMOVE_RECURSE "${accidentally_missing_variable}")
treated the empty string as a relative path with respect to the
current directory and removed its contents. Change this behavior
to ignore the empty string with a warning instead.
Normally such behavior changes are done with a policy, but in this case
such code is likely a real bug in project code that can delete data.
Fixes: #19274
This commit is contained in:
@@ -292,7 +292,8 @@ Move a file or directory within a filesystem from ``<oldname>`` to
|
|||||||
|
|
||||||
Remove the given files. The ``REMOVE_RECURSE`` mode will remove the given
|
Remove the given files. The ``REMOVE_RECURSE`` mode will remove the given
|
||||||
files and directories, also non-empty directories. No error is emitted if a
|
files and directories, also non-empty directories. No error is emitted if a
|
||||||
given file does not exist.
|
given file does not exist. Relative input paths are evaluated with respect
|
||||||
|
to the current source directory. Empty input paths are ignored with a warning.
|
||||||
|
|
||||||
.. _MAKE_DIRECTORY:
|
.. _MAKE_DIRECTORY:
|
||||||
|
|
||||||
|
|||||||
6
Help/release/dev/file-remove-no-empty.rst
Normal file
6
Help/release/dev/file-remove-no-empty.rst
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
file-remove-no-empty
|
||||||
|
--------------------
|
||||||
|
|
||||||
|
* The :command:`file(REMOVE)` and :command:`file(REMOVE_RECURSE)` commands
|
||||||
|
were changed to ignore empty arguments with a warning instead of treating
|
||||||
|
them as a relative path and removing the contents of the current directory.
|
||||||
@@ -1405,6 +1405,12 @@ bool cmFileCommand::HandleRemove(std::vector<std::string> const& args,
|
|||||||
cmMakeRange(args).advance(1)) // Get rid of subcommand
|
cmMakeRange(args).advance(1)) // Get rid of subcommand
|
||||||
{
|
{
|
||||||
std::string fileName = arg;
|
std::string fileName = arg;
|
||||||
|
if (fileName.empty()) {
|
||||||
|
std::string const r = recurse ? "REMOVE_RECURSE" : "REMOVE";
|
||||||
|
this->Makefile->IssueMessage(MessageType::AUTHOR_WARNING,
|
||||||
|
"Ignoring empty file name in " + r + ".");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if (!cmsys::SystemTools::FileIsFullPath(fileName)) {
|
if (!cmsys::SystemTools::FileIsFullPath(fileName)) {
|
||||||
fileName = this->Makefile->GetCurrentSourceDirectory();
|
fileName = this->Makefile->GetCurrentSourceDirectory();
|
||||||
fileName += "/" + arg;
|
fileName += "/" + arg;
|
||||||
|
|||||||
11
Tests/RunCMake/file/REMOVE-empty-stderr.txt
Normal file
11
Tests/RunCMake/file/REMOVE-empty-stderr.txt
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
^CMake Warning \(dev\) at REMOVE-empty.cmake:1 \(file\):
|
||||||
|
Ignoring empty file name in REMOVE.
|
||||||
|
Call Stack \(most recent call first\):
|
||||||
|
CMakeLists.txt:[0-9] \(include\)
|
||||||
|
This warning is for project developers. Use -Wno-dev to suppress it.
|
||||||
|
+
|
||||||
|
CMake Warning \(dev\) at REMOVE-empty.cmake:2 \(file\):
|
||||||
|
Ignoring empty file name in REMOVE_RECURSE.
|
||||||
|
Call Stack \(most recent call first\):
|
||||||
|
CMakeLists.txt:[0-9] \(include\)
|
||||||
|
This warning is for project developers. Use -Wno-dev to suppress it.$
|
||||||
2
Tests/RunCMake/file/REMOVE-empty.cmake
Normal file
2
Tests/RunCMake/file/REMOVE-empty.cmake
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
file(REMOVE "")
|
||||||
|
file(REMOVE_RECURSE "")
|
||||||
@@ -43,6 +43,8 @@ run_cmake(GLOB_RECURSE-noexp-FOLLOW_SYMLINKS)
|
|||||||
run_cmake(SIZE)
|
run_cmake(SIZE)
|
||||||
run_cmake(SIZE-error-does-not-exist)
|
run_cmake(SIZE-error-does-not-exist)
|
||||||
|
|
||||||
|
run_cmake(REMOVE-empty)
|
||||||
|
|
||||||
# tests are valid both for GLOB and GLOB_RECURSE
|
# tests are valid both for GLOB and GLOB_RECURSE
|
||||||
run_cmake(GLOB-sort-dedup)
|
run_cmake(GLOB-sort-dedup)
|
||||||
run_cmake(GLOB-error-LIST_DIRECTORIES-not-boolean)
|
run_cmake(GLOB-error-LIST_DIRECTORIES-not-boolean)
|
||||||
|
|||||||
Reference in New Issue
Block a user