llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: None (NeKon69)

<details>
<summary>Changes</summary>

This PR adds checks that ensure the compiler has the correct type so it does 
not crash when trying to get qualifiers from an incorrect type in the 
`IncompatiblePointerDiscardsQualifiers` and `IncompatibleOBTKinds` cases. It 
achieves this by performing a decay before accessing pointee qualifiers.

It also adds a test to ensure that such a bug won't arise again.

Fixes #<!-- -->182534

(same as #<!-- -->183616, unfortunately i forgot to create a new branch and 
wiped all changes)

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


3 Files Affected:

- (modified) clang/docs/ReleaseNotes.rst (+1) 
- (modified) clang/lib/Sema/SemaExpr.cpp (+10-2) 
- (added) clang/test/Sema/incompatible-function-to-ptr-decay.c (+18) 


``````````diff
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 6c26d514865ea..ef437b76f6a9f 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -355,6 +355,7 @@ Miscellaneous Clang Crashes Fixed
 - Fixed an assertion when diagnosing address-space qualified 
``new``/``delete`` in language-defined address spaces such as OpenCL 
``__local``. (#GH178319)
 - Fixed an assertion failure in ObjC++ ARC when binding a rvalue reference to 
reference with different lifetimes (#GH178524)
 - Fixed a crash when subscripting a vector type with large unsigned integer 
values. (#GH180563)
+- Fixed a crash when attempting to diagnose incompatible conversions involving 
function types (#GH182534)
 
 OpenACC Specific Changes
 ------------------------
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 53d215f5c5e3e..48293c8d7e92f 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -64,6 +64,7 @@
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/Support/ConvertUTF.h"
+#include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/SaveAndRestore.h"
 #include "llvm/Support/TimeProfiler.h"
 #include "llvm/Support/TypeSize.h"
@@ -17535,8 +17536,9 @@ bool Sema::DiagnoseAssignmentResult(AssignConvertType 
ConvTy,
     }
     break;
   case AssignConvertType::IncompatiblePointerDiscardsQualifiers: {
-    // Perform array-to-pointer decay if necessary.
-    if (SrcType->isArrayType()) SrcType = Context.getArrayDecayedType(SrcType);
+    // Perform decay if necessary.
+    if (SrcType->isArrayType() || SrcType->isFunctionType())
+      SrcType = Context.getDecayedType(SrcType);
 
     isInvalid = true;
 
@@ -17649,6 +17651,12 @@ bool Sema::DiagnoseAssignmentResult(AssignConvertType 
ConvTy,
   case AssignConvertType::CompatibleOBTDiscards:
     return false;
   case AssignConvertType::IncompatibleOBTKinds: {
+    if (SrcType->isArrayType())
+      SrcType = Context.getDecayedType(SrcType);
+
+    assert(!SrcType->isFunctionType() &&
+           "Unexpected function type found in IncompatibleOBTKinds 
assignment");
+
     auto getOBTKindName = [](QualType Ty) -> StringRef {
       if (Ty->isPointerType())
         Ty = Ty->getPointeeType();
diff --git a/clang/test/Sema/incompatible-function-to-ptr-decay.c 
b/clang/test/Sema/incompatible-function-to-ptr-decay.c
new file mode 100644
index 0000000000000..240b5b8763a23
--- /dev/null
+++ b/clang/test/Sema/incompatible-function-to-ptr-decay.c
@@ -0,0 +1,18 @@
+// RUN: %clang_cc1 -fsyntax-only -fexperimental-overflow-behavior-types 
-verify %s 
+
+// Issue 182534
+int foo();
+
+void bar(__attribute__((opencl_global)) int*); // #cldecl
+void baz(__ob_wrap int*); // #ofdecl
+
+void a() {
+  bar(foo);
+  // expected-error@-1 {{passing 'int (*)()' to parameter of type '__global 
int *' changes address space of pointer}}
+  // expected-note@#cldecl {{passing argument to parameter here}}
+  __ob_trap int val[10];
+  baz(val);
+  // expected-error@-1 {{assigning to '__ob_wrap int *' from '__ob_trap int *' 
with incompatible overflow behavior types ('__ob_wrap' and '__ob_trap')}}
+  // expected-note@#ofdecl {{passing argument to parameter here}}
+}
+

``````````

</details>


https://github.com/llvm/llvm-project/pull/187038
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to