Skip to content

Commit f74f3e6

Browse files
tuliomtru
authored andcommitted
[clang] Fix sorting module headers (#73146)
Struct Module::Header is not a POD type. As such, qsort() and llvm::array_pod_sort() must not be used to sort it. This became an issue with the new implementation of qsort() in glibc 2.39 that is not guaranteed to be a stable sort, causing Headers to be re-ordered and corrupted. Replace the usage of llvm::array_pod_sort() with std::stable_sort() in order to fix this issue. The signature of compareModuleHeaders() has to be modified. Fixes #73145. (cherry picked from commit cf1bde3)
1 parent 7e30ce9 commit f74f3e6

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

clang/lib/Lex/ModuleMap.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2481,9 +2481,9 @@ void ModuleMapParser::parseHeaderDecl(MMToken::TokenKind LeadingToken,
24812481
<< FixItHint::CreateReplacement(CurrModuleDeclLoc, "framework module");
24822482
}
24832483

2484-
static int compareModuleHeaders(const Module::Header *A,
2485-
const Module::Header *B) {
2486-
return A->NameAsWritten.compare(B->NameAsWritten);
2484+
static bool compareModuleHeaders(const Module::Header &A,
2485+
const Module::Header &B) {
2486+
return A.NameAsWritten < B.NameAsWritten;
24872487
}
24882488

24892489
/// Parse an umbrella directory declaration.
@@ -2546,7 +2546,7 @@ void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) {
25462546
}
25472547

25482548
// Sort header paths so that the pcm doesn't depend on iteration order.
2549-
llvm::array_pod_sort(Headers.begin(), Headers.end(), compareModuleHeaders);
2549+
std::stable_sort(Headers.begin(), Headers.end(), compareModuleHeaders);
25502550

25512551
for (auto &Header : Headers)
25522552
Map.addHeader(ActiveModule, std::move(Header), ModuleMap::TextualHeader);

0 commit comments

Comments
 (0)