Skip to content

Commit 21c0511

Browse files
committed
[clang][OpenMP] Avoid null deref in allocator classification; diagnose missing <omp.h> in (fixes #157868)
1 parent 1d2f9ac commit 21c0511

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

clang/lib/Sema/SemaOpenMP.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3321,9 +3321,9 @@ SemaOpenMP::CheckOMPThreadPrivateDecl(SourceLocation Loc,
33213321
}
33223322
return D;
33233323
}
3324-
33253324
static OMPAllocateDeclAttr::AllocatorTypeTy
33263325
getAllocatorKind(Sema &S, DSAStackTy *Stack, Expr *Allocator) {
3326+
// No allocator expression → Null mem alloc.
33273327
if (!Allocator)
33283328
return OMPAllocateDeclAttr::OMPNullMemAlloc;
33293329
if (Allocator->isTypeDependent() || Allocator->isValueDependent() ||
@@ -3337,6 +3337,8 @@ getAllocatorKind(Sema &S, DSAStackTy *Stack, Expr *Allocator) {
33373337
for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) {
33383338
auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I);
33393339
const Expr *DefAllocator = Stack->getAllocator(AllocatorKind);
3340+
if (!DefAllocator)
3341+
continue; // null-guard: predefined not populated
33403342
llvm::FoldingSetNodeID DAEId;
33413343
DefAllocator->IgnoreImpCasts()->Profile(DAEId, S.getASTContext(),
33423344
/*Canonical=*/true);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %clang_cc1 -fopenmp -verify %s
2+
3+
typedef enum omp_allocator_handle_t {
4+
omp_default_mem_alloc = 1,
5+
__omp_allocator_handle_t_max__ = __UINTPTR_MAX__
6+
} omp_allocator_handle_t;
7+
8+
void foo(void) {
9+
omp_allocator_handle_t my_handle;
10+
int A[2];
11+
// expected-error@+2 {{'omp_allocator_handle_t' type not found; include <omp.h>}}
12+
// expected-note@+1 {{previous allocator is specified here}}
13+
#pragma omp allocate(A) allocator(my_handle)
14+
// expected-warning@+1 {{allocate directive specifies 'my_handle' allocator while previously used default}}
15+
#pragma omp allocate(A) allocator(my_handle)
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %clang_cc1 -fopenmp -verify %s
2+
// No <omp.h>; forge a typedef.
3+
typedef enum omp_allocator_handle_t {
4+
omp_default_mem_alloc = 1,
5+
__omp_allocator_handle_t_max__ = __UINTPTR_MAX__
6+
} omp_allocator_handle_t;
7+
8+
void foo(void) {
9+
omp_allocator_handle_t my_handle;
10+
int A[2];
11+
// expected-error@+1 {{'omp_allocator_handle_t' type not found; include <omp.h>}}
12+
#pragma omp allocate(A) allocator(my_handle)
13+
}

0 commit comments

Comments
 (0)