Skip to content

Commit ef333bb

Browse files
committed
[clang][Sema] Add support for OpenBSD's format attribute syslog
1 parent ba0744e commit ef333bb

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
@@ -629,6 +629,8 @@ Attribute Changes in Clang
629629
The attributes declare constraints about a function's behavior pertaining to blocking and
630630
heap memory allocation.
631631

632+
- Introduced a new format attribute ``__attribute__((format(syslog, 1, 2)))`` from OpenBSD.
633+
632634
Improvements to Clang's diagnostics
633635
-----------------------------------
634636
- Clang now emits an error instead of a warning for ``-Wundefined-internal``

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
@@ -6026,7 +6026,7 @@ static const Expr *maybeConstEvalStringLiteral(ASTContext &Context,
60266026
Sema::FormatStringType Sema::GetFormatStringType(const FormatAttr *Format) {
60276027
return llvm::StringSwitch<FormatStringType>(Format->getType()->getName())
60286028
.Case("scanf", FST_Scanf)
6029-
.Cases("printf", "printf0", FST_Printf)
6029+
.Cases("printf", "printf0", "syslog", FST_Printf)
60306030
.Cases("NSString", "CFString", FST_NSString)
60316031
.Case("strftime", FST_Strftime)
60326032
.Case("strfmon", FST_Strfmon)
@@ -6120,6 +6120,7 @@ bool Sema::CheckFormatArguments(ArrayRef<const Expr *> Args,
61206120
case FST_Kprintf:
61216121
case FST_FreeBSDKPrintf:
61226122
case FST_Printf:
6123+
case FST_Syslog:
61236124
Diag(FormatLoc, diag::note_format_security_fixit)
61246125
<< FixItHint::CreateInsertion(FormatLoc, "\"%s\", ");
61256126
break;
@@ -7856,7 +7857,7 @@ static void CheckFormatString(
78567857

78577858
if (Type == Sema::FST_Printf || Type == Sema::FST_NSString ||
78587859
Type == Sema::FST_FreeBSDKPrintf || Type == Sema::FST_OSLog ||
7859-
Type == Sema::FST_OSTrace) {
7860+
Type == Sema::FST_OSTrace || Type == Sema::FST_Syslog) {
78607861
CheckPrintfHandler H(
78617862
S, FExpr, OrigFormatExpr, Type, firstDataArg, numDataArgs,
78627863
(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
@@ -3397,8 +3397,8 @@ static FormatAttrKind getFormatAttrKind(StringRef Format) {
33973397
// Otherwise, check for supported formats.
33983398
.Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
33993399
.Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3400-
.Case("kprintf", SupportedFormat) // OpenBSD.
3401-
.Case("freebsd_kprintf", SupportedFormat) // FreeBSD.
3400+
.Cases("kprintf", "syslog", SupportedFormat) // OpenBSD.
3401+
.Case("freebsd_kprintf", SupportedFormat) // FreeBSD.
34023402
.Case("os_trace", SupportedFormat)
34033403
.Case("os_log", SupportedFormat)
34043404

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)