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.
  • Loading branch information
tuliom committed Nov 24, 2023
1 parent a4ee55f commit cf1bde3
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 @@ -2509,9 +2509,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 @@ -2574,7 +2574,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 cf1bde3

Please sign in to comment.