Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

C++: Remove potential FPs for cpp/wrong-type-format-argument in BMN #18581

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
Loading
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
C++: Remove potential FPs in cpp/wrong-type-format-argument in BMN
  • Loading branch information
calumgrant committed Jan 23, 2025
commit 9b53a390c041588f450b88f8522470275a38baab
9 changes: 8 additions & 1 deletion 9 cpp/ql/src/Likely Bugs/Format/WrongTypeFormatArguments.ql
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ predicate trivialConversion(ExpectedType expected, Type actual) {
*/
int sizeof_IntType() { exists(IntType it | result = it.getSize()) }

predicate buildModeNoneIntLongConversion(IntType formatType, LongType argumentType) {
exists(formatType) and
exists(argumentType) and
exists(Compilation c | c.buildModeNone())
}

from FormattingFunctionCall ffc, int n, Expr arg, Type expected, Type actual
where
(
Expand All @@ -171,7 +177,8 @@ where
not arg.isAffectedByMacro() and
not arg.isFromUninstantiatedTemplate(_) and
not actual.stripType() instanceof ErroneousType and
not arg.(Call).mayBeFromImplicitlyDeclaredFunction()
not arg.(Call).mayBeFromImplicitlyDeclaredFunction() and
not buildModeNoneIntLongConversion(expected, actual.getUnspecifiedType())
select arg,
"This format specifier for type '" + expected.getName() + "' does not match the argument type '" +
actual.getUnspecifiedType().getName() + "'."
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
| tests.c:7:18:7:18 | 1 | This format specifier for type 'char *' does not match the argument type 'int'. |
| tests.c:12:27:12:29 | 42 | This format specifier for type 'int' does not match the argument type 'long'. |
| tests.c:12:32:12:35 | 42 | This format specifier for type 'int' does not match the argument type 'unsigned long'. |
| tests.c:12:38:12:40 | 42 | This format specifier for type 'unsigned int' does not match the argument type 'long'. |
| tests.c:12:43:12:46 | 42 | This format specifier for type 'unsigned int' does not match the argument type 'unsigned long'. |
| tests.c:13:27:13:30 | 42 | This format specifier for type 'int' does not match the argument type 'long long'. |
| tests.c:13:33:13:37 | 42 | This format specifier for type 'int' does not match the argument type 'unsigned long long'. |
| tests.c:13:40:13:43 | 42 | This format specifier for type 'unsigned int' does not match the argument type 'long long'. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ void f(UNKNOWN_CHAR * str) {
sprintf(0, "%s", ""); // GOOD
fprintf(0, "%s", ""); // GOOD
printf("%s", str); // GOOD: erroneous type is ignored
printf("%d %d %u %u", 42l, 42ul, 42l, 42ul); // BAD (FP)
printf("%d %d %u %u", 42l, 42ul, 42l, 42ul); // GOOD: build mode none
printf("%d %d %u %u", 42ll, 42ull, 42ll, 42ull); // BAD
printf("%ld %ld %lu %lu", 42, 42u, 42, 42u); // BAD
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.