Skip to content

[Clang][Sema] Fix crash with const qualified member operator new #80327

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 3, 2024

Conversation

shafik
Copy link
Collaborator

@shafik shafik commented Feb 1, 2024

We should diagnose a const qualified member operator new but we fail to do so and this leads to crash during debug info generation.

The fix is to diagnose this as ill-formed in the front-end.

Fixes: #79748

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

llvmbot commented Feb 1, 2024

@llvm/pr-subscribers-clang

Author: Shafik Yaghmour (shafik)

Changes

We should diagnose a const qualified member operator new but we fail to do so and this leads to crash during debug info generation.

The fix is to diagnose this as ill-formed in the front-end.

Fixes: #79748


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

2 Files Affected:

  • (modified) clang/lib/Sema/SemaType.cpp (+4-2)
  • (modified) clang/test/SemaCXX/function-type-qual.cpp (+6)
diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index 7cee73d5d6bae..2d9e3b2f73909 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -5914,8 +5914,10 @@ static TypeSourceInfo *GetFullTypeForDeclarator(TypeProcessingState &state,
     //
     // ... for instance.
     if (IsQualifiedFunction &&
-        !(Kind == Member && !D.isExplicitObjectMemberFunction() &&
-          D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_static) &&
+        (Kind != Member || D.isExplicitObjectMemberFunction() ||
+         D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static ||
+         (D.getContext() == clang::DeclaratorContext::Member &&
+          D.isStaticMember())) &&
         !IsTypedefName && D.getContext() != DeclaratorContext::TemplateArg &&
         D.getContext() != DeclaratorContext::TemplateTypeArg) {
       SourceLocation Loc = D.getBeginLoc();
diff --git a/clang/test/SemaCXX/function-type-qual.cpp b/clang/test/SemaCXX/function-type-qual.cpp
index bb25c17e83bdf..fc9fc20c14248 100644
--- a/clang/test/SemaCXX/function-type-qual.cpp
+++ b/clang/test/SemaCXX/function-type-qual.cpp
@@ -37,3 +37,9 @@ void instantiateArrayDecay() {
   int a[1];
   arrayDecay(a);
 }
+
+namespace GH79748 {
+struct A {
+  void* operator new(unsigned long bytes) const; //expected-error {{static member function cannot have 'const' qualifier}}
+};
+}

Copy link
Collaborator

@erichkeane erichkeane left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not the best reviewer for this, but it looks reasonable to me. Perhaps Aaron should take a look to make sure?

@shafik shafik force-pushed the fixCrashConstQualifiedOperatorNew branch from 571ca73 to afcf464 Compare February 1, 2024 21:25
Copy link
Contributor

@Fznamznon Fznamznon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a release note.

@shafik shafik force-pushed the fixCrashConstQualifiedOperatorNew branch from afcf464 to 154465e Compare February 2, 2024 22:23
Copy link
Contributor

@cor3ntin cor3ntin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

We should diagnose a const qualified member operator new but we fail to do so
and this leads to crash during debug info generation.

The fix is to diagnose this as ill-formed in the front-end.

Fixes: llvm#79748
@shafik shafik force-pushed the fixCrashConstQualifiedOperatorNew branch from 154465e to b047012 Compare February 3, 2024 01:11
@shafik shafik merged commit 82a3214 into llvm:main Feb 3, 2024
agozillon pushed a commit to agozillon/llvm-project that referenced this pull request Feb 5, 2024
…m#80327)

We should diagnose a const qualified member operator new but we fail to
do so and this leads to crash during debug info generation.

The fix is to diagnose this as ill-formed in the front-end.

Fixes: llvm#79748
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Front end crash with const qualifier on class member operator new()
5 participants