From 998d4bae5c00ed7b0028cc7f2db5c0c29ae0d647 Mon Sep 17 00:00:00 2001 From: SkyCipherr Date: Thu, 12 Nov 2026 11:06:45 +0800 Subject: [PATCH 1/3] C++: suggest specifiers for simple integer format mismatches --- .../Format/WrongTypeFormatArguments.ql | 39 ++++++++++++++++++- .../2026-11-12-integer-format-suggestions.md | 4 ++ .../Linux/WrongTypeFormatArguments.expected | 18 +++++++++ .../Linux/WrongTypeFormatArguments.qlref | 1 + .../Linux/integer-hints.c | 35 +++++++++++++++++ .../IntegerFormatSuggestions/Linux/options | 1 + .../WrongTypeFormatArguments.expected | 18 +++++++++ .../Microsoft/WrongTypeFormatArguments.qlref | 1 + .../Microsoft/integer-hints.c | 35 +++++++++++++++++ .../Microsoft/options | 1 + .../WrongTypeFormatArguments.expected | 4 +- .../WrongTypeFormatArguments.expected | 8 ++-- .../WrongTypeFormatArguments.expected | 6 +-- .../WrongTypeFormatArguments.expected | 6 +-- .../WrongTypeFormatArguments.expected | 6 +-- 15 files changed, 167 insertions(+), 16 deletions(-) create mode 100644 cpp/ql/src/change-notes/2026-11-12-integer-format-suggestions.md create mode 100644 cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Linux/WrongTypeFormatArguments.expected create mode 100644 cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Linux/WrongTypeFormatArguments.qlref create mode 100644 cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Linux/integer-hints.c create mode 100644 cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Linux/options create mode 100644 cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Microsoft/WrongTypeFormatArguments.expected create mode 100644 cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Microsoft/WrongTypeFormatArguments.qlref create mode 100644 cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Microsoft/integer-hints.c create mode 100644 cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Microsoft/options diff --git a/cpp/ql/src/Likely Bugs/Format/WrongTypeFormatArguments.ql b/cpp/ql/src/Likely Bugs/Format/WrongTypeFormatArguments.ql index 5842b9474f74..58253b4b1154 100644 --- a/cpp/ql/src/Likely Bugs/Format/WrongTypeFormatArguments.ql +++ b/cpp/ql/src/Likely Bugs/Format/WrongTypeFormatArguments.ql @@ -152,6 +152,43 @@ predicate trivialConversion(ExpectedType expected, Type actual) { */ int sizeof_IntType() { exists(IntType it | result = it.getSize()) } +/** + * Gets a suggested specifier for a simple decimal integer conversion. + * Use the promoted argument type, and do not suggest changing width or precision arguments. + * Decorated, positional, and non-decimal conversions are deliberately left unchanged. + */ +private string suggestedIntegerSpecifier(FormattingFunctionCall ffc, int n, Expr arg, Type actual) { + exists(FormatLiteral format, IntegralType t, string length, string conversion | + format = ffc.getFormat() and + ffc.getConversionArgument(n) = arg and + format.getConvSpec(n).regexpMatch("%(l|ll)?[diu]") and + t = actual.getUnspecifiedType() and + ( + t instanceof IntType and length = "" + or + t instanceof LongType and length = "l" + or + t instanceof LongLongType and length = "ll" + ) and + ( + if t.isUnsigned() + then conversion = "u" + else + if format.getConversionChar(n) = "i" + then conversion = "i" + else conversion = "d" + ) and + result = "%" + length + conversion + ) +} + +bindingset[ffc, n, arg, actual] +private string integerFormatSuggestion(FormattingFunctionCall ffc, int n, Expr arg, Type actual) { + if exists(suggestedIntegerSpecifier(ffc, n, arg, actual)) + then result = " Consider using '" + suggestedIntegerSpecifier(ffc, n, arg, actual) + "' instead." + else result = "" +} + from FormattingFunctionCall ffc, int n, Expr arg, Type expected, Type actual where ( @@ -178,4 +215,4 @@ where count(ffc.getTarget().getFormatParameterIndex()) = 1 select arg, "This format specifier for type '" + expected.getName() + "' does not match the argument type '" + - actual.getUnspecifiedType().getName() + "'." + actual.getUnspecifiedType().getName() + "'." + integerFormatSuggestion(ffc, n, arg, actual) diff --git a/cpp/ql/src/change-notes/2026-11-12-integer-format-suggestions.md b/cpp/ql/src/change-notes/2026-11-12-integer-format-suggestions.md new file mode 100644 index 000000000000..1bca7a8169f3 --- /dev/null +++ b/cpp/ql/src/change-notes/2026-11-12-integer-format-suggestions.md @@ -0,0 +1,4 @@ +--- +category: minorAnalysis +--- +* The `cpp/wrong-type-format-argument` query now suggests a matching format specifier for simple decimal integer conversions with mismatched `int`, `long`, or `long long` arguments. The set of reported results is unchanged. diff --git a/cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Linux/WrongTypeFormatArguments.expected b/cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Linux/WrongTypeFormatArguments.expected new file mode 100644 index 000000000000..c1c1c29e417a --- /dev/null +++ b/cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Linux/WrongTypeFormatArguments.expected @@ -0,0 +1,18 @@ +| integer-hints.c:7:16:7:17 | ul | This format specifier for type 'int' does not match the argument type 'unsigned long'. Consider using '%lu' instead. | +| integer-hints.c:8:16:8:17 | ll | This format specifier for type 'int' does not match the argument type 'long long'. Consider using '%lld' instead. | +| integer-hints.c:9:16:9:18 | ull | This format specifier for type 'int' does not match the argument type 'unsigned long long'. Consider using '%llu' instead. | +| integer-hints.c:10:16:10:17 | ll | This format specifier for type 'int' does not match the argument type 'long long'. Consider using '%lli' instead. | +| integer-hints.c:11:18:11:18 | i | This format specifier for type 'long long' does not match the argument type 'int'. Consider using '%d' instead. | +| integer-hints.c:12:18:12:19 | ui | This format specifier for type 'unsigned long long' does not match the argument type 'unsigned int'. Consider using '%u' instead. | +| integer-hints.c:15:26:15:28 | ull | This format specifier for type 'int' does not match the argument type 'unsigned long long'. Consider using '%llu' instead. | +| integer-hints.c:16:19:16:20 | ll | This format specifier for type 'int' does not match the argument type 'long long'. Consider using '%lld' instead. | +| integer-hints.c:16:23:16:25 | ull | This format specifier for type 'int' does not match the argument type 'unsigned long long'. Consider using '%llu' instead. | +| integer-hints.c:26:18:26:19 | ll | This format specifier for type 'int' does not match the argument type 'long long'. | +| integer-hints.c:27:17:27:18 | ll | This format specifier for type 'int' does not match the argument type 'long long'. | +| integer-hints.c:28:18:28:19 | ll | This format specifier for type 'int' does not match the argument type 'long long'. | +| integer-hints.c:29:18:29:19 | ll | This format specifier for type 'int' does not match the argument type 'long long'. | +| integer-hints.c:30:16:30:18 | ull | This format specifier for type 'unsigned int' does not match the argument type 'unsigned long long'. | +| integer-hints.c:31:16:31:16 | d | This format specifier for type 'int' does not match the argument type 'double'. | +| integer-hints.c:32:16:32:17 | ll | This format specifier for type 'char *' does not match the argument type 'long long'. | +| integer-hints.c:33:16:33:17 | ll | This format specifier for type 'void *' does not match the argument type 'long long'. | +| integer-hints.c:34:16:34:16 | i | This format specifier for type 'double' does not match the argument type 'int'. | diff --git a/cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Linux/WrongTypeFormatArguments.qlref b/cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Linux/WrongTypeFormatArguments.qlref new file mode 100644 index 000000000000..9244006903c7 --- /dev/null +++ b/cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Linux/WrongTypeFormatArguments.qlref @@ -0,0 +1 @@ +query: Likely Bugs/Format/WrongTypeFormatArguments.ql diff --git a/cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Linux/integer-hints.c b/cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Linux/integer-hints.c new file mode 100644 index 000000000000..61574d504d65 --- /dev/null +++ b/cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Linux/integer-hints.c @@ -0,0 +1,35 @@ +// Independent reproduction of github/codeql#15028. Compile only: these +// deliberately mismatched printf calls must not be executed. +int printf(const char *format, ...); + +void integer_hints(int i, unsigned int ui, long l, unsigned long ul, + long long ll, unsigned long long ull, short s, double d) { + printf("%d", ul); // Original issue; alert depends on the target ABI. + printf("%d", ll); // Suggest %lld. + printf("%d", ull); // Suggest %llu. + printf("%i", ll); // Suggest %lli. + printf("%lld", i); // Suggest %d. + printf("%llu", ui); // Suggest %u. + printf("%lld", l); // On LLP64, suggest %ld. + printf("%llu", ul); // On LLP64, suggest %lu. + printf("value=%d end", ull); // Suggest %llu for the embedded conversion. + printf("%d %d", ll, ull); // Distinct suggestions for two arguments. + + printf("%d", i); // Correct: no alert. + printf("%u", ui); // Correct: no alert. + printf("%ld", l); // Correct: no alert. + printf("%lu", ul); // Correct: no alert. + printf("%lld", ll); // Correct: no alert. + printf("%llu", ull); // Correct: no alert. + printf("%d", s); // Correct after default argument promotion. + + printf("%08d", ll); // Existing alert; decorated formats out of scope. + printf("%*d", ll, i); // Width argument mismatch; no conversion suggestion. + printf("%.*d", ll, i); // Precision argument mismatch; no suggestion. + printf("%1$d", ll); // Positional format out of scope. + printf("%x", ull); // Hexadecimal formatting out of scope. + printf("%d", d); // Non-integer actual type: no suggestion. + printf("%s", ll); // Non-integer conversion: no suggestion. + printf("%p", ll); // Pointer conversion: no suggestion. + printf("%f", i); // Floating-point conversion: no suggestion. +} diff --git a/cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Linux/options b/cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Linux/options new file mode 100644 index 000000000000..fcea28bbb584 --- /dev/null +++ b/cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Linux/options @@ -0,0 +1 @@ +semmle-extractor-options: --clang --edg --signed_chars diff --git a/cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Microsoft/WrongTypeFormatArguments.expected b/cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Microsoft/WrongTypeFormatArguments.expected new file mode 100644 index 000000000000..e1dd974286ab --- /dev/null +++ b/cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Microsoft/WrongTypeFormatArguments.expected @@ -0,0 +1,18 @@ +| integer-hints.c:8:16:8:17 | ll | This format specifier for type 'int' does not match the argument type 'long long'. Consider using '%lld' instead. | +| integer-hints.c:9:16:9:18 | ull | This format specifier for type 'int' does not match the argument type 'unsigned long long'. Consider using '%llu' instead. | +| integer-hints.c:10:16:10:17 | ll | This format specifier for type 'int' does not match the argument type 'long long'. Consider using '%lli' instead. | +| integer-hints.c:11:18:11:18 | i | This format specifier for type 'long long' does not match the argument type 'int'. Consider using '%d' instead. | +| integer-hints.c:12:18:12:19 | ui | This format specifier for type 'unsigned long long' does not match the argument type 'unsigned int'. Consider using '%u' instead. | +| integer-hints.c:13:18:13:18 | l | This format specifier for type 'long long' does not match the argument type 'long'. Consider using '%ld' instead. | +| integer-hints.c:14:18:14:19 | ul | This format specifier for type 'unsigned long long' does not match the argument type 'unsigned long'. Consider using '%lu' instead. | +| integer-hints.c:15:26:15:28 | ull | This format specifier for type 'int' does not match the argument type 'unsigned long long'. Consider using '%llu' instead. | +| integer-hints.c:16:19:16:20 | ll | This format specifier for type 'int' does not match the argument type 'long long'. Consider using '%lld' instead. | +| integer-hints.c:16:23:16:25 | ull | This format specifier for type 'int' does not match the argument type 'unsigned long long'. Consider using '%llu' instead. | +| integer-hints.c:26:18:26:19 | ll | This format specifier for type 'int' does not match the argument type 'long long'. | +| integer-hints.c:27:17:27:18 | ll | This format specifier for type 'int' does not match the argument type 'long long'. | +| integer-hints.c:28:18:28:19 | ll | This format specifier for type 'int' does not match the argument type 'long long'. | +| integer-hints.c:29:18:29:19 | ll | This format specifier for type 'int' does not match the argument type 'long long'. | +| integer-hints.c:30:16:30:18 | ull | This format specifier for type 'unsigned int' does not match the argument type 'unsigned long long'. | +| integer-hints.c:31:16:31:16 | d | This format specifier for type 'int' does not match the argument type 'double'. | +| integer-hints.c:32:16:32:17 | ll | This format specifier for type 'char *' does not match the argument type 'long long'. | +| integer-hints.c:34:16:34:16 | i | This format specifier for type 'double' does not match the argument type 'int'. | diff --git a/cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Microsoft/WrongTypeFormatArguments.qlref b/cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Microsoft/WrongTypeFormatArguments.qlref new file mode 100644 index 000000000000..9244006903c7 --- /dev/null +++ b/cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Microsoft/WrongTypeFormatArguments.qlref @@ -0,0 +1 @@ +query: Likely Bugs/Format/WrongTypeFormatArguments.ql diff --git a/cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Microsoft/integer-hints.c b/cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Microsoft/integer-hints.c new file mode 100644 index 000000000000..61574d504d65 --- /dev/null +++ b/cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Microsoft/integer-hints.c @@ -0,0 +1,35 @@ +// Independent reproduction of github/codeql#15028. Compile only: these +// deliberately mismatched printf calls must not be executed. +int printf(const char *format, ...); + +void integer_hints(int i, unsigned int ui, long l, unsigned long ul, + long long ll, unsigned long long ull, short s, double d) { + printf("%d", ul); // Original issue; alert depends on the target ABI. + printf("%d", ll); // Suggest %lld. + printf("%d", ull); // Suggest %llu. + printf("%i", ll); // Suggest %lli. + printf("%lld", i); // Suggest %d. + printf("%llu", ui); // Suggest %u. + printf("%lld", l); // On LLP64, suggest %ld. + printf("%llu", ul); // On LLP64, suggest %lu. + printf("value=%d end", ull); // Suggest %llu for the embedded conversion. + printf("%d %d", ll, ull); // Distinct suggestions for two arguments. + + printf("%d", i); // Correct: no alert. + printf("%u", ui); // Correct: no alert. + printf("%ld", l); // Correct: no alert. + printf("%lu", ul); // Correct: no alert. + printf("%lld", ll); // Correct: no alert. + printf("%llu", ull); // Correct: no alert. + printf("%d", s); // Correct after default argument promotion. + + printf("%08d", ll); // Existing alert; decorated formats out of scope. + printf("%*d", ll, i); // Width argument mismatch; no conversion suggestion. + printf("%.*d", ll, i); // Precision argument mismatch; no suggestion. + printf("%1$d", ll); // Positional format out of scope. + printf("%x", ull); // Hexadecimal formatting out of scope. + printf("%d", d); // Non-integer actual type: no suggestion. + printf("%s", ll); // Non-integer conversion: no suggestion. + printf("%p", ll); // Pointer conversion: no suggestion. + printf("%f", i); // Floating-point conversion: no suggestion. +} diff --git a/cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Microsoft/options b/cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Microsoft/options new file mode 100644 index 000000000000..668c53efd7db --- /dev/null +++ b/cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Microsoft/options @@ -0,0 +1 @@ +semmle-extractor-options: --microsoft --edg --target --edg win64 diff --git a/cpp/ql/test/query-tests/Likely Bugs/Format/WrongTypeFormatArguments/Buildless/WrongTypeFormatArguments.expected b/cpp/ql/test/query-tests/Likely Bugs/Format/WrongTypeFormatArguments/Buildless/WrongTypeFormatArguments.expected index 8eefcc95a24f..600cf9e8254c 100644 --- a/cpp/ql/test/query-tests/Likely Bugs/Format/WrongTypeFormatArguments/Buildless/WrongTypeFormatArguments.expected +++ b/cpp/ql/test/query-tests/Likely Bugs/Format/WrongTypeFormatArguments/Buildless/WrongTypeFormatArguments.expected @@ -1,3 +1,3 @@ -| second.cpp:26:18:26:39 | ... - ... | This format specifier for type 'int' does not match the argument type 'long'. | -| second.cpp:29:18:29:39 | ... - ... | This format specifier for type 'unsigned int' does not match the argument type 'long'. | +| second.cpp:26:18:26:39 | ... - ... | This format specifier for type 'int' does not match the argument type 'long'. Consider using '%ld' instead. | +| second.cpp:29:18:29:39 | ... - ... | This format specifier for type 'unsigned int' does not match the argument type 'long'. Consider using '%ld' instead. | | tests.c:7:18:7:18 | 1 | This format specifier for type 'char *' does not match the argument type 'int'. | diff --git a/cpp/ql/test/query-tests/Likely Bugs/Format/WrongTypeFormatArguments/Linux_signed_chars/WrongTypeFormatArguments.expected b/cpp/ql/test/query-tests/Likely Bugs/Format/WrongTypeFormatArguments/Linux_signed_chars/WrongTypeFormatArguments.expected index f3963eff2b02..8e291793dbfd 100644 --- a/cpp/ql/test/query-tests/Likely Bugs/Format/WrongTypeFormatArguments/Linux_signed_chars/WrongTypeFormatArguments.expected +++ b/cpp/ql/test/query-tests/Likely Bugs/Format/WrongTypeFormatArguments/Linux_signed_chars/WrongTypeFormatArguments.expected @@ -1,16 +1,16 @@ | format.h:16:59:16:61 | str | This format specifier for type 'int' does not match the argument type 'char *'. | | format.h:16:64:16:64 | i | This format specifier for type 'double' does not match the argument type 'int'. | | format.h:16:67:16:67 | d | This format specifier for type 'char *' does not match the argument type 'double'. | -| linux.cpp:15:24:15:41 | call to get_template_value | This format specifier for type 'int' does not match the argument type 'long'. | +| linux.cpp:15:24:15:41 | call to get_template_value | This format specifier for type 'int' does not match the argument type 'long'. Consider using '%ld' instead. | | linux_c.c:11:15:11:18 | str3 | This format specifier for type 'char *' does not match the argument type 'short *'. | -| pri_macros.h:15:35:15:40 | my_u64 | This format specifier for type 'unsigned int' does not match the argument type 'unsigned long long'. | +| pri_macros.h:15:35:15:40 | my_u64 | This format specifier for type 'unsigned int' does not match the argument type 'unsigned long long'. Consider using '%llu' instead. | | printf1.h:12:27:12:27 | i | This format specifier for type 'double' does not match the argument type 'int'. | | printf1.h:18:18:18:18 | i | This format specifier for type 'void *' does not match the argument type 'int'. | | printf1.h:25:22:25:22 | i | This format specifier for type 'char *' does not match the argument type 'int'. | | printf1.h:27:19:27:20 | cs | This format specifier for type 'int' does not match the argument type 'char *'. | | printf1.h:38:18:38:30 | MYONETHOUSAND | This format specifier for type 'char *' does not match the argument type 'int'. | -| printf1.h:44:18:44:20 | ull | This format specifier for type 'int' does not match the argument type 'unsigned long long'. | -| printf1.h:45:18:45:20 | ull | This format specifier for type 'unsigned int' does not match the argument type 'unsigned long long'. | +| printf1.h:44:18:44:20 | ull | This format specifier for type 'int' does not match the argument type 'unsigned long long'. Consider using '%llu' instead. | +| printf1.h:45:18:45:20 | ull | This format specifier for type 'unsigned int' does not match the argument type 'unsigned long long'. Consider using '%llu' instead. | | printf1.h:46:18:46:20 | ull | This format specifier for type 'unsigned int' does not match the argument type 'unsigned long long'. | | printf1.h:113:17:113:17 | d | This format specifier for type 'long double' does not match the argument type 'double'. | | printf1.h:114:18:114:18 | d | This format specifier for type 'long double' does not match the argument type 'double'. | diff --git a/cpp/ql/test/query-tests/Likely Bugs/Format/WrongTypeFormatArguments/Linux_unsigned_chars/WrongTypeFormatArguments.expected b/cpp/ql/test/query-tests/Likely Bugs/Format/WrongTypeFormatArguments/Linux_unsigned_chars/WrongTypeFormatArguments.expected index 45bf793082eb..20d32bce8f46 100644 --- a/cpp/ql/test/query-tests/Likely Bugs/Format/WrongTypeFormatArguments/Linux_unsigned_chars/WrongTypeFormatArguments.expected +++ b/cpp/ql/test/query-tests/Likely Bugs/Format/WrongTypeFormatArguments/Linux_unsigned_chars/WrongTypeFormatArguments.expected @@ -1,14 +1,14 @@ | format.h:16:59:16:61 | str | This format specifier for type 'int' does not match the argument type 'char *'. | | format.h:16:64:16:64 | i | This format specifier for type 'double' does not match the argument type 'int'. | | format.h:16:67:16:67 | d | This format specifier for type 'char *' does not match the argument type 'double'. | -| pri_macros.h:15:35:15:40 | my_u64 | This format specifier for type 'unsigned int' does not match the argument type 'unsigned long long'. | +| pri_macros.h:15:35:15:40 | my_u64 | This format specifier for type 'unsigned int' does not match the argument type 'unsigned long long'. Consider using '%llu' instead. | | printf1.h:12:27:12:27 | i | This format specifier for type 'double' does not match the argument type 'int'. | | printf1.h:18:18:18:18 | i | This format specifier for type 'void *' does not match the argument type 'int'. | | printf1.h:25:22:25:22 | i | This format specifier for type 'char *' does not match the argument type 'int'. | | printf1.h:27:19:27:20 | cs | This format specifier for type 'int' does not match the argument type 'char *'. | | printf1.h:38:18:38:30 | MYONETHOUSAND | This format specifier for type 'char *' does not match the argument type 'int'. | -| printf1.h:44:18:44:20 | ull | This format specifier for type 'int' does not match the argument type 'unsigned long long'. | -| printf1.h:45:18:45:20 | ull | This format specifier for type 'unsigned int' does not match the argument type 'unsigned long long'. | +| printf1.h:44:18:44:20 | ull | This format specifier for type 'int' does not match the argument type 'unsigned long long'. Consider using '%llu' instead. | +| printf1.h:45:18:45:20 | ull | This format specifier for type 'unsigned int' does not match the argument type 'unsigned long long'. Consider using '%llu' instead. | | printf1.h:46:18:46:20 | ull | This format specifier for type 'unsigned int' does not match the argument type 'unsigned long long'. | | printf1.h:130:18:130:18 | 0 | This format specifier for type 'void *' does not match the argument type 'int'. | | printf1.h:168:19:168:19 | i | This format specifier for type 'long long' does not match the argument type 'int'. | diff --git a/cpp/ql/test/query-tests/Likely Bugs/Format/WrongTypeFormatArguments/Microsoft/WrongTypeFormatArguments.expected b/cpp/ql/test/query-tests/Likely Bugs/Format/WrongTypeFormatArguments/Microsoft/WrongTypeFormatArguments.expected index 6e17cecc33b9..0eaebd2c8420 100644 --- a/cpp/ql/test/query-tests/Likely Bugs/Format/WrongTypeFormatArguments/Microsoft/WrongTypeFormatArguments.expected +++ b/cpp/ql/test/query-tests/Likely Bugs/Format/WrongTypeFormatArguments/Microsoft/WrongTypeFormatArguments.expected @@ -1,14 +1,14 @@ | format.h:16:59:16:61 | str | This format specifier for type 'int' does not match the argument type 'char *'. | | format.h:16:64:16:64 | i | This format specifier for type 'double' does not match the argument type 'int'. | | format.h:16:67:16:67 | d | This format specifier for type 'char *' does not match the argument type 'double'. | -| pri_macros.h:15:35:15:40 | my_u64 | This format specifier for type 'unsigned int' does not match the argument type 'unsigned long long'. | +| pri_macros.h:15:35:15:40 | my_u64 | This format specifier for type 'unsigned int' does not match the argument type 'unsigned long long'. Consider using '%llu' instead. | | printf1.h:12:27:12:27 | i | This format specifier for type 'double' does not match the argument type 'int'. | | printf1.h:18:18:18:18 | i | This format specifier for type 'void *' does not match the argument type 'int'. | | printf1.h:25:22:25:22 | i | This format specifier for type 'char *' does not match the argument type 'int'. | | printf1.h:27:19:27:20 | cs | This format specifier for type 'int' does not match the argument type 'char *'. | | printf1.h:38:18:38:30 | MYONETHOUSAND | This format specifier for type 'char *' does not match the argument type 'int'. | -| printf1.h:44:18:44:20 | ull | This format specifier for type 'int' does not match the argument type 'unsigned long long'. | -| printf1.h:45:18:45:20 | ull | This format specifier for type 'unsigned int' does not match the argument type 'unsigned long long'. | +| printf1.h:44:18:44:20 | ull | This format specifier for type 'int' does not match the argument type 'unsigned long long'. Consider using '%llu' instead. | +| printf1.h:45:18:45:20 | ull | This format specifier for type 'unsigned int' does not match the argument type 'unsigned long long'. Consider using '%llu' instead. | | printf1.h:46:18:46:20 | ull | This format specifier for type 'unsigned int' does not match the argument type 'unsigned long long'. | | printf1.h:62:19:62:20 | ul | This format specifier for type 'size_t' does not match the argument type 'unsigned long'. | | printf1.h:68:19:68:21 | sst | This format specifier for type 'size_t' does not match the argument type 'long'. | diff --git a/cpp/ql/test/query-tests/Likely Bugs/Format/WrongTypeFormatArguments/Microsoft_no_wchar/WrongTypeFormatArguments.expected b/cpp/ql/test/query-tests/Likely Bugs/Format/WrongTypeFormatArguments/Microsoft_no_wchar/WrongTypeFormatArguments.expected index 0958cba59e72..85d549be5e58 100644 --- a/cpp/ql/test/query-tests/Likely Bugs/Format/WrongTypeFormatArguments/Microsoft_no_wchar/WrongTypeFormatArguments.expected +++ b/cpp/ql/test/query-tests/Likely Bugs/Format/WrongTypeFormatArguments/Microsoft_no_wchar/WrongTypeFormatArguments.expected @@ -1,14 +1,14 @@ | format.h:16:59:16:61 | str | This format specifier for type 'int' does not match the argument type 'char *'. | | format.h:16:64:16:64 | i | This format specifier for type 'double' does not match the argument type 'int'. | | format.h:16:67:16:67 | d | This format specifier for type 'char *' does not match the argument type 'double'. | -| pri_macros.h:15:35:15:40 | my_u64 | This format specifier for type 'unsigned int' does not match the argument type 'unsigned long long'. | +| pri_macros.h:15:35:15:40 | my_u64 | This format specifier for type 'unsigned int' does not match the argument type 'unsigned long long'. Consider using '%llu' instead. | | printf1.h:12:27:12:27 | i | This format specifier for type 'double' does not match the argument type 'int'. | | printf1.h:18:18:18:18 | i | This format specifier for type 'void *' does not match the argument type 'int'. | | printf1.h:25:22:25:22 | i | This format specifier for type 'char *' does not match the argument type 'int'. | | printf1.h:27:19:27:20 | cs | This format specifier for type 'int' does not match the argument type 'char *'. | | printf1.h:38:18:38:30 | MYONETHOUSAND | This format specifier for type 'char *' does not match the argument type 'int'. | -| printf1.h:44:18:44:20 | ull | This format specifier for type 'int' does not match the argument type 'unsigned long long'. | -| printf1.h:45:18:45:20 | ull | This format specifier for type 'unsigned int' does not match the argument type 'unsigned long long'. | +| printf1.h:44:18:44:20 | ull | This format specifier for type 'int' does not match the argument type 'unsigned long long'. Consider using '%llu' instead. | +| printf1.h:45:18:45:20 | ull | This format specifier for type 'unsigned int' does not match the argument type 'unsigned long long'. Consider using '%llu' instead. | | printf1.h:46:18:46:20 | ull | This format specifier for type 'unsigned int' does not match the argument type 'unsigned long long'. | | printf1.h:62:19:62:20 | ul | This format specifier for type 'size_t' does not match the argument type 'unsigned long'. | | printf1.h:68:19:68:21 | sst | This format specifier for type 'size_t' does not match the argument type 'long'. | From 0dc2c48c80c213382d750116c46e3a265d9cf418 Mon Sep 17 00:00:00 2001 From: SkyCipherr Date: Sat, 14 Nov 2026 21:14:31 +0800 Subject: [PATCH 2/3] C++: provide integer format specifier suggestions in Printf library --- .../2026-11-14-integer-format-specifiers.md | 4 + cpp/ql/lib/semmle/code/cpp/commons/Printf.qll | 36 +++++++++ .../Format/WrongTypeFormatArguments.ql | 40 ++-------- .../integerConversionSpecifier/Linux/options | 1 + .../Linux/suggestedSpecifier.expected | 61 +++++++++++++++ .../Linux/suggestedSpecifier.ql | 15 ++++ .../integerConversionSpecifier/Linux/test.c | 1 + .../Microsoft/options | 1 + .../Microsoft/suggestedSpecifier.expected | 61 +++++++++++++++ .../Microsoft/suggestedSpecifier.ql | 15 ++++ .../Microsoft/test.c | 1 + .../printf/integerConversionSpecifier/cases.h | 75 +++++++++++++++++++ 12 files changed, 279 insertions(+), 32 deletions(-) create mode 100644 cpp/ql/lib/change-notes/2026-11-14-integer-format-specifiers.md create mode 100644 cpp/ql/test/library-tests/printf/integerConversionSpecifier/Linux/options create mode 100644 cpp/ql/test/library-tests/printf/integerConversionSpecifier/Linux/suggestedSpecifier.expected create mode 100644 cpp/ql/test/library-tests/printf/integerConversionSpecifier/Linux/suggestedSpecifier.ql create mode 100644 cpp/ql/test/library-tests/printf/integerConversionSpecifier/Linux/test.c create mode 100644 cpp/ql/test/library-tests/printf/integerConversionSpecifier/Microsoft/options create mode 100644 cpp/ql/test/library-tests/printf/integerConversionSpecifier/Microsoft/suggestedSpecifier.expected create mode 100644 cpp/ql/test/library-tests/printf/integerConversionSpecifier/Microsoft/suggestedSpecifier.ql create mode 100644 cpp/ql/test/library-tests/printf/integerConversionSpecifier/Microsoft/test.c create mode 100644 cpp/ql/test/library-tests/printf/integerConversionSpecifier/cases.h diff --git a/cpp/ql/lib/change-notes/2026-11-14-integer-format-specifiers.md b/cpp/ql/lib/change-notes/2026-11-14-integer-format-specifiers.md new file mode 100644 index 000000000000..436614bc147a --- /dev/null +++ b/cpp/ql/lib/change-notes/2026-11-14-integer-format-specifiers.md @@ -0,0 +1,4 @@ +--- +category: feature +--- +* Added `FormatLiteral.getSuggestedIntegerConversionSpecifier` to `semmle.code.cpp.commons.Printf`. It provides matching specifiers for plain decimal integer conversions and promoted `int`, `long`, and `long long` argument types, including unsigned variants. diff --git a/cpp/ql/lib/semmle/code/cpp/commons/Printf.qll b/cpp/ql/lib/semmle/code/cpp/commons/Printf.qll index dfda2f4796c5..f08338194815 100644 --- a/cpp/ql/lib/semmle/code/cpp/commons/Printf.qll +++ b/cpp/ql/lib/semmle/code/cpp/commons/Printf.qll @@ -704,6 +704,42 @@ class FormatLiteral extends Literal instanceof StringLiteral { */ string getConversionChar(int n) { this.parseConvSpec(n, _, _, _, _, _, _, result) } + /** + * Gets a suggested replacement for the `n`th conversion specifier for an + * argument of type `type`, after default argument promotions. + * + * This only handles plain decimal integer conversions (`d`, `i`, and `u`, + * optionally with `l` or `ll`) and the signed and unsigned variants of `int`, + * `long`, and `long long`. There is no result for other types or conversions, + * or for conversions with flags, width, precision, or positional parameters. + * The result includes `%` and preserves `i` for signed integer arguments. + * + * This does not check whether the existing specifier matches `type`. + */ + string getSuggestedIntegerConversionSpecifier(int n, Type type) { + exists(IntegralType t, string length, string conversion, string originalConversion | + this.parseConvSpec(n, _, "", "", "", "", ["", "l", "ll"], originalConversion) and + originalConversion = ["d", "i", "u"] and + t = type.getUnspecifiedType() and + ( + t instanceof IntType and length = "" + or + t instanceof LongType and length = "l" + or + t instanceof LongLongType and length = "ll" + ) and + ( + if t.isUnsigned() + then conversion = "u" + else + if originalConversion = "i" + then conversion = "i" + else conversion = "d" + ) and + result = "%" + length + conversion + ) + } + /** * Gets the size of pointers in the target this formatting function is * compiled for. diff --git a/cpp/ql/src/Likely Bugs/Format/WrongTypeFormatArguments.ql b/cpp/ql/src/Likely Bugs/Format/WrongTypeFormatArguments.ql index 58253b4b1154..c1b246cde609 100644 --- a/cpp/ql/src/Likely Bugs/Format/WrongTypeFormatArguments.ql +++ b/cpp/ql/src/Likely Bugs/Format/WrongTypeFormatArguments.ql @@ -152,40 +152,16 @@ predicate trivialConversion(ExpectedType expected, Type actual) { */ int sizeof_IntType() { exists(IntType it | result = it.getSize()) } -/** - * Gets a suggested specifier for a simple decimal integer conversion. - * Use the promoted argument type, and do not suggest changing width or precision arguments. - * Decorated, positional, and non-decimal conversions are deliberately left unchanged. - */ -private string suggestedIntegerSpecifier(FormattingFunctionCall ffc, int n, Expr arg, Type actual) { - exists(FormatLiteral format, IntegralType t, string length, string conversion | - format = ffc.getFormat() and - ffc.getConversionArgument(n) = arg and - format.getConvSpec(n).regexpMatch("%(l|ll)?[diu]") and - t = actual.getUnspecifiedType() and - ( - t instanceof IntType and length = "" - or - t instanceof LongType and length = "l" - or - t instanceof LongLongType and length = "ll" - ) and - ( - if t.isUnsigned() - then conversion = "u" - else - if format.getConversionChar(n) = "i" - then conversion = "i" - else conversion = "d" - ) and - result = "%" + length + conversion - ) -} - bindingset[ffc, n, arg, actual] private string integerFormatSuggestion(FormattingFunctionCall ffc, int n, Expr arg, Type actual) { - if exists(suggestedIntegerSpecifier(ffc, n, arg, actual)) - then result = " Consider using '" + suggestedIntegerSpecifier(ffc, n, arg, actual) + "' instead." + if + ffc.getConversionArgument(n) = arg and + exists(ffc.getFormat().(FormatLiteral).getSuggestedIntegerConversionSpecifier(n, actual)) + then + result = + " Consider using '" + + ffc.getFormat().(FormatLiteral).getSuggestedIntegerConversionSpecifier(n, actual) + + "' instead." else result = "" } diff --git a/cpp/ql/test/library-tests/printf/integerConversionSpecifier/Linux/options b/cpp/ql/test/library-tests/printf/integerConversionSpecifier/Linux/options new file mode 100644 index 000000000000..fcea28bbb584 --- /dev/null +++ b/cpp/ql/test/library-tests/printf/integerConversionSpecifier/Linux/options @@ -0,0 +1 @@ +semmle-extractor-options: --clang --edg --signed_chars diff --git a/cpp/ql/test/library-tests/printf/integerConversionSpecifier/Linux/suggestedSpecifier.expected b/cpp/ql/test/library-tests/printf/integerConversionSpecifier/Linux/suggestedSpecifier.expected new file mode 100644 index 000000000000..7fd732531929 --- /dev/null +++ b/cpp/ql/test/library-tests/printf/integerConversionSpecifier/Linux/suggestedSpecifier.expected @@ -0,0 +1,61 @@ +| ../cases.h:11:10:11:13 | %d | 0 | %d | %d | +| ../cases.h:12:10:12:13 | %d | 0 | %u | %u | +| ../cases.h:13:10:13:13 | %d | 0 | %ld | %ld | +| ../cases.h:14:10:14:13 | %d | 0 | %lu | %lu | +| ../cases.h:15:10:15:13 | %d | 0 | %lld | %lld | +| ../cases.h:16:10:16:13 | %d | 0 | %llu | %llu | +| ../cases.h:17:10:17:13 | %i | 0 | %i | %i | +| ../cases.h:18:10:18:13 | %i | 0 | %u | %u | +| ../cases.h:19:10:19:13 | %i | 0 | %li | %li | +| ../cases.h:20:10:20:13 | %i | 0 | %lu | %lu | +| ../cases.h:21:10:21:13 | %i | 0 | %lli | %lli | +| ../cases.h:22:10:22:13 | %i | 0 | %llu | %llu | +| ../cases.h:23:10:23:13 | %u | 0 | %d | %d | +| ../cases.h:24:10:24:14 | %ld | 0 | %d | %d | +| ../cases.h:25:10:25:14 | %li | 0 | %u | %u | +| ../cases.h:26:10:26:14 | %lu | 0 | %lld | %lld | +| ../cases.h:27:10:27:15 | %lld | 0 | %ld | %ld | +| ../cases.h:28:10:28:15 | %lli | 0 | %llu | %llu | +| ../cases.h:29:10:29:15 | %llu | 0 | %lu | %lu | +| ../cases.h:30:10:30:13 | %d | 0 | %lu | %lu | +| ../cases.h:32:10:32:15 | %lld | 0 | %d | | +| ../cases.h:33:10:33:15 | %lld | 0 | %d | | +| ../cases.h:34:10:34:15 | %lld | 0 | %d | | +| ../cases.h:35:10:35:15 | %lld | 0 | %d | | +| ../cases.h:36:10:36:15 | %lld | 0 | | | +| ../cases.h:38:10:38:23 | value=%d end | 0 | %llu | %llu | +| ../cases.h:39:10:39:19 | %% %d %i | 0 | %ld | %ld | +| ../cases.h:39:10:39:19 | %% %d %i | 1 | %llu | %llu | +| ../cases.h:42:10:42:14 | %+d | 0 | | | +| ../cases.h:43:10:43:14 | % d | 0 | | | +| ../cases.h:44:10:44:14 | %-d | 0 | | | +| ../cases.h:45:10:45:14 | %#d | 0 | | | +| ../cases.h:46:10:46:14 | %'d | 0 | | | +| ../cases.h:47:10:47:15 | %08d | 0 | | | +| ../cases.h:48:10:48:15 | %12d | 0 | | | +| ../cases.h:49:10:49:14 | %.d | 0 | | | +| ../cases.h:50:10:50:15 | %.3d | 0 | | | +| ../cases.h:51:10:51:14 | %*d | 0 | | | +| ../cases.h:52:10:52:15 | %.*d | 0 | | | +| ../cases.h:53:10:53:15 | %1$d | 0 | | | +| ../cases.h:54:10:54:20 | %2$d %1$i | 0 | | | +| ../cases.h:54:10:54:20 | %2$d %1$i | 1 | | | +| ../cases.h:56:10:56:15 | %hhd | 0 | | | +| ../cases.h:57:10:57:14 | %hd | 0 | | | +| ../cases.h:58:10:58:14 | %jd | 0 | | | +| ../cases.h:59:10:59:14 | %zd | 0 | | | +| ../cases.h:60:10:60:14 | %td | 0 | | | +| ../cases.h:61:10:61:14 | %qd | 0 | | | +| ../cases.h:62:10:62:16 | %I64d | 0 | | | +| ../cases.h:63:10:63:13 | %x | 0 | | | +| ../cases.h:64:10:64:13 | %X | 0 | | | +| ../cases.h:65:10:65:13 | %o | 0 | | | +| ../cases.h:66:10:66:13 | %c | 0 | | | +| ../cases.h:67:10:67:13 | %s | 0 | | | +| ../cases.h:68:10:68:13 | %p | 0 | | | +| ../cases.h:69:10:69:13 | %f | 0 | | | +| ../cases.h:70:10:70:13 | %n | 0 | | | +| ../cases.h:71:10:71:13 | %d | 0 | | | +| ../cases.h:72:10:72:13 | %d | 0 | | | +| ../cases.h:73:10:73:13 | %d | 0 | | | +| ../cases.h:74:10:74:14 | %kd | 0 | | | diff --git a/cpp/ql/test/library-tests/printf/integerConversionSpecifier/Linux/suggestedSpecifier.ql b/cpp/ql/test/library-tests/printf/integerConversionSpecifier/Linux/suggestedSpecifier.ql new file mode 100644 index 000000000000..b8fbd970b81a --- /dev/null +++ b/cpp/ql/test/library-tests/printf/integerConversionSpecifier/Linux/suggestedSpecifier.ql @@ -0,0 +1,15 @@ +import semmle.code.cpp.commons.Printf + +from FormatLiteral format, int n +where exists(format.getConvSpecOffset(n)) +select format, n, + concat(Expr arg | + arg = format.getUse().getConversionArgument(n) + | + format.getSuggestedIntegerConversionSpecifier(n, arg.getFullyConverted().getType()), ", " + ), + concat(Expr arg | + arg = format.getUse().getConversionArgument(n) + | + format.getSuggestedIntegerConversionSpecifier(n, arg.getUnconverted().getType()), ", " + ) diff --git a/cpp/ql/test/library-tests/printf/integerConversionSpecifier/Linux/test.c b/cpp/ql/test/library-tests/printf/integerConversionSpecifier/Linux/test.c new file mode 100644 index 000000000000..9878439493e6 --- /dev/null +++ b/cpp/ql/test/library-tests/printf/integerConversionSpecifier/Linux/test.c @@ -0,0 +1 @@ +#include "../cases.h" diff --git a/cpp/ql/test/library-tests/printf/integerConversionSpecifier/Microsoft/options b/cpp/ql/test/library-tests/printf/integerConversionSpecifier/Microsoft/options new file mode 100644 index 000000000000..668c53efd7db --- /dev/null +++ b/cpp/ql/test/library-tests/printf/integerConversionSpecifier/Microsoft/options @@ -0,0 +1 @@ +semmle-extractor-options: --microsoft --edg --target --edg win64 diff --git a/cpp/ql/test/library-tests/printf/integerConversionSpecifier/Microsoft/suggestedSpecifier.expected b/cpp/ql/test/library-tests/printf/integerConversionSpecifier/Microsoft/suggestedSpecifier.expected new file mode 100644 index 000000000000..7fd732531929 --- /dev/null +++ b/cpp/ql/test/library-tests/printf/integerConversionSpecifier/Microsoft/suggestedSpecifier.expected @@ -0,0 +1,61 @@ +| ../cases.h:11:10:11:13 | %d | 0 | %d | %d | +| ../cases.h:12:10:12:13 | %d | 0 | %u | %u | +| ../cases.h:13:10:13:13 | %d | 0 | %ld | %ld | +| ../cases.h:14:10:14:13 | %d | 0 | %lu | %lu | +| ../cases.h:15:10:15:13 | %d | 0 | %lld | %lld | +| ../cases.h:16:10:16:13 | %d | 0 | %llu | %llu | +| ../cases.h:17:10:17:13 | %i | 0 | %i | %i | +| ../cases.h:18:10:18:13 | %i | 0 | %u | %u | +| ../cases.h:19:10:19:13 | %i | 0 | %li | %li | +| ../cases.h:20:10:20:13 | %i | 0 | %lu | %lu | +| ../cases.h:21:10:21:13 | %i | 0 | %lli | %lli | +| ../cases.h:22:10:22:13 | %i | 0 | %llu | %llu | +| ../cases.h:23:10:23:13 | %u | 0 | %d | %d | +| ../cases.h:24:10:24:14 | %ld | 0 | %d | %d | +| ../cases.h:25:10:25:14 | %li | 0 | %u | %u | +| ../cases.h:26:10:26:14 | %lu | 0 | %lld | %lld | +| ../cases.h:27:10:27:15 | %lld | 0 | %ld | %ld | +| ../cases.h:28:10:28:15 | %lli | 0 | %llu | %llu | +| ../cases.h:29:10:29:15 | %llu | 0 | %lu | %lu | +| ../cases.h:30:10:30:13 | %d | 0 | %lu | %lu | +| ../cases.h:32:10:32:15 | %lld | 0 | %d | | +| ../cases.h:33:10:33:15 | %lld | 0 | %d | | +| ../cases.h:34:10:34:15 | %lld | 0 | %d | | +| ../cases.h:35:10:35:15 | %lld | 0 | %d | | +| ../cases.h:36:10:36:15 | %lld | 0 | | | +| ../cases.h:38:10:38:23 | value=%d end | 0 | %llu | %llu | +| ../cases.h:39:10:39:19 | %% %d %i | 0 | %ld | %ld | +| ../cases.h:39:10:39:19 | %% %d %i | 1 | %llu | %llu | +| ../cases.h:42:10:42:14 | %+d | 0 | | | +| ../cases.h:43:10:43:14 | % d | 0 | | | +| ../cases.h:44:10:44:14 | %-d | 0 | | | +| ../cases.h:45:10:45:14 | %#d | 0 | | | +| ../cases.h:46:10:46:14 | %'d | 0 | | | +| ../cases.h:47:10:47:15 | %08d | 0 | | | +| ../cases.h:48:10:48:15 | %12d | 0 | | | +| ../cases.h:49:10:49:14 | %.d | 0 | | | +| ../cases.h:50:10:50:15 | %.3d | 0 | | | +| ../cases.h:51:10:51:14 | %*d | 0 | | | +| ../cases.h:52:10:52:15 | %.*d | 0 | | | +| ../cases.h:53:10:53:15 | %1$d | 0 | | | +| ../cases.h:54:10:54:20 | %2$d %1$i | 0 | | | +| ../cases.h:54:10:54:20 | %2$d %1$i | 1 | | | +| ../cases.h:56:10:56:15 | %hhd | 0 | | | +| ../cases.h:57:10:57:14 | %hd | 0 | | | +| ../cases.h:58:10:58:14 | %jd | 0 | | | +| ../cases.h:59:10:59:14 | %zd | 0 | | | +| ../cases.h:60:10:60:14 | %td | 0 | | | +| ../cases.h:61:10:61:14 | %qd | 0 | | | +| ../cases.h:62:10:62:16 | %I64d | 0 | | | +| ../cases.h:63:10:63:13 | %x | 0 | | | +| ../cases.h:64:10:64:13 | %X | 0 | | | +| ../cases.h:65:10:65:13 | %o | 0 | | | +| ../cases.h:66:10:66:13 | %c | 0 | | | +| ../cases.h:67:10:67:13 | %s | 0 | | | +| ../cases.h:68:10:68:13 | %p | 0 | | | +| ../cases.h:69:10:69:13 | %f | 0 | | | +| ../cases.h:70:10:70:13 | %n | 0 | | | +| ../cases.h:71:10:71:13 | %d | 0 | | | +| ../cases.h:72:10:72:13 | %d | 0 | | | +| ../cases.h:73:10:73:13 | %d | 0 | | | +| ../cases.h:74:10:74:14 | %kd | 0 | | | diff --git a/cpp/ql/test/library-tests/printf/integerConversionSpecifier/Microsoft/suggestedSpecifier.ql b/cpp/ql/test/library-tests/printf/integerConversionSpecifier/Microsoft/suggestedSpecifier.ql new file mode 100644 index 000000000000..b8fbd970b81a --- /dev/null +++ b/cpp/ql/test/library-tests/printf/integerConversionSpecifier/Microsoft/suggestedSpecifier.ql @@ -0,0 +1,15 @@ +import semmle.code.cpp.commons.Printf + +from FormatLiteral format, int n +where exists(format.getConvSpecOffset(n)) +select format, n, + concat(Expr arg | + arg = format.getUse().getConversionArgument(n) + | + format.getSuggestedIntegerConversionSpecifier(n, arg.getFullyConverted().getType()), ", " + ), + concat(Expr arg | + arg = format.getUse().getConversionArgument(n) + | + format.getSuggestedIntegerConversionSpecifier(n, arg.getUnconverted().getType()), ", " + ) diff --git a/cpp/ql/test/library-tests/printf/integerConversionSpecifier/Microsoft/test.c b/cpp/ql/test/library-tests/printf/integerConversionSpecifier/Microsoft/test.c new file mode 100644 index 000000000000..9878439493e6 --- /dev/null +++ b/cpp/ql/test/library-tests/printf/integerConversionSpecifier/Microsoft/test.c @@ -0,0 +1 @@ +#include "../cases.h" diff --git a/cpp/ql/test/library-tests/printf/integerConversionSpecifier/cases.h b/cpp/ql/test/library-tests/printf/integerConversionSpecifier/cases.h new file mode 100644 index 000000000000..499d7cdc3e5d --- /dev/null +++ b/cpp/ql/test/library-tests/printf/integerConversionSpecifier/cases.h @@ -0,0 +1,75 @@ +// Compile-only fixtures. Deliberately mismatched printf calls are not executed. +int printf(const char *format, ...); +typedef unsigned long UnsignedLong; +enum SmallEnum { SmallValue = 1 }; + +void integer_specifiers(int i, unsigned int ui, long l, unsigned long ul, + long long ll, unsigned long long ull, signed int si, + const UnsignedLong cul, short s, unsigned short us, + char c, _Bool b, enum SmallEnum e, float f, double d, int *p) { + // All six type categories, independently of whether the query reports a mismatch. + printf("%d", i); // EXPECT: %d + printf("%d", ui); // EXPECT: %u + printf("%d", l); // EXPECT: %ld + printf("%d", ul); // EXPECT: %lu + printf("%d", ll); // EXPECT: %lld + printf("%d", ull); // EXPECT: %llu + printf("%i", i); // EXPECT: %i + printf("%i", ui); // EXPECT: %u + printf("%i", l); // EXPECT: %li + printf("%i", ul); // EXPECT: %lu + printf("%i", ll); // EXPECT: %lli + printf("%i", ull); // EXPECT: %llu + printf("%u", i); // EXPECT: %d + printf("%ld", si); // EXPECT: %d + printf("%li", ui); // EXPECT: %u + printf("%lu", ll); // EXPECT: %lld + printf("%lld", l); // EXPECT: %ld + printf("%lli", ull); // EXPECT: %llu + printf("%llu", ul); // EXPECT: %lu + printf("%d", cul); // EXPECT: %lu + // The caller supplies promoted types; raw narrow types and enums are unsupported. + printf("%lld", s); // EXPECT: %d; RAW: none + printf("%lld", us); // EXPECT: %d; RAW: none + printf("%lld", c); // EXPECT: %d; RAW: none + printf("%lld", b); // EXPECT: %d; RAW: none + printf("%lld", e); // EXPECT: none + // Per-conversion mapping, literal text, and escaped percent signs. + printf("value=%d end", ull); // EXPECT: %llu + printf("%% %d %i", l, ull); // EXPECT: %ld,%llu + printf("%%", i); // NO_CONVERSION + // Unsupported decorators and positional conversions must have no result. + printf("%+d", ll); // EXPECT: none + printf("% d", ll); // EXPECT: none + printf("%-d", ll); // EXPECT: none + printf("%#d", ll); // EXPECT: none + printf("%'d", ll); // EXPECT: none + printf("%08d", ll); // EXPECT: none + printf("%12d", ll); // EXPECT: none + printf("%.d", ll); // EXPECT: none + printf("%.3d", ll); // EXPECT: none + printf("%*d", ll, i); // EXPECT: none + printf("%.*d", ll, i); // EXPECT: none + printf("%1$d", ll); // EXPECT: none + printf("%2$d %1$i", ll, ull); // EXPECT: none,none + // Unsupported length flags, conversions, types, and malformed formats. + printf("%hhd", ll); // EXPECT: none + printf("%hd", ll); // EXPECT: none + printf("%jd", ll); // EXPECT: none + printf("%zd", ll); // EXPECT: none + printf("%td", ll); // EXPECT: none + printf("%qd", ll); // EXPECT: none + printf("%I64d", ll); // EXPECT: none + printf("%x", ull); // EXPECT: none + printf("%X", ull); // EXPECT: none + printf("%o", ull); // EXPECT: none + printf("%c", ll); // EXPECT: none + printf("%s", ll); // EXPECT: none + printf("%p", ll); // EXPECT: none + printf("%f", i); // EXPECT: none + printf("%n", p); // EXPECT: none + printf("%d", f); // EXPECT: none + printf("%d", d); // EXPECT: none + printf("%d", p); // EXPECT: none + printf("%kd", ll); // EXPECT: none +} From 30acf5c93992b300016de5174b68a37ffb1f4645 Mon Sep 17 00:00:00 2001 From: SkyCipherr Date: Mon, 16 Nov 2026 17:31:24 +0800 Subject: [PATCH 3/3] C++: Share printf integer conversion rules with specifier suggestions Use the existing length and signedness model for both conversion type inference and reverse specifier suggestions. Preserve narrow display types, target-specific extensions, and the existing suggestion scope. Add Linux and Microsoft regression coverage recorded against the prior model. All 26 printf library and format query tests pass; real-GCC messages and locations remain unchanged. --- cpp/ql/lib/semmle/code/cpp/commons/Printf.qll | 146 +++++++----------- .../Linux/integerTypes.expected | 108 +++++++++++++ .../Linux/integerTypes.ql | 8 + .../integerConversionTypes/Linux/options | 1 + .../integerConversionTypes/Linux/test.c | 1 + .../Microsoft/integerTypes.expected | 108 +++++++++++++ .../Microsoft/integerTypes.ql | 8 + .../integerConversionTypes/Microsoft/options | 1 + .../integerConversionTypes/Microsoft/test.c | 1 + .../printf/integerConversionTypes/cases.h | 116 ++++++++++++++ 10 files changed, 405 insertions(+), 93 deletions(-) create mode 100644 cpp/ql/test/library-tests/printf/integerConversionTypes/Linux/integerTypes.expected create mode 100644 cpp/ql/test/library-tests/printf/integerConversionTypes/Linux/integerTypes.ql create mode 100644 cpp/ql/test/library-tests/printf/integerConversionTypes/Linux/options create mode 100644 cpp/ql/test/library-tests/printf/integerConversionTypes/Linux/test.c create mode 100644 cpp/ql/test/library-tests/printf/integerConversionTypes/Microsoft/integerTypes.expected create mode 100644 cpp/ql/test/library-tests/printf/integerConversionTypes/Microsoft/integerTypes.ql create mode 100644 cpp/ql/test/library-tests/printf/integerConversionTypes/Microsoft/options create mode 100644 cpp/ql/test/library-tests/printf/integerConversionTypes/Microsoft/test.c create mode 100644 cpp/ql/test/library-tests/printf/integerConversionTypes/cases.h diff --git a/cpp/ql/lib/semmle/code/cpp/commons/Printf.qll b/cpp/ql/lib/semmle/code/cpp/commons/Printf.qll index f08338194815..488ea6db679c 100644 --- a/cpp/ql/lib/semmle/code/cpp/commons/Printf.qll +++ b/cpp/ql/lib/semmle/code/cpp/commons/Printf.qll @@ -717,25 +717,13 @@ class FormatLiteral extends Literal instanceof StringLiteral { * This does not check whether the existing specifier matches `type`. */ string getSuggestedIntegerConversionSpecifier(int n, Type type) { - exists(IntegralType t, string length, string conversion, string originalConversion | + exists(string length, string conversion, string originalConversion | this.parseConvSpec(n, _, "", "", "", "", ["", "l", "ll"], originalConversion) and originalConversion = ["d", "i", "u"] and - t = type.getUnspecifiedType() and - ( - t instanceof IntType and length = "" - or - t instanceof LongType and length = "l" - or - t instanceof LongLongType and length = "ll" - ) and - ( - if t.isUnsigned() - then conversion = "u" - else - if originalConversion = "i" - then conversion = "i" - else conversion = "d" - ) and + length = ["", "l", "ll"] and + (if originalConversion = "i" then conversion = ["i", "u"] else conversion = ["d", "u"]) and + this.getIntegerConversionType(length, conversion) = + type.getUnspecifiedType().(IntegralType).getCanonicalArithmeticType() and result = "%" + length + conversion ) } @@ -795,38 +783,36 @@ class FormatLiteral extends Literal instanceof StringLiteral { * Gets the family of integral types required by the nth conversion * specifier's length flag. */ - Type getIntegralConversion(int n) { - exists(string len | - len = this.getLength(n) and - ( - len = "hh" and result instanceof IntType - or - len = "h" and result instanceof IntType - or - len = "l" and result = this.getLongType() - or - len = ["ll", "L", "q"] and - result instanceof LongLongType - or - len = "j" and result = this.getIntmax_t() - or - len = ["z", "Z"] and - (result = this.getSize_t() or result = this.getSsize_t()) - or - len = "t" and result = this.getPtrdiff_t() - or - len = "I" and - (result = this.getSize_t() or result = this.getPtrdiff_t()) - or - len = "I32" and - exists(MicrosoftInt32Type t | t.getUnsigned() = result.(IntegralType).getUnsigned()) - or - len = "I64" and - exists(MicrosoftInt64Type t | t.getUnsigned() = result.(IntegralType).getUnsigned()) - or - len = "" and result instanceof IntType - ) - ) + Type getIntegralConversion(int n) { result = this.getIntegralTypeForLength(this.getLength(n)) } + + /** Gets the integer type family associated with a length flag. */ + private Type getIntegralTypeForLength(string len) { + len = "hh" and result instanceof IntType + or + len = "h" and result instanceof IntType + or + len = "l" and result = this.getLongType() + or + len = ["ll", "L", "q"] and + result instanceof LongLongType + or + len = "j" and result = this.getIntmax_t() + or + len = ["z", "Z"] and + (result = this.getSize_t() or result = this.getSsize_t()) + or + len = "t" and result = this.getPtrdiff_t() + or + len = "I" and + (result = this.getSize_t() or result = this.getPtrdiff_t()) + or + len = "I32" and + exists(MicrosoftInt32Type t | t.getUnsigned() = result.(IntegralType).getUnsigned()) + or + len = "I64" and + exists(MicrosoftInt64Type t | t.getUnsigned() = result.(IntegralType).getUnsigned()) + or + len = "" and result instanceof IntType } /** @@ -834,36 +820,12 @@ class FormatLiteral extends Literal instanceof StringLiteral { * conversion specifier's length flag. */ Type getIntegralDisplayType(int n) { - exists(string len | - len = this.getLength(n) and - ( - len = "hh" and result instanceof CharType - or - len = "h" and result instanceof ShortType - or - len = "l" and result = this.getLongType() - or - len = ["ll", "L", "q"] and - result instanceof LongLongType - or - len = "j" and result = this.getIntmax_t() - or - len = ["z", "Z"] and - (result = this.getSize_t() or result = this.getSsize_t()) - or - len = "t" and result = this.getPtrdiff_t() - or - len = "I" and - (result = this.getSize_t() or result = this.getPtrdiff_t()) - or - len = "I32" and - exists(MicrosoftInt32Type t | t.getUnsigned() = result.(IntegralType).getUnsigned()) - or - len = "I64" and - exists(MicrosoftInt64Type t | t.getUnsigned() = result.(IntegralType).getUnsigned()) - or - len = "" and result instanceof IntType - ) + exists(string len | len = this.getLength(n) | + len = "hh" and result instanceof CharType + or + len = "h" and result instanceof ShortType + or + not len = ["hh", "h"] and result = this.getIntegralTypeForLength(len) ) } @@ -905,9 +867,8 @@ class FormatLiteral extends Literal instanceof StringLiteral { * Gets the argument type required by the nth conversion specifier. */ Type getConversionType(int n) { - result = this.getConversionType1(n) or + result = this.getIntegerConversionType(this.getLength(n), this.getConversionChar(n)) or result = this.getConversionType1b(n) or - result = this.getConversionType2(n) or result = this.getConversionType3(n) or result = this.getConversionType4(n) or result = this.getConversionType6(n) or @@ -917,12 +878,19 @@ class FormatLiteral extends Literal instanceof StringLiteral { result = this.getConversionType10(n) } - private Type getConversionType1(int n) { - exists(string cnv | cnv = this.getConversionChar(n) | - cnv = ["d", "i"] and - result = this.getIntegralConversion(n) and + /** + * Gets the integer argument type for a length flag and conversion character. + * Shared by conversion type inference and replacement specifier suggestions. + */ + private Type getIntegerConversionType(string len, string conv) { + result = this.getIntegralTypeForLength(len) and + ( + conv = ["d", "i"] and not result.getUnderlyingType().(IntegralType).isExplicitlySigned() and not result.getUnderlyingType().(IntegralType).isExplicitlyUnsigned() + or + conv = ["o", "u", "x", "X"] and + result.getUnderlyingType().(IntegralType).isUnsigned() ) } @@ -957,14 +925,6 @@ class FormatLiteral extends Literal instanceof StringLiteral { ) } - private Type getConversionType2(int n) { - exists(string cnv | cnv = this.getConversionChar(n) | - cnv = ["o", "u", "x", "X"] and - result = this.getIntegralConversion(n) and - result.getUnderlyingType().(IntegralType).isUnsigned() - ) - } - private Type getConversionType3(int n) { exists(string cnv | cnv = this.getConversionChar(n) | cnv = ["a", "A", "e", "E", "f", "F", "g", "G"] and result = this.getFloatingPointConversion(n) diff --git a/cpp/ql/test/library-tests/printf/integerConversionTypes/Linux/integerTypes.expected b/cpp/ql/test/library-tests/printf/integerConversionTypes/Linux/integerTypes.expected new file mode 100644 index 000000000000..ecf308082149 --- /dev/null +++ b/cpp/ql/test/library-tests/printf/integerConversionTypes/Linux/integerTypes.expected @@ -0,0 +1,108 @@ +| ../cases.h:16:10:16:13 | %d | 0 | int, signed int, unsigned int | int, signed int, unsigned int | int | | +| ../cases.h:17:10:17:13 | %i | 0 | int, signed int, unsigned int | int, signed int, unsigned int | int | | +| ../cases.h:18:10:18:13 | %u | 0 | int, signed int, unsigned int | int, signed int, unsigned int | unsigned int | | +| ../cases.h:19:10:19:13 | %o | 0 | int, signed int, unsigned int | int, signed int, unsigned int | unsigned int | | +| ../cases.h:20:10:20:13 | %x | 0 | int, signed int, unsigned int | int, signed int, unsigned int | unsigned int | | +| ../cases.h:21:10:21:13 | %X | 0 | int, signed int, unsigned int | int, signed int, unsigned int | unsigned int | | +| ../cases.h:22:10:22:13 | %n | 0 | int, signed int, unsigned int | int, signed int, unsigned int | | | +| ../cases.h:23:10:23:15 | %hhd | 0 | int, signed int, unsigned int | char, signed char, unsigned char | int | char * | +| ../cases.h:24:10:24:15 | %hhi | 0 | int, signed int, unsigned int | char, signed char, unsigned char | int | char * | +| ../cases.h:25:10:25:15 | %hhu | 0 | int, signed int, unsigned int | char, signed char, unsigned char | unsigned int | char * | +| ../cases.h:26:10:26:15 | %hho | 0 | int, signed int, unsigned int | char, signed char, unsigned char | unsigned int | char * | +| ../cases.h:27:10:27:15 | %hhx | 0 | int, signed int, unsigned int | char, signed char, unsigned char | unsigned int | char * | +| ../cases.h:28:10:28:15 | %hhX | 0 | int, signed int, unsigned int | char, signed char, unsigned char | unsigned int | char * | +| ../cases.h:29:10:29:15 | %hhn | 0 | int, signed int, unsigned int | char, signed char, unsigned char | char * | char * | +| ../cases.h:30:10:30:14 | %hd | 0 | int, signed int, unsigned int | short, signed short, unsigned short | int | | +| ../cases.h:31:10:31:14 | %hi | 0 | int, signed int, unsigned int | short, signed short, unsigned short | int | | +| ../cases.h:32:10:32:14 | %hu | 0 | int, signed int, unsigned int | short, signed short, unsigned short | unsigned int | | +| ../cases.h:33:10:33:14 | %ho | 0 | int, signed int, unsigned int | short, signed short, unsigned short | unsigned int | | +| ../cases.h:34:10:34:14 | %hx | 0 | int, signed int, unsigned int | short, signed short, unsigned short | unsigned int | | +| ../cases.h:35:10:35:14 | %hX | 0 | int, signed int, unsigned int | short, signed short, unsigned short | unsigned int | | +| ../cases.h:36:10:36:14 | %hn | 0 | int, signed int, unsigned int | short, signed short, unsigned short | | | +| ../cases.h:37:10:37:14 | %ld | 0 | long, signed long, unsigned long | long, signed long, unsigned long | long | | +| ../cases.h:38:10:38:14 | %li | 0 | long, signed long, unsigned long | long, signed long, unsigned long | long | | +| ../cases.h:39:10:39:14 | %lu | 0 | long, signed long, unsigned long | long, signed long, unsigned long | unsigned long | | +| ../cases.h:40:10:40:14 | %lo | 0 | long, signed long, unsigned long | long, signed long, unsigned long | unsigned long | | +| ../cases.h:41:10:41:14 | %lx | 0 | long, signed long, unsigned long | long, signed long, unsigned long | unsigned long | | +| ../cases.h:42:10:42:14 | %lX | 0 | long, signed long, unsigned long | long, signed long, unsigned long | unsigned long | | +| ../cases.h:43:10:43:14 | %ln | 0 | long, signed long, unsigned long | long, signed long, unsigned long | | | +| ../cases.h:44:10:44:15 | %lld | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | long long | | +| ../cases.h:45:10:45:15 | %lli | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | long long | | +| ../cases.h:46:10:46:15 | %llu | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | unsigned long long | | +| ../cases.h:47:10:47:15 | %llo | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | unsigned long long | | +| ../cases.h:48:10:48:15 | %llx | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | unsigned long long | | +| ../cases.h:49:10:49:15 | %llX | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | unsigned long long | | +| ../cases.h:50:10:50:15 | %lln | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | | | +| ../cases.h:51:10:51:14 | %Ld | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | long long | | +| ../cases.h:52:10:52:14 | %Li | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | long long | | +| ../cases.h:53:10:53:14 | %Lu | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | unsigned long long | | +| ../cases.h:54:10:54:14 | %Lo | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | unsigned long long | | +| ../cases.h:55:10:55:14 | %Lx | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | unsigned long long | | +| ../cases.h:56:10:56:14 | %LX | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | unsigned long long | | +| ../cases.h:57:10:57:14 | %Ln | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | | | +| ../cases.h:58:10:58:14 | %qd | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | long long | | +| ../cases.h:59:10:59:14 | %qi | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | long long | | +| ../cases.h:60:10:60:14 | %qu | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | unsigned long long | | +| ../cases.h:61:10:61:14 | %qo | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | unsigned long long | | +| ../cases.h:62:10:62:14 | %qx | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | unsigned long long | | +| ../cases.h:63:10:63:14 | %qX | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | unsigned long long | | +| ../cases.h:64:10:64:14 | %qn | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | | | +| ../cases.h:65:10:65:14 | %jd | 0 | intmax_t | intmax_t | intmax_t | | +| ../cases.h:66:10:66:14 | %ji | 0 | intmax_t | intmax_t | intmax_t | | +| ../cases.h:67:10:67:14 | %ju | 0 | intmax_t | intmax_t | | | +| ../cases.h:68:10:68:14 | %jo | 0 | intmax_t | intmax_t | | | +| ../cases.h:69:10:69:14 | %jx | 0 | intmax_t | intmax_t | | | +| ../cases.h:70:10:70:14 | %jX | 0 | intmax_t | intmax_t | | | +| ../cases.h:71:10:71:14 | %jn | 0 | intmax_t | intmax_t | | | +| ../cases.h:72:10:72:14 | %zd | 0 | size_t, ssize_t | size_t, ssize_t | ssize_t | | +| ../cases.h:73:10:73:14 | %zi | 0 | size_t, ssize_t | size_t, ssize_t | ssize_t | | +| ../cases.h:74:10:74:14 | %zu | 0 | size_t, ssize_t | size_t, ssize_t | size_t | | +| ../cases.h:75:10:75:14 | %zo | 0 | size_t, ssize_t | size_t, ssize_t | size_t | | +| ../cases.h:76:10:76:14 | %zx | 0 | size_t, ssize_t | size_t, ssize_t | size_t | | +| ../cases.h:77:10:77:14 | %zX | 0 | size_t, ssize_t | size_t, ssize_t | size_t | | +| ../cases.h:78:10:78:14 | %zn | 0 | size_t, ssize_t | size_t, ssize_t | | | +| ../cases.h:79:10:79:14 | %Zd | 0 | size_t, ssize_t | size_t, ssize_t | ssize_t | | +| ../cases.h:80:10:80:14 | %Zi | 0 | size_t, ssize_t | size_t, ssize_t | ssize_t | | +| ../cases.h:81:10:81:14 | %Zu | 0 | size_t, ssize_t | size_t, ssize_t | size_t | | +| ../cases.h:82:10:82:14 | %Zo | 0 | size_t, ssize_t | size_t, ssize_t | size_t | | +| ../cases.h:83:10:83:14 | %Zx | 0 | size_t, ssize_t | size_t, ssize_t | size_t | | +| ../cases.h:84:10:84:14 | %ZX | 0 | size_t, ssize_t | size_t, ssize_t | size_t | | +| ../cases.h:85:10:85:14 | %Zn | 0 | size_t, ssize_t | size_t, ssize_t | | | +| ../cases.h:86:10:86:14 | %td | 0 | ptrdiff_t | ptrdiff_t | ptrdiff_t | | +| ../cases.h:87:10:87:14 | %ti | 0 | ptrdiff_t | ptrdiff_t | ptrdiff_t | | +| ../cases.h:88:10:88:14 | %tu | 0 | ptrdiff_t | ptrdiff_t | | | +| ../cases.h:89:10:89:14 | %to | 0 | ptrdiff_t | ptrdiff_t | | | +| ../cases.h:90:10:90:14 | %tx | 0 | ptrdiff_t | ptrdiff_t | | | +| ../cases.h:91:10:91:14 | %tX | 0 | ptrdiff_t | ptrdiff_t | | | +| ../cases.h:92:10:92:14 | %tn | 0 | ptrdiff_t | ptrdiff_t | | | +| ../cases.h:93:10:93:14 | %Id | 0 | int, signed int, unsigned int | int, signed int, unsigned int | int | | +| ../cases.h:94:10:94:14 | %Ii | 0 | int, signed int, unsigned int | int, signed int, unsigned int | int | | +| ../cases.h:95:10:95:14 | %Iu | 0 | int, signed int, unsigned int | int, signed int, unsigned int | unsigned int | | +| ../cases.h:96:10:96:14 | %Io | 0 | int, signed int, unsigned int | int, signed int, unsigned int | unsigned int | | +| ../cases.h:97:10:97:14 | %Ix | 0 | int, signed int, unsigned int | int, signed int, unsigned int | unsigned int | | +| ../cases.h:98:10:98:14 | %IX | 0 | int, signed int, unsigned int | int, signed int, unsigned int | unsigned int | | +| ../cases.h:99:10:99:14 | %In | 0 | int, signed int, unsigned int | int, signed int, unsigned int | | | +| ../cases.h:100:10:100:16 | %I32d | 0 | int, signed int, unsigned int | int, signed int, unsigned int | int | | +| ../cases.h:101:10:101:16 | %I32i | 0 | int, signed int, unsigned int | int, signed int, unsigned int | int | | +| ../cases.h:102:10:102:16 | %I32u | 0 | int, signed int, unsigned int | int, signed int, unsigned int | unsigned int | | +| ../cases.h:103:10:103:16 | %I32o | 0 | int, signed int, unsigned int | int, signed int, unsigned int | unsigned int | | +| ../cases.h:104:10:104:16 | %I32x | 0 | int, signed int, unsigned int | int, signed int, unsigned int | unsigned int | | +| ../cases.h:105:10:105:16 | %I32X | 0 | int, signed int, unsigned int | int, signed int, unsigned int | unsigned int | | +| ../cases.h:106:10:106:16 | %I32n | 0 | int, signed int, unsigned int | int, signed int, unsigned int | | | +| ../cases.h:107:10:107:16 | %I64d | 0 | int, signed int, unsigned int | int, signed int, unsigned int | int | | +| ../cases.h:108:10:108:16 | %I64i | 0 | int, signed int, unsigned int | int, signed int, unsigned int | int | | +| ../cases.h:109:10:109:16 | %I64u | 0 | int, signed int, unsigned int | int, signed int, unsigned int | unsigned int | | +| ../cases.h:110:10:110:16 | %I64o | 0 | int, signed int, unsigned int | int, signed int, unsigned int | unsigned int | | +| ../cases.h:111:10:111:16 | %I64x | 0 | int, signed int, unsigned int | int, signed int, unsigned int | unsigned int | | +| ../cases.h:112:10:112:16 | %I64X | 0 | int, signed int, unsigned int | int, signed int, unsigned int | unsigned int | | +| ../cases.h:113:10:113:16 | %I64n | 0 | int, signed int, unsigned int | int, signed int, unsigned int | | | +| ../cases.h:114:10:114:38 | %08lld %.3lu %*d %.*u %1$li | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | long long | | +| ../cases.h:114:10:114:38 | %08lld %.3lu %*d %.*u %1$li | 1 | long, signed long, unsigned long | long, signed long, unsigned long | unsigned long | | +| ../cases.h:114:10:114:38 | %08lld %.3lu %*d %.*u %1$li | 2 | int, signed int, unsigned int | int, signed int, unsigned int | int | | +| ../cases.h:114:10:114:38 | %08lld %.3lu %*d %.*u %1$li | 3 | int, signed int, unsigned int | int, signed int, unsigned int | unsigned int | | +| ../cases.h:114:10:114:38 | %08lld %.3lu %*d %.*u %1$li | 4 | long, signed long, unsigned long | long, signed long, unsigned long | long | | +| ../cases.h:115:10:115:26 | %f %Lf %s %p %c | 0 | int, signed int, unsigned int | int, signed int, unsigned int | double | | +| ../cases.h:115:10:115:26 | %f %Lf %s %p %c | 1 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | long double | | +| ../cases.h:115:10:115:26 | %f %Lf %s %p %c | 2 | int, signed int, unsigned int | int, signed int, unsigned int | char * | | +| ../cases.h:115:10:115:26 | %f %Lf %s %p %c | 3 | int, signed int, unsigned int | int, signed int, unsigned int | void * | | +| ../cases.h:115:10:115:26 | %f %Lf %s %p %c | 4 | int, signed int, unsigned int | int, signed int, unsigned int | char | | diff --git a/cpp/ql/test/library-tests/printf/integerConversionTypes/Linux/integerTypes.ql b/cpp/ql/test/library-tests/printf/integerConversionTypes/Linux/integerTypes.ql new file mode 100644 index 000000000000..22d16003f9c1 --- /dev/null +++ b/cpp/ql/test/library-tests/printf/integerConversionTypes/Linux/integerTypes.ql @@ -0,0 +1,8 @@ +import semmle.code.cpp.commons.Printf + +from FormatLiteral format, int n +where exists(format.getConvSpecOffset(n)) +select format, n, concat(format.getIntegralConversion(n).toString(), ", "), + concat(format.getIntegralDisplayType(n).toString(), ", "), + concat(format.getConversionType(n).toString(), ", "), + concat(format.getStorePointerConversion(n).toString(), ", ") diff --git a/cpp/ql/test/library-tests/printf/integerConversionTypes/Linux/options b/cpp/ql/test/library-tests/printf/integerConversionTypes/Linux/options new file mode 100644 index 000000000000..fcea28bbb584 --- /dev/null +++ b/cpp/ql/test/library-tests/printf/integerConversionTypes/Linux/options @@ -0,0 +1 @@ +semmle-extractor-options: --clang --edg --signed_chars diff --git a/cpp/ql/test/library-tests/printf/integerConversionTypes/Linux/test.c b/cpp/ql/test/library-tests/printf/integerConversionTypes/Linux/test.c new file mode 100644 index 000000000000..9878439493e6 --- /dev/null +++ b/cpp/ql/test/library-tests/printf/integerConversionTypes/Linux/test.c @@ -0,0 +1 @@ +#include "../cases.h" diff --git a/cpp/ql/test/library-tests/printf/integerConversionTypes/Microsoft/integerTypes.expected b/cpp/ql/test/library-tests/printf/integerConversionTypes/Microsoft/integerTypes.expected new file mode 100644 index 000000000000..cdc60db0201c --- /dev/null +++ b/cpp/ql/test/library-tests/printf/integerConversionTypes/Microsoft/integerTypes.expected @@ -0,0 +1,108 @@ +| ../cases.h:16:10:16:13 | %d | 0 | int, signed int, unsigned int | int, signed int, unsigned int | int | | +| ../cases.h:17:10:17:13 | %i | 0 | int, signed int, unsigned int | int, signed int, unsigned int | int | | +| ../cases.h:18:10:18:13 | %u | 0 | int, signed int, unsigned int | int, signed int, unsigned int | unsigned int | | +| ../cases.h:19:10:19:13 | %o | 0 | int, signed int, unsigned int | int, signed int, unsigned int | unsigned int | | +| ../cases.h:20:10:20:13 | %x | 0 | int, signed int, unsigned int | int, signed int, unsigned int | unsigned int | | +| ../cases.h:21:10:21:13 | %X | 0 | int, signed int, unsigned int | int, signed int, unsigned int | unsigned int | | +| ../cases.h:22:10:22:13 | %n | 0 | int, signed int, unsigned int | int, signed int, unsigned int | | | +| ../cases.h:23:10:23:15 | %hhd | 0 | int, signed int, unsigned int | char, signed char, unsigned char | int | char * | +| ../cases.h:24:10:24:15 | %hhi | 0 | int, signed int, unsigned int | char, signed char, unsigned char | int | char * | +| ../cases.h:25:10:25:15 | %hhu | 0 | int, signed int, unsigned int | char, signed char, unsigned char | unsigned int | char * | +| ../cases.h:26:10:26:15 | %hho | 0 | int, signed int, unsigned int | char, signed char, unsigned char | unsigned int | char * | +| ../cases.h:27:10:27:15 | %hhx | 0 | int, signed int, unsigned int | char, signed char, unsigned char | unsigned int | char * | +| ../cases.h:28:10:28:15 | %hhX | 0 | int, signed int, unsigned int | char, signed char, unsigned char | unsigned int | char * | +| ../cases.h:29:10:29:15 | %hhn | 0 | int, signed int, unsigned int | char, signed char, unsigned char | char * | char * | +| ../cases.h:30:10:30:14 | %hd | 0 | int, signed int, unsigned int | short, signed short, unsigned short | int | | +| ../cases.h:31:10:31:14 | %hi | 0 | int, signed int, unsigned int | short, signed short, unsigned short | int | | +| ../cases.h:32:10:32:14 | %hu | 0 | int, signed int, unsigned int | short, signed short, unsigned short | unsigned int | | +| ../cases.h:33:10:33:14 | %ho | 0 | int, signed int, unsigned int | short, signed short, unsigned short | unsigned int | | +| ../cases.h:34:10:34:14 | %hx | 0 | int, signed int, unsigned int | short, signed short, unsigned short | unsigned int | | +| ../cases.h:35:10:35:14 | %hX | 0 | int, signed int, unsigned int | short, signed short, unsigned short | unsigned int | | +| ../cases.h:36:10:36:14 | %hn | 0 | int, signed int, unsigned int | short, signed short, unsigned short | | | +| ../cases.h:37:10:37:14 | %ld | 0 | long, signed long, unsigned long | long, signed long, unsigned long | long | | +| ../cases.h:38:10:38:14 | %li | 0 | long, signed long, unsigned long | long, signed long, unsigned long | long | | +| ../cases.h:39:10:39:14 | %lu | 0 | long, signed long, unsigned long | long, signed long, unsigned long | unsigned long | | +| ../cases.h:40:10:40:14 | %lo | 0 | long, signed long, unsigned long | long, signed long, unsigned long | unsigned long | | +| ../cases.h:41:10:41:14 | %lx | 0 | long, signed long, unsigned long | long, signed long, unsigned long | unsigned long | | +| ../cases.h:42:10:42:14 | %lX | 0 | long, signed long, unsigned long | long, signed long, unsigned long | unsigned long | | +| ../cases.h:43:10:43:14 | %ln | 0 | long, signed long, unsigned long | long, signed long, unsigned long | | | +| ../cases.h:44:10:44:15 | %lld | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | long long | | +| ../cases.h:45:10:45:15 | %lli | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | long long | | +| ../cases.h:46:10:46:15 | %llu | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | unsigned long long | | +| ../cases.h:47:10:47:15 | %llo | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | unsigned long long | | +| ../cases.h:48:10:48:15 | %llx | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | unsigned long long | | +| ../cases.h:49:10:49:15 | %llX | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | unsigned long long | | +| ../cases.h:50:10:50:15 | %lln | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | | | +| ../cases.h:51:10:51:14 | %Ld | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | long long | | +| ../cases.h:52:10:52:14 | %Li | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | long long | | +| ../cases.h:53:10:53:14 | %Lu | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | unsigned long long | | +| ../cases.h:54:10:54:14 | %Lo | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | unsigned long long | | +| ../cases.h:55:10:55:14 | %Lx | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | unsigned long long | | +| ../cases.h:56:10:56:14 | %LX | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | unsigned long long | | +| ../cases.h:57:10:57:14 | %Ln | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | | | +| ../cases.h:58:10:58:14 | %qd | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | long long | | +| ../cases.h:59:10:59:14 | %qi | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | long long | | +| ../cases.h:60:10:60:14 | %qu | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | unsigned long long | | +| ../cases.h:61:10:61:14 | %qo | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | unsigned long long | | +| ../cases.h:62:10:62:14 | %qx | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | unsigned long long | | +| ../cases.h:63:10:63:14 | %qX | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | unsigned long long | | +| ../cases.h:64:10:64:14 | %qn | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | | | +| ../cases.h:65:10:65:14 | %jd | 0 | intmax_t | intmax_t | intmax_t | | +| ../cases.h:66:10:66:14 | %ji | 0 | intmax_t | intmax_t | intmax_t | | +| ../cases.h:67:10:67:14 | %ju | 0 | intmax_t | intmax_t | | | +| ../cases.h:68:10:68:14 | %jo | 0 | intmax_t | intmax_t | | | +| ../cases.h:69:10:69:14 | %jx | 0 | intmax_t | intmax_t | | | +| ../cases.h:70:10:70:14 | %jX | 0 | intmax_t | intmax_t | | | +| ../cases.h:71:10:71:14 | %jn | 0 | intmax_t | intmax_t | | | +| ../cases.h:72:10:72:14 | %zd | 0 | size_t, ssize_t | size_t, ssize_t | ssize_t | | +| ../cases.h:73:10:73:14 | %zi | 0 | size_t, ssize_t | size_t, ssize_t | ssize_t | | +| ../cases.h:74:10:74:14 | %zu | 0 | size_t, ssize_t | size_t, ssize_t | size_t | | +| ../cases.h:75:10:75:14 | %zo | 0 | size_t, ssize_t | size_t, ssize_t | size_t | | +| ../cases.h:76:10:76:14 | %zx | 0 | size_t, ssize_t | size_t, ssize_t | size_t | | +| ../cases.h:77:10:77:14 | %zX | 0 | size_t, ssize_t | size_t, ssize_t | size_t | | +| ../cases.h:78:10:78:14 | %zn | 0 | size_t, ssize_t | size_t, ssize_t | | | +| ../cases.h:79:10:79:14 | %Zd | 0 | int, signed int, unsigned int | int, signed int, unsigned int | | | +| ../cases.h:80:10:80:14 | %Zi | 0 | int, signed int, unsigned int | int, signed int, unsigned int | | | +| ../cases.h:81:10:81:14 | %Zu | 0 | int, signed int, unsigned int | int, signed int, unsigned int | | | +| ../cases.h:82:10:82:14 | %Zo | 0 | int, signed int, unsigned int | int, signed int, unsigned int | | | +| ../cases.h:83:10:83:14 | %Zx | 0 | int, signed int, unsigned int | int, signed int, unsigned int | | | +| ../cases.h:84:10:84:14 | %ZX | 0 | int, signed int, unsigned int | int, signed int, unsigned int | | | +| ../cases.h:85:10:85:14 | %Zn | 0 | int, signed int, unsigned int | int, signed int, unsigned int | | | +| ../cases.h:86:10:86:14 | %td | 0 | ptrdiff_t | ptrdiff_t | ptrdiff_t | | +| ../cases.h:87:10:87:14 | %ti | 0 | ptrdiff_t | ptrdiff_t | ptrdiff_t | | +| ../cases.h:88:10:88:14 | %tu | 0 | ptrdiff_t | ptrdiff_t | | | +| ../cases.h:89:10:89:14 | %to | 0 | ptrdiff_t | ptrdiff_t | | | +| ../cases.h:90:10:90:14 | %tx | 0 | ptrdiff_t | ptrdiff_t | | | +| ../cases.h:91:10:91:14 | %tX | 0 | ptrdiff_t | ptrdiff_t | | | +| ../cases.h:92:10:92:14 | %tn | 0 | ptrdiff_t | ptrdiff_t | | | +| ../cases.h:93:10:93:14 | %Id | 0 | ptrdiff_t, size_t | ptrdiff_t, size_t | ptrdiff_t | | +| ../cases.h:94:10:94:14 | %Ii | 0 | ptrdiff_t, size_t | ptrdiff_t, size_t | ptrdiff_t | | +| ../cases.h:95:10:95:14 | %Iu | 0 | ptrdiff_t, size_t | ptrdiff_t, size_t | size_t | | +| ../cases.h:96:10:96:14 | %Io | 0 | ptrdiff_t, size_t | ptrdiff_t, size_t | size_t | | +| ../cases.h:97:10:97:14 | %Ix | 0 | ptrdiff_t, size_t | ptrdiff_t, size_t | size_t | | +| ../cases.h:98:10:98:14 | %IX | 0 | ptrdiff_t, size_t | ptrdiff_t, size_t | size_t | | +| ../cases.h:99:10:99:14 | %In | 0 | ptrdiff_t, size_t | ptrdiff_t, size_t | | | +| ../cases.h:100:10:100:16 | %I32d | 0 | int, signed int, unsigned int | int, signed int, unsigned int | int | | +| ../cases.h:101:10:101:16 | %I32i | 0 | int, signed int, unsigned int | int, signed int, unsigned int | int | | +| ../cases.h:102:10:102:16 | %I32u | 0 | int, signed int, unsigned int | int, signed int, unsigned int | unsigned int | | +| ../cases.h:103:10:103:16 | %I32o | 0 | int, signed int, unsigned int | int, signed int, unsigned int | unsigned int | | +| ../cases.h:104:10:104:16 | %I32x | 0 | int, signed int, unsigned int | int, signed int, unsigned int | unsigned int | | +| ../cases.h:105:10:105:16 | %I32X | 0 | int, signed int, unsigned int | int, signed int, unsigned int | unsigned int | | +| ../cases.h:106:10:106:16 | %I32n | 0 | int, signed int, unsigned int | int, signed int, unsigned int | | | +| ../cases.h:107:10:107:16 | %I64d | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | long long | | +| ../cases.h:108:10:108:16 | %I64i | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | long long | | +| ../cases.h:109:10:109:16 | %I64u | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | unsigned long long | | +| ../cases.h:110:10:110:16 | %I64o | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | unsigned long long | | +| ../cases.h:111:10:111:16 | %I64x | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | unsigned long long | | +| ../cases.h:112:10:112:16 | %I64X | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | unsigned long long | | +| ../cases.h:113:10:113:16 | %I64n | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | | | +| ../cases.h:114:10:114:38 | %08lld %.3lu %*d %.*u %1$li | 0 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | long long | | +| ../cases.h:114:10:114:38 | %08lld %.3lu %*d %.*u %1$li | 1 | long, signed long, unsigned long | long, signed long, unsigned long | unsigned long | | +| ../cases.h:114:10:114:38 | %08lld %.3lu %*d %.*u %1$li | 2 | int, signed int, unsigned int | int, signed int, unsigned int | int | | +| ../cases.h:114:10:114:38 | %08lld %.3lu %*d %.*u %1$li | 3 | int, signed int, unsigned int | int, signed int, unsigned int | unsigned int | | +| ../cases.h:114:10:114:38 | %08lld %.3lu %*d %.*u %1$li | 4 | long, signed long, unsigned long | long, signed long, unsigned long | long | | +| ../cases.h:115:10:115:26 | %f %Lf %s %p %c | 0 | int, signed int, unsigned int | int, signed int, unsigned int | double | | +| ../cases.h:115:10:115:26 | %f %Lf %s %p %c | 1 | long long, signed long long, unsigned long long | long long, signed long long, unsigned long long | long double | | +| ../cases.h:115:10:115:26 | %f %Lf %s %p %c | 2 | int, signed int, unsigned int | int, signed int, unsigned int | char * | | +| ../cases.h:115:10:115:26 | %f %Lf %s %p %c | 3 | int, signed int, unsigned int | int, signed int, unsigned int | | | +| ../cases.h:115:10:115:26 | %f %Lf %s %p %c | 4 | int, signed int, unsigned int | int, signed int, unsigned int | signed char | | diff --git a/cpp/ql/test/library-tests/printf/integerConversionTypes/Microsoft/integerTypes.ql b/cpp/ql/test/library-tests/printf/integerConversionTypes/Microsoft/integerTypes.ql new file mode 100644 index 000000000000..22d16003f9c1 --- /dev/null +++ b/cpp/ql/test/library-tests/printf/integerConversionTypes/Microsoft/integerTypes.ql @@ -0,0 +1,8 @@ +import semmle.code.cpp.commons.Printf + +from FormatLiteral format, int n +where exists(format.getConvSpecOffset(n)) +select format, n, concat(format.getIntegralConversion(n).toString(), ", "), + concat(format.getIntegralDisplayType(n).toString(), ", "), + concat(format.getConversionType(n).toString(), ", "), + concat(format.getStorePointerConversion(n).toString(), ", ") diff --git a/cpp/ql/test/library-tests/printf/integerConversionTypes/Microsoft/options b/cpp/ql/test/library-tests/printf/integerConversionTypes/Microsoft/options new file mode 100644 index 000000000000..668c53efd7db --- /dev/null +++ b/cpp/ql/test/library-tests/printf/integerConversionTypes/Microsoft/options @@ -0,0 +1 @@ +semmle-extractor-options: --microsoft --edg --target --edg win64 diff --git a/cpp/ql/test/library-tests/printf/integerConversionTypes/Microsoft/test.c b/cpp/ql/test/library-tests/printf/integerConversionTypes/Microsoft/test.c new file mode 100644 index 000000000000..9878439493e6 --- /dev/null +++ b/cpp/ql/test/library-tests/printf/integerConversionTypes/Microsoft/test.c @@ -0,0 +1 @@ +#include "../cases.h" diff --git a/cpp/ql/test/library-tests/printf/integerConversionTypes/cases.h b/cpp/ql/test/library-tests/printf/integerConversionTypes/cases.h new file mode 100644 index 000000000000..6928e95eefd7 --- /dev/null +++ b/cpp/ql/test/library-tests/printf/integerConversionTypes/cases.h @@ -0,0 +1,116 @@ +// Compile-only coverage of the existing integer conversion model. +int printf(const char *format, ...); +#ifdef _MSC_VER +typedef unsigned long long size_t; +typedef long long ssize_t; +typedef long long ptrdiff_t; +#else +typedef unsigned long size_t; +typedef long ssize_t; +typedef long ptrdiff_t; +#endif +typedef long long intmax_t; +typedef unsigned long long uintmax_t; + +void integer_types(void) { + printf("%d", 0); + printf("%i", 0); + printf("%u", 0); + printf("%o", 0); + printf("%x", 0); + printf("%X", 0); + printf("%n", 0); + printf("%hhd", 0); + printf("%hhi", 0); + printf("%hhu", 0); + printf("%hho", 0); + printf("%hhx", 0); + printf("%hhX", 0); + printf("%hhn", 0); + printf("%hd", 0); + printf("%hi", 0); + printf("%hu", 0); + printf("%ho", 0); + printf("%hx", 0); + printf("%hX", 0); + printf("%hn", 0); + printf("%ld", 0); + printf("%li", 0); + printf("%lu", 0); + printf("%lo", 0); + printf("%lx", 0); + printf("%lX", 0); + printf("%ln", 0); + printf("%lld", 0); + printf("%lli", 0); + printf("%llu", 0); + printf("%llo", 0); + printf("%llx", 0); + printf("%llX", 0); + printf("%lln", 0); + printf("%Ld", 0); + printf("%Li", 0); + printf("%Lu", 0); + printf("%Lo", 0); + printf("%Lx", 0); + printf("%LX", 0); + printf("%Ln", 0); + printf("%qd", 0); + printf("%qi", 0); + printf("%qu", 0); + printf("%qo", 0); + printf("%qx", 0); + printf("%qX", 0); + printf("%qn", 0); + printf("%jd", 0); + printf("%ji", 0); + printf("%ju", 0); + printf("%jo", 0); + printf("%jx", 0); + printf("%jX", 0); + printf("%jn", 0); + printf("%zd", 0); + printf("%zi", 0); + printf("%zu", 0); + printf("%zo", 0); + printf("%zx", 0); + printf("%zX", 0); + printf("%zn", 0); + printf("%Zd", 0); + printf("%Zi", 0); + printf("%Zu", 0); + printf("%Zo", 0); + printf("%Zx", 0); + printf("%ZX", 0); + printf("%Zn", 0); + printf("%td", 0); + printf("%ti", 0); + printf("%tu", 0); + printf("%to", 0); + printf("%tx", 0); + printf("%tX", 0); + printf("%tn", 0); + printf("%Id", 0); + printf("%Ii", 0); + printf("%Iu", 0); + printf("%Io", 0); + printf("%Ix", 0); + printf("%IX", 0); + printf("%In", 0); + printf("%I32d", 0); + printf("%I32i", 0); + printf("%I32u", 0); + printf("%I32o", 0); + printf("%I32x", 0); + printf("%I32X", 0); + printf("%I32n", 0); + printf("%I64d", 0); + printf("%I64i", 0); + printf("%I64u", 0); + printf("%I64o", 0); + printf("%I64x", 0); + printf("%I64X", 0); + printf("%I64n", 0); + printf("%08lld %.3lu %*d %.*u %1$li", 0, 0, 0, 0, 0, 0, 0); + printf("%f %Lf %s %p %c", 0, 0, 0, 0, 0); +}