diff options
author | Kyle Edwards <kyle.edwards@kitware.com> | 2018-01-09 20:56:40 (GMT) |
---|---|---|
committer | Kyle Edwards <kyle.edwards@kitware.com> | 2018-01-15 14:27:44 (GMT) |
commit | a9c483071eb8a5b5da38726f42be98a2392f7a7f (patch) | |
tree | a1dfc2d3a42e84f89342b5845ea6795c1ccff654 /Source/cmCacheManager.cxx | |
parent | e40541339ae8a780bc410f0ef8b22d01e594c1ba (diff) | |
download | CMake-a9c483071eb8a5b5da38726f42be98a2392f7a7f.zip CMake-a9c483071eb8a5b5da38726f42be98a2392f7a7f.tar.gz CMake-a9c483071eb8a5b5da38726f42be98a2392f7a7f.tar.bz2 |
cmCacheManager: Truncate values containing newlines
Fixes #16098.
Diffstat (limited to 'Source/cmCacheManager.cxx')
-rw-r--r-- | Source/cmCacheManager.cxx | 74 |
1 files changed, 70 insertions, 4 deletions
diff --git a/Source/cmCacheManager.cxx b/Source/cmCacheManager.cxx index 44095ec..64aa46e 100644 --- a/Source/cmCacheManager.cxx +++ b/Source/cmCacheManager.cxx @@ -10,6 +10,7 @@ #include <string.h> #include "cmGeneratedFileStream.h" +#include "cmMessenger.h" #include "cmState.h" #include "cmSystemTools.h" #include "cmVersion.h" @@ -205,7 +206,8 @@ bool cmCacheManager::ReadPropertyEntry(std::string const& entryKey, return false; } -void cmCacheManager::WritePropertyEntries(std::ostream& os, CacheIterator i) +void cmCacheManager::WritePropertyEntries(std::ostream& os, CacheIterator i, + cmMessenger* messenger) { for (const char** p = this->PersistentProperties; *p; ++p) { if (const char* value = i.GetProperty(*p)) { @@ -221,11 +223,13 @@ void cmCacheManager::WritePropertyEntries(std::ostream& os, CacheIterator i) os << ":INTERNAL="; this->OutputValue(os, value); os << "\n"; + cmCacheManager::OutputNewlineTruncationWarning(os, key, value, + messenger); } } } -bool cmCacheManager::SaveCache(const std::string& path) +bool cmCacheManager::SaveCache(const std::string& path, cmMessenger* messenger) { std::string cacheFile = path; cacheFile += "/CMakeCache.txt"; @@ -316,7 +320,10 @@ bool cmCacheManager::SaveCache(const std::string& path) this->OutputKey(fout, i.first); fout << ":" << cmState::CacheEntryTypeToString(t) << "="; this->OutputValue(fout, ce.Value); - fout << "\n\n"; + fout << "\n"; + cmCacheManager::OutputNewlineTruncationWarning(fout, i.first, ce.Value, + messenger); + fout << "\n"; } } @@ -333,7 +340,7 @@ bool cmCacheManager::SaveCache(const std::string& path) } cmStateEnums::CacheEntryType t = i.GetType(); - this->WritePropertyEntries(fout, i); + this->WritePropertyEntries(fout, i, messenger); if (t == cmStateEnums::INTERNAL) { // Format is key:type=value if (const char* help = i.GetProperty("HELPSTRING")) { @@ -343,6 +350,8 @@ bool cmCacheManager::SaveCache(const std::string& path) fout << ":" << cmState::CacheEntryTypeToString(t) << "="; this->OutputValue(fout, i.GetValue()); fout << "\n"; + cmCacheManager::OutputNewlineTruncationWarning(fout, i.GetName(), + i.GetValue(), messenger); } } fout << "\n"; @@ -390,6 +399,19 @@ void cmCacheManager::OutputKey(std::ostream& fout, std::string const& key) void cmCacheManager::OutputValue(std::ostream& fout, std::string const& value) { + // look for and truncate newlines + std::string::size_type newline = value.find('\n'); + if (newline != std::string::npos) { + std::string truncated = value.substr(0, newline); + OutputValueNoNewlines(fout, truncated); + } else { + OutputValueNoNewlines(fout, value); + } +} + +void cmCacheManager::OutputValueNoNewlines(std::ostream& fout, + std::string const& value) +{ // if value has trailing space or tab, enclose it in single quotes if (!value.empty() && (value[value.size() - 1] == ' ' || value[value.size() - 1] == '\t')) { @@ -423,6 +445,50 @@ void cmCacheManager::OutputHelpString(std::ostream& fout, } } +void cmCacheManager::OutputWarningComment(std::ostream& fout, + std::string const& message, + bool wrapSpaces) +{ + 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++) { + if ((i == end) || (message[i] == '\n') || + ((i - pos >= 60) && (message[i] == ' ') && wrapSpaces)) { + fout << "# "; + if (message[pos] == '\n') { + pos++; + fout << "\\n"; + } + oneLine = message.substr(pos, i - pos); + fout << oneLine << "\n"; + pos = i; + } + } +} + +void cmCacheManager::OutputNewlineTruncationWarning(std::ostream& fout, + std::string const& key, + std::string const& value, + cmMessenger* messenger) +{ + if (value.find('\n') != std::string::npos) { + if (messenger) { + std::string message = "Value of "; + message += key; + message += " contained a newline; truncating"; + messenger->IssueMessage(cmake::WARNING, message); + } + + std::string comment = "WARNING: Value of "; + comment += key; + comment += " contained a newline and was truncated. Original value:"; + + OutputWarningComment(fout, comment, true); + OutputWarningComment(fout, value, false); + } +} + void cmCacheManager::RemoveCacheEntry(const std::string& key) { CacheEntryMap::iterator i = this->Cache.find(key); |