Skip to content

Commit cd975ea

Browse files
committed
Add a diagnostic for K&R-style functions without a prototype.
1 parent 132995f commit cd975ea

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

clang/lib/Sema/SemaType.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7955,8 +7955,13 @@ static bool handleFunctionTypeAttr(TypeProcessingState &state, ParsedAttr &attr,
79557955
return false;
79567956

79577957
const auto *FnTy = unwrapped.get()->getAs<FunctionProtoType>();
7958-
if (!FnTy)
7958+
if (!FnTy) {
7959+
S.Diag(attr.getLoc(), diag::err_attribute_wrong_decl_type)
7960+
<< attr << attr.isRegularKeywordAttribute()
7961+
<< ExpectedFunctionWithProtoType;
7962+
attr.setInvalid();
79597963
return true;
7964+
}
79607965

79617966
FunctionProtoType::ExtProtoInfo EPI = FnTy->getExtProtoInfo();
79627967
EPI.ExtraAttributeInfo.CFISalt = Argument;

clang/test/Sema/attr-cfi-salt.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: %clang_cc1 -fsyntax-only -fsanitize=kcfi -verify %s
2+
// RUN: %clang_cc1 -std=c89 -DKNR -fsyntax-only -fsanitize=kcfi -verify %s
23

34
#define __cfi_salt(S) __attribute__((cfi_salt(S)))
45

@@ -10,8 +11,10 @@ int bad3() __attribute__((cfi_salt("a", "b", "c"))); // expected-error{{'cfi_sal
1011
int foo(int a, int b) __cfi_salt("pepper"); // ok
1112
int foo(int a, int b) __cfi_salt("pepper"); // ok
1213

14+
#ifndef KNR
1315
typedef int (*bar_t)(void) __cfi_salt("pepper"); // ok
1416
typedef int (*bar_t)(void) __cfi_salt("pepper"); // ok
17+
#endif
1518

1619
// FIXME: Should we allow this?
1720
// int b(void) __cfi_salt("salt 'n") __cfi_salt("pepper");
@@ -49,3 +52,9 @@ void func4(int) [[clang::cfi_salt("test")]]; // ok
4952
void func4(int a) [[clang::cfi_salt("test")]] { } // ok
5053
void (* [[clang::cfi_salt("test")]] fp4)(int) = func4; // ok
5154
void (*fp4_noattr)(int) = func4; // expected-error{{incompatible function pointer types initializing 'void (*)(int)' with an expression of type 'void (int)'}}
55+
56+
#ifdef KNR
57+
// K&R C function without a prototype
58+
void func() __attribute__((cfi_salt("pepper"))); // expected-error {{attribute only applies to non-K&R-style functions}}
59+
void (*fp)() __attribute__((cfi_salt("pepper"))); // expected-error {{attribute only applies to non-K&R-style functions}}
60+
#endif

0 commit comments

Comments
 (0)