cmCacheManager: Fix warning comment for cache vars containing newlines

Reuse the logic from commit 637a0f7ed4 (cmCacheManager: Prevent
corruption from multiline help strings, 2025-02-04, v4.0.0-rc1~52^2)
for the warning message placed in the cache when a cache variable's
value contains one or more newline characters.

While we're at it, remove a superfluous early-exit in the help strings
method.

Fixes: #27807

Suggested-By: Yauheni Khnykin <yauheni.khnykin@here.com>
This commit is contained in:
Tyler Yankee committed 2026-05-13 18:21:04 -04:00
1 parent 3d1f9d3dee
commit 053f02a41c
12 files changed
+38 -7

No files matched your search

+1 -4
View File
@@ -410,9 +410,6 @@ void cmCacheManager::OutputHelpString(std::ostream& fout,
std::string const& helpString)
{
std::string::size_type end = helpString.size();
if (end == 0) {
return;
}
std::string oneLine;
std::string::size_type pos = 0;
for (std::string::size_type i = 1; i <= end; i++) {
@@ -437,7 +434,7 @@ void cmCacheManager::OutputWarningComment(std::ostream& fout,
std::string::size_type end = message.size();
std::string oneLine;
std::string::size_type pos = 0;
for (std::string::size_type i = 0; i <= end; i++) {
for (std::string::size_type i = 1; i <= end; i++) {
if ((i == end) || (message[i] == '\n') ||
((i - pos >= 60) && (message[i] == ' ') && wrapSpaces)) {
fout << "# ";
@@ -0,0 +1,5 @@
//Offending entry
NEWLINE_VARIABLE:STRING=
# WARNING: Value of NEWLINE_VARIABLE contained a newline and was
# truncated\. Original value:
# \\n
@@ -0,0 +1,2 @@
CMake Warning:
Value of NEWLINE_VARIABLE contained a newline; truncating
@@ -0,0 +1 @@
set(NEWLINE_VARIABLE "\n" CACHE STRING "Offending entry")
@@ -0,0 +1,6 @@
//Offending entry
NEWLINE_VARIABLE:STRING=
# WARNING: Value of NEWLINE_VARIABLE contained a newline and was
# truncated\. Original value:
# \\na
# \\nb
@@ -0,0 +1,2 @@
CMake Warning:
Value of NEWLINE_VARIABLE contained a newline; truncating
@@ -0,0 +1 @@
set(NEWLINE_VARIABLE "\na\nb" CACHE STRING "Offending entry")
@@ -1,5 +1,9 @@
set(CACHE_EXPECTED_FILE "${RunCMake_TEST_SOURCE_DIR}/cache-regex.txt")
set(CACHE_ACTUAL_FILE "${RunCMake_BINARY_DIR}/CacheNewline-build/CMakeCache.txt")
set(CACHE_EXPECTED_FILE "${RunCMake_TEST_SOURCE_DIR}/${case}-CMakeCacheRegex.txt")
set(CACHE_ACTUAL_FILE "${RunCMake_TEST_BINARY_DIR}/CMakeCache.txt")
if(NOT EXISTS ${CACHE_EXPECTED_FILE})
message(FATAL_ERROR "Could not find ${RunCMake_TEST_SOURCE_DIR}/${case}-CMakeCacheRegex.txt")
endif()
file(READ ${CACHE_EXPECTED_FILE} CACHE_EXPECTED)
string(REGEX REPLACE "\r\n" "\n" CACHE_EXPECTED "${CACHE_EXPECTED}")
+14 -1
View File
@@ -1,3 +1,16 @@
include(RunCMake)
run_cmake(CacheNewline)
function(run_cmake_cache_newline_test case)
set(RunCMake-check-file CheckCache.cmake)
run_cmake(${case})
# Sanity check that the CMakeCache.txt is valid by trying to list variables.
unset(RunCMake-check-file)
set(RunCMake_TEST_NO_CLEAN 1)
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/${case}-build)
run_cmake_command(${case}-List ${CMAKE_COMMAND} -L ${RunCMake_TEST_BINARY_DIR})
endfunction()
run_cmake_cache_newline_test(CacheNewlines)
run_cmake_cache_newline_test(CacheStartsWithNewline)
run_cmake_cache_newline_test(CacheSingleNewline)