Skip to content

Commit cf1bde3

Browse files
authored
[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.
1 parent a4ee55f commit cf1bde3

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
@@ -2509,9 +2509,9 @@ void ModuleMapParser::parseHeaderDecl(MMToken::TokenKind LeadingToken,
25092509
<< FixItHint::CreateReplacement(CurrModuleDeclLoc, "framework module");
25102510
}
25112511

2512-
static int compareModuleHeaders(const Module::Header *A,
2513-
const Module::Header *B) {
2514-
return A->NameAsWritten.compare(B->NameAsWritten);
2512+
static bool compareModuleHeaders(const Module::Header &A,
2513+
const Module::Header &B) {
2514+
return A.NameAsWritten < B.NameAsWritten;
25152515
}
25162516

25172517
/// Parse an umbrella directory declaration.
@@ -2574,7 +2574,7 @@ void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) {
25742574
}
25752575

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

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

0 commit comments

Comments
 (0)