Skip to content

Commit 559f750

Browse files
committed
Fix undefined behavior with std::tolower/toupper
1 parent 4b6b728 commit 559f750

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/cmake_generator.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,10 @@ static std::string vcpkg_escape_identifier(const std::string &name) {
681681
ch = '-';
682682
}
683683

684-
escaped += std::tolower(ch);
684+
if (ch >= 'A' && ch <= 'Z') {
685+
ch += ('a' - 'A');
686+
}
687+
escaped += ch;
685688
}
686689
if (!vcpkg_valid_identifier(escaped)) {
687690
throw std::runtime_error("The escaped project name '" + escaped + "' is not usable with [vcpkg]");

src/project_parser.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,8 @@ Project::Project(const Project *parent, const std::string &path, bool build) : p
381381
if (ch == '_') {
382382
normalized += '-';
383383
} else if (ch >= 'A' && ch <= 'Z') {
384-
normalized += std::tolower(ch);
384+
ch += ('a' - 'A');
385+
normalized += ch;
385386
} else if (ch == '-' || (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z')) {
386387
normalized += ch;
387388
} else {
@@ -532,8 +533,11 @@ Project::Project(const Project *parent, const std::string &path, bool build) : p
532533
key = "URL";
533534
} else if (hash_algorithms.contains(key)) {
534535
std::string algo;
535-
for (auto c : key) {
536-
algo.push_back(std::toupper(c));
536+
for (auto ch : key) {
537+
if (ch >= 'a' && ch <= 'z') {
538+
ch -= ('a' - 'A');
539+
}
540+
algo.push_back(ch);
537541
}
538542
key = "URL_HASH";
539543
value = algo + "=" + value;

0 commit comments

Comments
 (0)