Skip to content

Commit

Permalink
[clang] Fix sorting module headers (#73146)
Browse files Browse the repository at this point in the history
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 cf1bde33423da5eb9b7dc95daac4aada3167de3c)
  • Loading branch information
tuliom authored and tru committed Nov 27, 2023
1 parent 7e30ce9 commit f74f3e6
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions clang/lib/Lex/ModuleMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2481,9 +2481,9 @@ void ModuleMapParser::parseHeaderDecl(MMToken::TokenKind LeadingToken,
<< FixItHint::CreateReplacement(CurrModuleDeclLoc, "framework module");
}

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

/// Parse an umbrella directory declaration.
Expand Down Expand Up @@ -2546,7 +2546,7 @@ void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) {
}

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

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

0 comments on commit f74f3e6

Please sign in to comment.