Skip to content

Commit 69ee072

Browse files
committed
[clang][Sema] Add support for OpenBSD's syslog format attribute
1 parent f76ea31 commit 69ee072

File tree

4 files changed

+13
-4
lines changed

4 files changed

+13
-4
lines changed

clang/include/clang/Sema/Sema.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2108,6 +2108,7 @@ class Sema final : public SemaBase {
21082108
FST_FreeBSDKPrintf,
21092109
FST_OSTrace,
21102110
FST_OSLog,
2111+
FST_Syslog,
21112112
FST_Unknown
21122113
};
21132114
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
@@ -6114,7 +6114,7 @@ static const Expr *maybeConstEvalStringLiteral(ASTContext &Context,
61146114
Sema::FormatStringType Sema::GetFormatStringType(const FormatAttr *Format) {
61156115
return llvm::StringSwitch<FormatStringType>(Format->getType()->getName())
61166116
.Case("scanf", FST_Scanf)
6117-
.Cases("printf", "printf0", FST_Printf)
6117+
.Cases("printf", "printf0", "syslog", FST_Printf)
61186118
.Cases("NSString", "CFString", FST_NSString)
61196119
.Case("strftime", FST_Strftime)
61206120
.Case("strfmon", FST_Strfmon)
@@ -6211,6 +6211,7 @@ bool Sema::CheckFormatArguments(ArrayRef<const Expr *> Args,
62116211
case FST_Kprintf:
62126212
case FST_FreeBSDKPrintf:
62136213
case FST_Printf:
6214+
case FST_Syslog:
62146215
Diag(FormatLoc, diag::note_format_security_fixit)
62156216
<< FixItHint::CreateInsertion(FormatLoc, "\"%s\", ");
62166217
break;
@@ -7947,7 +7948,7 @@ static void CheckFormatString(
79477948

79487949
if (Type == Sema::FST_Printf || Type == Sema::FST_NSString ||
79497950
Type == Sema::FST_FreeBSDKPrintf || Type == Sema::FST_OSLog ||
7950-
Type == Sema::FST_OSTrace) {
7951+
Type == Sema::FST_OSTrace || Type == Sema::FST_Syslog) {
79517952
CheckPrintfHandler H(
79527953
S, FExpr, OrigFormatExpr, Type, firstDataArg, numDataArgs,
79537954
(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
@@ -3436,8 +3436,8 @@ static FormatAttrKind getFormatAttrKind(StringRef Format) {
34363436
// Otherwise, check for supported formats.
34373437
.Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
34383438
.Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3439-
.Case("kprintf", SupportedFormat) // OpenBSD.
3440-
.Case("freebsd_kprintf", SupportedFormat) // FreeBSD.
3439+
.Cases("kprintf", "syslog", SupportedFormat) // OpenBSD.
3440+
.Case("freebsd_kprintf", SupportedFormat) // FreeBSD.
34413441
.Case("os_trace", SupportedFormat)
34423442
.Case("os_log", SupportedFormat)
34433443

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)