Skip to content

[Clang] Add diagnostic about "%P" specifier with Objective-C pointers #89977

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -9901,6 +9901,9 @@ def warn_format_invalid_annotation : Warning<
def warn_format_P_no_precision : Warning<
"using '%%P' format specifier without precision">,
InGroup<Format>;
def warn_format_P_with_objc_pointer : Warning<
"using '%%P' format specifier with an Objective-C pointer results in dumping runtime object structure, not object value">,
InGroup<Format>;
def warn_printf_ignored_flag: Warning<
"flag '%0' is ignored when flag '%1' is present">,
InGroup<Format>;
Expand Down
11 changes: 11 additions & 0 deletions clang/lib/Sema/SemaChecking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12544,6 +12544,17 @@ CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS,
return true;
}

// Diagnose attempts to use '%P' with ObjC object types, which will result in
// dumping raw class data (like is-a pointer), not actual data.
if (FS.getConversionSpecifier().getKind() == ConversionSpecifier::PArg &&
ExprTy->isObjCObjectPointerType()) {
const CharSourceRange &CSR =
getSpecifierRange(StartSpecifier, SpecifierLen);
EmitFormatDiagnostic(S.PDiag(diag::warn_format_P_with_objc_pointer),
E->getExprLoc(), false, CSR);
return true;
}

ArgType::MatchKind ImplicitMatch = ArgType::NoMatch;
ArgType::MatchKind Match = AT.matchesType(S.Context, ExprTy);
ArgType::MatchKind OrigMatch = Match;
Expand Down
5 changes: 4 additions & 1 deletion clang/test/SemaObjC/format-strings-oslog.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,18 @@ void test_os_log_format(const char *pc, int i, void *p, void *buf) {
}

// Test os_log_format primitive with ObjC string literal format argument.
void test_objc(const char *pc, int i, void *p, void *buf, NSString *nss) {
void test_objc(const char *pc, int i, void *p, void *buf, NSString *nss, id obj) {
__builtin_os_log_format(buf, @"");
__builtin_os_log_format(buf, @"%d"); // expected-warning {{more '%' conversions than data arguments}}
__builtin_os_log_format(buf, @"%d", i);

__builtin_os_log_format(buf, @"%P", p); // expected-warning {{using '%P' format specifier without precision}}
__builtin_os_log_format(buf, @"%.10P", p);
__builtin_os_log_format(buf, @"%.*P", p); // expected-warning {{field precision should have type 'int', but argument has type 'void *'}}
__builtin_os_log_format(buf, @"%.*P", i, p);
__builtin_os_log_format(buf, @"%.*P", i, i); // expected-warning {{format specifies type 'void *' but the argument has type 'int'}}
__builtin_os_log_format(buf, @"%.8P", nss); // expected-warning {{using '%P' format specifier with an Objective-C pointer results in dumping runtime object structure, not object value}}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it warn when (void *)nss is passed too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, but I can be convinced. IMO, using a (void*) cast could be the escape-hatch if someone actually really does want to dump the ObjC runtime structure but also squash this warning.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that makes sense.

__builtin_os_log_format(buf, @"%.*P", i, obj); // expected-warning {{using '%P' format specifier with an Objective-C pointer results in dumping runtime object structure, not object value}}

__builtin_os_log_format(buf, @"%{private}s", pc);
__builtin_os_log_format(buf, @"%@", nss);
Expand Down