Skip to content

Commit 8a0d145

Browse files
authored
[os_log] Fix a CodeGen crash that occurs when arguments of struct, class, or complex types are passed to _builtin_os_log_format (#158744)
This change fixes the crash in clang's CodeGen by erroring out in Sema if those arguments are passed. rdar://139824423
1 parent 360fc7b commit 8a0d145

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10456,6 +10456,9 @@ def warn_format_conversion_argument_type_mismatch : Warning<
1045610456
"format specifies type %0 but the argument has "
1045710457
"%select{type|underlying type}2 %1">,
1045810458
InGroup<Format>;
10459+
def err_format_conversion_argument_type_mismatch : Error<
10460+
"format specifies type %0 but the argument has "
10461+
"%select{type|underlying type}2 %1">;
1045910462
def warn_format_conversion_argument_type_mismatch_pedantic : Extension<
1046010463
warn_format_conversion_argument_type_mismatch.Summary>,
1046110464
InGroup<FormatPedantic>;
@@ -10505,6 +10508,8 @@ def warn_printf_asterisk_missing_arg : Warning<
1050510508
def warn_printf_asterisk_wrong_type : Warning<
1050610509
"field %select{width|precision}0 should have type %1, but argument has type %2">,
1050710510
InGroup<Format>;
10511+
def err_printf_asterisk_wrong_type : Error<
10512+
"field %select{width|precision}0 should have type %1, but argument has type %2">;
1050810513
def warn_printf_nonsensical_optional_amount: Warning<
1050910514
"%select{field width|precision}0 used with '%1' conversion specifier, resulting in undefined behavior">,
1051010515
InGroup<Format>;

clang/lib/Sema/SemaChecking.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7673,6 +7673,14 @@ void CheckPrintfHandler::handleInvalidMaskType(StringRef MaskType) {
76737673
S.Diag(getLocationOfByte(MaskType.data()), diag::err_invalid_mask_type_size);
76747674
}
76757675

7676+
// Error out if struct or complex type argments are passed to os_log.
7677+
static bool isInvalidOSLogArgTypeForCodeGen(FormatStringType FSType,
7678+
QualType T) {
7679+
if (FSType != FormatStringType::OSLog)
7680+
return false;
7681+
return T->isRecordType() || T->isComplexType();
7682+
}
7683+
76767684
bool CheckPrintfHandler::HandleAmount(
76777685
const analyze_format_string::OptionalAmount &Amt, unsigned k,
76787686
const char *startSpecifier, unsigned specifierLen) {
@@ -7705,11 +7713,14 @@ bool CheckPrintfHandler::HandleAmount(
77057713
assert(AT.isValid());
77067714

77077715
if (!AT.matchesType(S.Context, T)) {
7708-
EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_wrong_type)
7709-
<< k << AT.getRepresentativeTypeName(S.Context)
7710-
<< T << Arg->getSourceRange(),
7716+
unsigned DiagID = isInvalidOSLogArgTypeForCodeGen(FSType, T)
7717+
? diag::err_printf_asterisk_wrong_type
7718+
: diag::warn_printf_asterisk_wrong_type;
7719+
EmitFormatDiagnostic(S.PDiag(DiagID)
7720+
<< k << AT.getRepresentativeTypeName(S.Context)
7721+
<< T << Arg->getSourceRange(),
77117722
getLocationOfByte(Amt.getStart()),
7712-
/*IsStringLocation*/true,
7723+
/*IsStringLocation*/ true,
77137724
getSpecifierRange(startSpecifier, specifierLen));
77147725
// Don't do any more checking. We will just emit
77157726
// spurious errors.
@@ -8764,7 +8775,9 @@ CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS,
87648775
Diag = diag::warn_format_conversion_argument_type_mismatch_confusion;
87658776
break;
87668777
case ArgType::NoMatch:
8767-
Diag = diag::warn_format_conversion_argument_type_mismatch;
8778+
Diag = isInvalidOSLogArgTypeForCodeGen(FSType, ExprTy)
8779+
? diag::err_format_conversion_argument_type_mismatch
8780+
: diag::warn_format_conversion_argument_type_mismatch;
87688781
break;
87698782
}
87708783

clang/test/SemaObjC/os_log.m

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// RUN: %clang_cc1 -verify %s
2+
3+
struct S {
4+
int a[4];
5+
};
6+
7+
struct S s;
8+
_Complex float cf;
9+
10+
void test_builtin_os_log_invalid_arg(void *buf) {
11+
__builtin_os_log_format(buf, "%*.*f", s, 5, 1.3); // expected-error {{field width should have type 'int', but argument has type 'struct S'}}
12+
__builtin_os_log_format(buf, "%*.*f", 1, s, 1.3); // expected-error {{field precision should have type 'int', but argument has type 'struct S'}}
13+
__builtin_os_log_format(buf, "%*.*f", 1, 5, s); // expected-error {{format specifies type 'double' but the argument has type 'struct S'}}
14+
15+
__builtin_os_log_format(buf, "%*.*f", cf, 5, 1.3); // expected-error {{field width should have type 'int', but argument has type '_Complex float'}}
16+
__builtin_os_log_format(buf, "%*.*f", 1, cf, 1.3); // expected-error {{field precision should have type 'int', but argument has type '_Complex float'}}
17+
__builtin_os_log_format(buf, "%*.*f", 1, 5, cf); // expected-error {{format specifies type 'double' but the argument has type '_Complex float'}}
18+
19+
__builtin_os_log_format(buf, "%*.*f", (void *)0, 5, 1.3); // expected-warning {{field width should have type 'int', but argument has type 'void *'}}
20+
__builtin_os_log_format(buf, "%*.*f", 1, (void *)0, 1.3); // expected-warning {{field precision should have type 'int', but argument has type 'void *'}}
21+
__builtin_os_log_format(buf, "%*.*f", 1, 5, (void *)0); // expected-warning {{format specifies type 'double' but the argument has type 'void *'}}
22+
}

0 commit comments

Comments
 (0)