Skip to content

file(GLOB) does not escape filenames with semicolons when creating the output variable definition

We recently stumped against an escaping problem in CMake. GLOBbing a folder to get all filenames creates a list with the wrong number of items if any filename contains semicolons. Things also behave weirdly when square brackets are involved.

To reproduce try the following:

  1. Create an empty folder
  2. Run the following:
      echo 0 > 'a'
      echo 1 > 'a b '
      echo 2 > 'a;b c'  
      echo 3 > 'a;[;]c'
      echo 4 > 'a;[;c'
  3. Use the following CMakeLists.txt in the directory:
      cmake_minimum_required(VERSION 3.11)
      
      file(GLOB files "a*")
      message(STATUS " ++++ ${files} ++++")
      foreach(file IN LISTS files)
        message(STATUS " ------------- ${file} ------------- ")
        file(READ "${file}" contents)
        message("${contents}")
      endforeach()

The culprit seems to be bool cmFileCommand::HandleGlobCommand(std::vector<std::string> const& args, bool recurse). At the end of the method it does: this->Makefile->AddDefinition(variable, cmJoin(files, ";").c_str());.

The contents of the "files" vector should instead be escaped before joining it.

Edited by Ghost User
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information