Skip to content

Commit e788788

Browse files
authored
[clang][Sema] Add support for OpenBSD's syslog format attribute (#97366)
1 parent b27360c commit e788788

File tree

5 files changed

+15
-4
lines changed

5 files changed

+15
-4
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ Attribute Changes in Clang
111111
- Clang now disallows more than one ``__attribute__((ownership_returns(class, idx)))`` with
112112
different class names attached to one function.
113113

114+
- Introduced a new format attribute ``__attribute__((format(syslog, 1, 2)))`` from OpenBSD.
115+
114116
Improvements to Clang's diagnostics
115117
-----------------------------------
116118

clang/include/clang/Sema/Sema.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2214,6 +2214,7 @@ class Sema final : public SemaBase {
22142214
FST_FreeBSDKPrintf,
22152215
FST_OSTrace,
22162216
FST_OSLog,
2217+
FST_Syslog,
22172218
FST_Unknown
22182219
};
22192220
static FormatStringType GetFormatStringType(const FormatAttr *Format);

clang/lib/Sema/SemaChecking.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6030,7 +6030,7 @@ static const Expr *maybeConstEvalStringLiteral(ASTContext &Context,
60306030
Sema::FormatStringType Sema::GetFormatStringType(const FormatAttr *Format) {
60316031
return llvm::StringSwitch<FormatStringType>(Format->getType()->getName())
60326032
.Case("scanf", FST_Scanf)
6033-
.Cases("printf", "printf0", FST_Printf)
6033+
.Cases("printf", "printf0", "syslog", FST_Printf)
60346034
.Cases("NSString", "CFString", FST_NSString)
60356035
.Case("strftime", FST_Strftime)
60366036
.Case("strfmon", FST_Strfmon)
@@ -6124,6 +6124,7 @@ bool Sema::CheckFormatArguments(ArrayRef<const Expr *> Args,
61246124
case FST_Kprintf:
61256125
case FST_FreeBSDKPrintf:
61266126
case FST_Printf:
6127+
case FST_Syslog:
61276128
Diag(FormatLoc, diag::note_format_security_fixit)
61286129
<< FixItHint::CreateInsertion(FormatLoc, "\"%s\", ");
61296130
break;
@@ -7860,7 +7861,7 @@ static void CheckFormatString(
78607861

78617862
if (Type == Sema::FST_Printf || Type == Sema::FST_NSString ||
78627863
Type == Sema::FST_FreeBSDKPrintf || Type == Sema::FST_OSLog ||
7863-
Type == Sema::FST_OSTrace) {
7864+
Type == Sema::FST_OSTrace || Type == Sema::FST_Syslog) {
78647865
CheckPrintfHandler H(
78657866
S, FExpr, OrigFormatExpr, Type, firstDataArg, numDataArgs,
78667867
(Type == Sema::FST_NSString || Type == Sema::FST_OSTrace), Str, APK,

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3407,8 +3407,8 @@ static FormatAttrKind getFormatAttrKind(StringRef Format) {
34073407
// Otherwise, check for supported formats.
34083408
.Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
34093409
.Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3410-
.Case("kprintf", SupportedFormat) // OpenBSD.
3411-
.Case("freebsd_kprintf", SupportedFormat) // FreeBSD.
3410+
.Cases("kprintf", "syslog", SupportedFormat) // OpenBSD.
3411+
.Case("freebsd_kprintf", SupportedFormat) // FreeBSD.
34123412
.Case("os_trace", SupportedFormat)
34133413
.Case("os_log", SupportedFormat)
34143414

clang/test/Sema/attr-format.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,10 @@ void forward_fixed(const char *fmt, _Bool b, char i, short j, int k, float l, do
9999
a(fmt, b, i, j, k, l, m);
100100
}
101101

102+
// OpenBSD
103+
// same as format(printf(...))...
104+
void a2(const char *a, ...) __attribute__((format(syslog, 1, 2))); // no-error
105+
void b2(const char *a, ...) __attribute__((format(syslog, 1, 1))); // expected-error {{'format' attribute parameter 3 is out of bounds}}
106+
void c2(const char *a, ...) __attribute__((format(syslog, 0, 2))); // expected-error {{'format' attribute parameter 2 is out of bounds}}
107+
void d2(const char *a, int c) __attribute__((format(syslog, 1, 2))); // expected-warning {{GCC requires a function with the 'format' attribute to be variadic}}
108+
void e2(char *str, int c, ...) __attribute__((format(syslog, 2, 3))); // expected-error {{format argument not a string type}}

0 commit comments

Comments
 (0)