From 559f750c897ddf0469ad04be6070dff24cef1ee8 Mon Sep 17 00:00:00 2001 From: Duncan Ogilvie Date: Fri, 31 May 2024 22:45:43 +0200 Subject: [PATCH] Fix undefined behavior with std::tolower/toupper --- src/cmake_generator.cpp | 5 ++++- src/project_parser.cpp | 10 +++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/cmake_generator.cpp b/src/cmake_generator.cpp index 75118ff..255b885 100644 --- a/src/cmake_generator.cpp +++ b/src/cmake_generator.cpp @@ -681,7 +681,10 @@ static std::string vcpkg_escape_identifier(const std::string &name) { ch = '-'; } - escaped += std::tolower(ch); + if (ch >= 'A' && ch <= 'Z') { + ch += ('a' - 'A'); + } + escaped += ch; } if (!vcpkg_valid_identifier(escaped)) { throw std::runtime_error("The escaped project name '" + escaped + "' is not usable with [vcpkg]"); diff --git a/src/project_parser.cpp b/src/project_parser.cpp index 4f14515..217252f 100644 --- a/src/project_parser.cpp +++ b/src/project_parser.cpp @@ -381,7 +381,8 @@ Project::Project(const Project *parent, const std::string &path, bool build) : p if (ch == '_') { normalized += '-'; } else if (ch >= 'A' && ch <= 'Z') { - normalized += std::tolower(ch); + ch += ('a' - 'A'); + normalized += ch; } else if (ch == '-' || (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z')) { normalized += ch; } else { @@ -532,8 +533,11 @@ Project::Project(const Project *parent, const std::string &path, bool build) : p key = "URL"; } else if (hash_algorithms.contains(key)) { std::string algo; - for (auto c : key) { - algo.push_back(std::toupper(c)); + for (auto ch : key) { + if (ch >= 'a' && ch <= 'z') { + ch -= ('a' - 'A'); + } + algo.push_back(ch); } key = "URL_HASH"; value = algo + "=" + value;