Skip to content

Commit 4be5615

Browse files
committed
Add special case for function types
1 parent aa57034 commit 4be5615

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3684,10 +3684,9 @@ def warn_alloca_align_alignof : Warning<
36843684
"second argument to __builtin_alloca_with_align is supposed to be in bits">,
36853685
InGroup<DiagGroup<"alloca-with-align-alignof">>;
36863686

3687-
def warn_alloc_size
3688-
: Warning<
3689-
"allocation of insufficient size '%0' for type %1 with size '%2'">,
3690-
InGroup<DiagGroup<"alloc-size">>;
3687+
def warn_alloc_size : Warning<
3688+
"allocation of insufficient size '%0' for type %1 with size '%2'">,
3689+
InGroup<DiagGroup<"alloc-size">>;
36913690

36923691
def err_alignment_too_small : Error<
36933692
"requested alignment must be %0 or greater">;

clang/lib/Sema/SemaExpr.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7842,7 +7842,11 @@ static void CheckSufficientAllocSize(Sema &S, QualType DestType,
78427842
auto Size = CharUnits::fromQuantity(AllocSize->getZExtValue());
78437843

78447844
QualType TargetType = DestType->getPointeeType();
7845-
auto LhsSize = S.Context.getTypeSizeInCharsIfKnown(TargetType);
7845+
// Find the destination size. As a special case function types have size of
7846+
// one byte to match the sizeof operator behavior.
7847+
auto LhsSize = TargetType->isFunctionType()
7848+
? CharUnits::One()
7849+
: S.Context.getTypeSizeInCharsIfKnown(TargetType);
78467850
if (LhsSize && Size < LhsSize)
78477851
S.Diag(E->getExprLoc(), diag::warn_alloc_size)
78487852
<< Size.getQuantity() << TargetType << LhsSize->getQuantity();

clang/test/Sema/warn-alloc-size.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,7 @@ void alloc_foo(void) {
4343
(void)(int *)my_malloc(1); // expected-warning {{allocation of insufficient size '1' for type 'int' with size '4'}}
4444

4545
struct ZeroSize *ptr18 = my_malloc(0); // okay becuase sizeof(struct ZeroSize) = 0
46-
// allocation of size 0 is implementation defined behavior though
46+
47+
void *funcptr_1 = (void (*)(int))my_malloc(0); // expected-warning {{allocation of insufficient size '0' for type 'void (int)' with size '1'}}
48+
void *funcptr_2 = (void (*)(int))my_malloc(1);
4749
}

0 commit comments

Comments
 (0)