Create file lists without using regular expression

Using the path prefix as part of a regular expression can lead to a
regular expression injection, the path might contain regular expression
meta characters that get interpreted. Use cmake_path() instead to filter
path prefixes.
This commit is contained in:
Steffen Olszewski 2023-05-23 19:07:18 +02:00
parent 7e520ec646
commit 7467f14193

View File

@ -62,10 +62,17 @@ function(set_target_source_groups arg_TARGET)
# Normally, the build tree is a subdirectory of the source tree. For the root directory the prefix match
# will also match the binary tree, so create separate file lists by filtering the binary prefix explicit.
# This will fail when doing an in-tree build, in that case everything will be classified as binary.
set(sourceFiles ${fileSources})
list(FILTER sourceFiles EXCLUDE REGEX "^${binaryDir}/")
set(binaryFiles ${fileSources})
list(FILTER binaryFiles INCLUDE REGEX "^${binaryDir}/")
set(sourceFiles "")
set(binaryFiles "")
foreach(file IN LISTS fileSources)
cmake_path(IS_PREFIX sourceDir "${file}" isSourceFile)
cmake_path(IS_PREFIX binaryDir "${file}" isBinaryFile)
if(isBinaryFile)
list(APPEND binaryFiles "${file}")
elseif(isSourceFile)
list(APPEND sourceFiles "${file}")
endif()
endforeach()
set(filterSources ${sourceFiles})
list(FILTER filterSources INCLUDE REGEX "^${sourceDir}/.+\\.h(h|pp)?$")
@ -106,9 +113,16 @@ function(set_target_source_groups arg_TARGET)
if(DEFINED arg_EXTRA_BINARY_DIRECTORY)
# If the specified directory contains the binary dir of the target, the files will get added
# twice with a different path, so exclude that directory.
set(filterSources ${fileSources})
list(FILTER filterSources INCLUDE REGEX "^${arg_EXTRA_BINARY_DIRECTORY}/")
list(FILTER filterSources EXCLUDE REGEX "^${binaryDir}/")
set(filterSources "")
foreach(file IN LISTS fileSources)
cmake_path(IS_PREFIX arg_EXTRA_BINARY_DIRECTORY "${file}" isExtraBinaryFile)
if(isExtraBinaryFile)
cmake_path(IS_PREFIX binaryDir "${file}" isBinaryFile)
if(NOT isBinaryFile)
list(APPEND filterSources "${file}")
endif()
endif()
endforeach()
source_group(
TREE "${arg_EXTRA_BINARY_DIRECTORY}"
PREFIX "Generated Files"