Skip to content

Commit 749f34b

Browse files
committed
fixup: more restrictions on the pointer type
Disallow function pointers. Add tests to verify that member/data member pointers also fail.
1 parent 79b0bea commit 749f34b

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1857,11 +1857,15 @@ static bool checkSpanLikeType(const QualType &Ty) {
18571857
return false;
18581858
const QualType FirstFieldType = FieldsBegin->getType();
18591859
const QualType SecondFieldType = std::next(FieldsBegin)->getType();
1860+
auto validatePointerType = [](const QualType &T) {
1861+
// It must not point to functions.
1862+
return T->isPointerType() && !T->isFunctionPointerType();
1863+
};
18601864
// Verify two possible orderings.
1861-
return (FirstFieldType->isAnyPointerType() &&
1865+
return (validatePointerType(FirstFieldType) &&
18621866
SecondFieldType->isIntegerType()) ||
18631867
(FirstFieldType->isIntegerType() &&
1864-
SecondFieldType->isAnyPointerType());
1868+
validatePointerType(SecondFieldType));
18651869
}
18661870

18671871
static void handleMallocSpanAttr(Sema &S, Decl *D, const ParsedAttr &AL) {

clang/test/Sema/attr-malloc_span.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,10 @@ typedef struct {
2929
size_t n2;
3030
} invalid_span2;
3131
invalid_span2 returns_non_std_span2 (void) __attribute((malloc_span)); // expected-warning {{attribute only applies to functions that return span-like structures}}
32+
33+
// Function pointers are not allowed.
34+
typedef struct {
35+
int (*func_ptr)(void);
36+
size_t n;
37+
} func_span;
38+
func_span returns_func_span (void) __attribute((malloc_span)); // expected-warning {{attribute only applies to functions that return span-like structures}}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify %s
2+
3+
class SomeClass {
4+
public:
5+
int Data;
6+
};
7+
8+
// Returning pointers to data members is not allowed.
9+
struct DataMemberSpan {
10+
int SomeClass::* member_ptr;
11+
int n;
12+
};
13+
14+
DataMemberSpan returns_data_member_span(void) __attribute((malloc_span)) { // expected-warning {{attribute only applies to functions that return span-like structures}}
15+
return DataMemberSpan{};
16+
}
17+
18+
// Returning pointers to member functions is not allowed.
19+
struct MemberFuncSpan {
20+
void (SomeClass::*member_func_ptr)();
21+
int n;
22+
};
23+
24+
MemberFuncSpan returns_member_func_span(void) __attribute((malloc_span)) { // expected-warning {{attribute only applies to functions that return span-like structures}}
25+
return MemberFuncSpan{};
26+
}
27+

0 commit comments

Comments
 (0)