Skip to content

Conversation

@code-pankaj
Copy link

@code-pankaj code-pankaj commented Dec 12, 2025

This fixes a crash (segmentation fault) when the standard library implementation of comparison categories (like std::partial_ordering) is malformed.

Specifically, if the static members (like equivalent) are defined as a primitive type (e.g., int) instead of the comparison category type itself, the compiler previously attempted to process them incorrectly, leading to a crash.

This adds strict type checking in ComparisonCategoryInfo::lookupValueInfo. If the found static member does not match the record type, it is rejected safely, triggering a diagnostic error instead of a crash.

Fixes #170015

@github-actions
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Dec 12, 2025
@llvmbot
Copy link
Member

llvmbot commented Dec 12, 2025

@llvm/pr-subscribers-clang

Author: Pankaj (code-pankaj)

Changes

Add a null check for Record in
ComparisonCategoryInfo::ValueInfo::hasValidIntValue to avoid dereferencing a null CXXRecordDecl when the comparison category types (such as std::partial_ordering) are missing, incomplete, or malformed.

Previously, this caused a crash in both freestanding (-nostdinc++) and normal configurations when <compare> is unavailable or inconsistent.


Full diff: https://github.com/llvm/llvm-project/pull/172001.diff

1 Files Affected:

  • (modified) clang/lib/AST/ComparisonCategories.cpp (+1-1)
diff --git a/clang/lib/AST/ComparisonCategories.cpp b/clang/lib/AST/ComparisonCategories.cpp index 1b9c938e2ace3..431d6f77bc0a0 100644 --- a/clang/lib/AST/ComparisonCategories.cpp +++ b/clang/lib/AST/ComparisonCategories.cpp @@ -49,7 +49,7 @@ bool ComparisonCategoryInfo::ValueInfo::hasValidIntValue() const { // Before we attempt to get the value of the first field, ensure that we // actually have one (and only one) field. const auto *Record = VD->getType()->getAsCXXRecordDecl(); - if (Record->getNumFields() != 1 || + if (!Record || Record->getNumFields() != 1 || !Record->field_begin()->getType()->isIntegralOrEnumerationType()) return false; 
@tbaederr
Copy link
Contributor

tbaederr commented Dec 12, 2025

  1. Add "Fixes <link to issue>" to your PR description
  2. You need to add a test as well.
@tbaederr tbaederr requested a review from cor3ntin December 12, 2025 13:02
@zwuis
Copy link
Contributor

zwuis commented Dec 12, 2025

If AI was used to create PR, please disclose it. And to what extent it was used.

Please mention the issue fixed by this patch in PR description.

Please add tests.

Please add a release note entry.

@code-pankaj
Copy link
Author

Yes, GitHub Copilot was used to suggest code changes and commit messages.
But not to create PR (I was just testing it for the first time ever and didn't knew till what extent it go)
I added the issues fixes in the PR description

Will let you guys know after writing the test case.

This fixes a crash (segmentation fault) when the standard library implementation of comparison categories (like `std::partial_ordering`) is malformed. Specifically, if the static members (like `equivalent`) are defined as a primitive type (e.g., `int`) instead of the comparison category type itself, the compiler previously attempted to process them incorrectly, leading to a crash. This patch adds strict type checking in `ComparisonCategoryInfo::lookupValueInfo`. If the found static member does not match the record type, it is rejected safely, triggering a diagnostic error instead of a crash. Fixes llvm#170015
@code-pankaj code-pankaj changed the title [clang] Fix crash in hasValidIntValue when comparison category type is missing (fixes #170015) [Clang] Fix crash on malformed std::partial_ordering static members Dec 12, 2025
@code-pankaj
Copy link
Author

@tbaederr @zwuis Thanks for the Review!

I have updated the PR based on the feedback. I moved the fix to ComparisonCategoryInfo::lookupValueInfo to enforce strict type checking on the static members. This ensures we reject malformed types (like int) immediately with a proper "member missing" error.

I have also added the test and the Release Note entry.

Please let me know if anything else is needed!

Co-authored-by: Timm Baeder <tbaeder@redhat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

4 participants