From f740f528fa08f9d126650d92472a921e53a95a73 Mon Sep 17 00:00:00 2001 From: Sandy Zhang Date: Thu, 8 Feb 2024 17:08:45 -0800 Subject: [PATCH 1/2] Escape _from_dir in install.cmake for regex special characters (e.g. c++) before REGEX REPLACE. PiperOrigin-RevId: 605469982 --- cmake/install.cmake | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cmake/install.cmake b/cmake/install.cmake index 9679d5b267..18ebb37b69 100644 --- a/cmake/install.cmake +++ b/cmake/install.cmake @@ -78,10 +78,12 @@ foreach(_header ${protobuf_HEADERS}) elseif (_find_nosrc GREATER -1) set(_from_dir "${protobuf_SOURCE_DIR}") endif() + # Escape _from_dir for regex special characters in the directory name. + string(REGEX REPLACE "([.+*?\^$()[\]{}|\\])" "\\\\$1" _from_dir_regexp ${_from_dir}) # On some platforms `_form_dir` ends up being just "protobuf", which can # easily match multiple times in our paths. We force it to only replace # prefixes to avoid this case. - string(REGEX REPLACE "^${_from_dir}" "" _header ${_header}) + string(REGEX REPLACE "^${_from_dir_regexp}" "" _header ${_header}) get_filename_component(_extract_from "${_from_dir}/${_header}" ABSOLUTE) get_filename_component(_extract_name ${_header} NAME) get_filename_component(_extract_to "${CMAKE_INSTALL_INCLUDEDIR}/${_header}" DIRECTORY) From 3ad69cf8b07ebcd54e76ee5360586a41b4f9d627 Mon Sep 17 00:00:00 2001 From: Sandy Zhang Date: Mon, 12 Feb 2024 11:12:08 -0800 Subject: [PATCH 2/2] Fix CMake regex escaping. CMake documentation (https://cmake.org/cmake/help/latest/command/string.html#regex-replace) indicates subexpressions are actually referenced via \1 not $1 in cmake. PiperOrigin-RevId: 606307156 --- cmake/install.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/install.cmake b/cmake/install.cmake index 18ebb37b69..52914a8ea9 100644 --- a/cmake/install.cmake +++ b/cmake/install.cmake @@ -79,7 +79,7 @@ foreach(_header ${protobuf_HEADERS}) set(_from_dir "${protobuf_SOURCE_DIR}") endif() # Escape _from_dir for regex special characters in the directory name. - string(REGEX REPLACE "([.+*?\^$()[\]{}|\\])" "\\\\$1" _from_dir_regexp ${_from_dir}) + string(REGEX REPLACE "([$^.[|*+?()]|])" "\\\\\\1" _from_dir_regexp "${_from_dir}") # On some platforms `_form_dir` ends up being just "protobuf", which can # easily match multiple times in our paths. We force it to only replace # prefixes to avoid this case.