Skip to content

C++: suggest specifiers for simple integer format mismatches - #23108

Open
SkyCipherr wants to merge 5 commits into
github:mainfrom
SkyCipherr:codex/issue-15028-integer-format-hints
Open

SkyCipherr wants to merge 5 commits into
github:mainfrom
SkyCipherr:codex/issue-15028-integer-format-hints

Conversation

@SkyCipherr

@SkyCipherr SkyCipherr commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Related to #15028.

Add format specifier suggestions to existing cpp/wrong-type-format-argument alerts. For example, %#08x with an unsigned long long argument suggests %#08llx, preserving hexadecimal output, the flags, and the width.

Printf.qll uses the same conversion-type rules for the existing diagnostic and for candidate replacements. Candidates cover decimal, octal, hexadecimal, floating-point, character, and string conversions. The model checks the candidate against the argument type and target format rules, preserving flags, width, precision, and supported positional fields. The query adds a suggestion only when a conversion-value argument has a single best valid replacement.

Validation

  • All 20 final printf library and format-argument regression tests passed with CodeQL CLI 2.27.4.
  • Compared with e71839291 on the same test inputs, all 133 Linux and 110 Microsoft alerts retained their locations and original messages. The number of alerts with suggestions increased from 16 to 34 on Linux and from 16 to 66 on Microsoft.
  • Eleven corrected calls passed GCC format checking. QL formatting and git diff --check passed.

Upstream CI and broader DCA remain pending.

@SkyCipherr
SkyCipherr requested a review from a team as a code owner November 12, 2026 03:10
Copilot AI adaptive review requested due to automatic review settings November 12, 2026 03:10

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The production-query change lacks the repository’s broader DCA evaluation to check for unexpected diagnostic regressions beyond the focused tests and MinGW database.

1 open issue
What changed in this PR

Adds format-specifier suggestions to the C++ cpp/wrong-type-format-argument query, partially addressing #15028 without changing mismatch detection.

Changes:

  • Suggests matching specifiers for plain decimal integer conversions using promoted argument types.
  • Adds Linux and Microsoft target-model tests covering suggestions and excluded cases.
  • Updates existing expected diagnostics and adds a release note.
File Description
cpp/​ql/​test/​query-tests/​Likely Bugs/​Format/​WrongTypeFormatArguments/​Microsoft/​WrongTypeFormatArguments.expected Updates Microsoft diagnostic expectations.
cpp/​ql/​test/​query-tests/​Likely Bugs/​Format/​WrongTypeFormatArguments/​Microsoft_no_wchar/​WrongTypeFormatArguments.expected Updates no-wchar diagnostic expectations.
cpp/​ql/​test/​query-tests/​Likely Bugs/​Format/​WrongTypeFormatArguments/​Linux_unsigned_chars/​WrongTypeFormatArguments.expected Updates unsigned-char diagnostic expectations.
cpp/​ql/​test/​query-tests/​Likely Bugs/​Format/​WrongTypeFormatArguments/​Linux_signed_chars/​WrongTypeFormatArguments.expected Updates signed-char diagnostic expectations.
cpp/​ql/​test/​query-tests/​Likely Bugs/​Format/​WrongTypeFormatArguments/​Buildless/​WrongTypeFormatArguments.expected Updates buildless diagnostic expectations.
cpp/​ql/​test/​query-tests/​Likely Bugs/​Format/​IntegerFormatSuggestions/​Microsoft/​WrongTypeFormatArguments.qlref Connects Microsoft tests to the query.
cpp/​ql/​test/​query-tests/​Likely Bugs/​Format/​IntegerFormatSuggestions/​Microsoft/​WrongTypeFormatArguments.expected Records Microsoft suggestion expectations.
cpp/​ql/​test/​query-tests/​Likely Bugs/​Format/​IntegerFormatSuggestions/​Microsoft/​options Configures the Microsoft target model.
cpp/​ql/​test/​query-tests/​Likely Bugs/​Format/​IntegerFormatSuggestions/​Microsoft/​integer-hints.c Adds Microsoft suggestion and exclusion cases.
cpp/​ql/​test/​query-tests/​Likely Bugs/​Format/​IntegerFormatSuggestions/​Linux/​WrongTypeFormatArguments.qlref Connects Linux tests to the query.
cpp/​ql/​test/​query-tests/​Likely Bugs/​Format/​IntegerFormatSuggestions/​Linux/​WrongTypeFormatArguments.expected Records Linux suggestion expectations.
cpp/​ql/​test/​query-tests/​Likely Bugs/​Format/​IntegerFormatSuggestions/​Linux/​options Configures the Linux test extractor.
cpp/​ql/​test/​query-tests/​Likely Bugs/​Format/​IntegerFormatSuggestions/​Linux/​integer-hints.c Adds Linux suggestion and exclusion cases.
cpp/​ql/​src/​Likely Bugs/​Format/​WrongTypeFormatArguments.ql Appends suggestions for supported integer mismatches.
cpp/​ql/​src/​change-notes/​2026-11-12-integer-format-suggestions.md Documents the diagnostic enhancement.

Review effort: Balanced (auto)

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)
@jketema

jketema commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

This feels very hacky. I think a more principled approach would likely be to update cpp/ql/lib/semmle/code/cpp/commons/Printf.qll to provide this information.

@SkyCipherr

Copy link
Copy Markdown
Contributor Author

Thanks @jketema. Following your suggestion, I've reworked this in 0dc2c48.

Printf.qll now exposes FormatLiteral.getSuggestedIntegerConversionSpecifier(int n, Type type). It uses the existing parseConvSpec fields and the supplied promoted argument type to provide the replacement specifier. The query no longer has its own type-to-specifier mapping or regular-expression check; it only checks that the diagnostic concerns the conversion-value argument and formats the suggestion message. The alert detection predicates are unchanged.

The API is deliberately limited to the existing scope: plain decimal d, i, and u conversions with optional l or ll, for signed/unsigned int, long, and long long types. It preserves i for signed arguments and has no result for unsupported types, flags, width, precision, positional parameters, or other conversions. Its documentation states that callers supply the type after default argument promotions; it provides specifier information independently of whether a mismatch is reported. I've also added a library feature change note.

Validation with CodeQL CLI 2.27.4:

  • All 20 relevant tests pass: the 14 existing PR query tests, 4 existing printf library tests, and 2 new direct API tests.
  • Each new Linux/Microsoft API test checks 61 conversion positions: 27 supported and 34 excluded. These cover type categories independent of ABI widths, signedness, i preservation, typedefs/qualifiers, raw versus promoted narrow types, per-conversion mapping, decorators, positional parameters, and malformed/unsupported formats.
  • All 14 existing query result files are unchanged, including locations and complete messages.
  • Reanalysis of the existing real-GCC reproducer still produces the same 19 alerts and 10 suggestions, with identical messages and locations.
  • The 26 corrected calls from the new library fixtures and the previous 11 corrected calls pass GCC -Wformat=2 -Werror=format compilation checks. Mismatched calls were not executed.
  • QL compilation, formatting, and git diff --check pass.

These are local validation results. I have not run the full repository suite or refreshed upstream DCA for this commit; it still needs upstream CI/DCA.

@jketema

jketema commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

You misunderstood me: this will need to be properly integrated with what is in Print.qll, not just moved around.

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.
@SkyCipherr

Copy link
Copy Markdown
Contributor Author

Thanks for clarifying. I misunderstood your earlier suggestion: the previous revision still maintained a separate type-to-specifier mapping, even though it was placed in Printf.qll.

I've reworked this in 30acf5c9 so that type inference and suggestions now share the existing model:

  • Extracted the existing length-to-type-family rules into getIntegralTypeForLength, retaining target-specific selection and extension lengths. Both getIntegralConversion and getIntegralDisplayType use this relation, while preserving the distinct h/hh display types.
  • Factored the existing signed/unsigned conversion-character rules into getIntegerConversionType(length, conversion), which getConversionType now uses for integer conversions.
  • Changed suggestion generation to reverse that same relation over the supported candidate lengths and characters, matching the argument’s canonical arithmetic type. The separate reverse mapping is gone. This handles explicitly signed types while keeping equal-width int, long, and long long distinct.

The query’s detection predicates and the supported suggestion scope remain unchanged.

Validation with CodeQL CLI 2.27.4:

  • All 26 tests across the complete printf library-test and format query-test directories pass.
  • Added Linux/Microsoft forward-model regression tests, recording their outputs before modifying the library. All 108 conversion positions per model remain identical after refactoring.
  • Existing test sources and expectations are unchanged, including the suggestion API tests covering 61 positions per model.
  • Re-ran both the preceding PR model and this revision against the same real-GCC database: both produce 19 alerts and 10 suggestions, with identical complete messages and locations.
  • All 37 corrected calls pass GCC -std=c11 -Wformat=2 -Werror=format -c checks. Deliberately mismatched calls were not executed.
  • QL compilation, formatting, and git diff --check pass.

These are local results; I have not run the full repository suite or upstream DCA.

@jketema

jketema commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

We're moving in a direction where the code complexity is increasing and this is starting to become unmaintainable. Also getSuggestedIntegerConversionSpecifier still seems it separate thing, while there is already information about format specifiers in the code that was originally in Print.qll. What is the problem with exposing and using that information?

@SkyCipherr

Copy link
Copy Markdown
Contributor Author

Thanks for clarifying. I restored the original display-type implementation in Printf.qll and exposed its existing rules for determining the required argument type from an integer conversion’s length modifier and conversion character. Both getConversionType and the query use those rules. The query uses parseConvSpec to identify eligible conversions and matches candidate specifiers against the promoted argument type.

All 24 retained tests passed locally. Re-running the query on the MinGW-w64 GCC database produced the same 19 alerts and 10 suggestions, with identical messages and locations. Broader DCA is still pending.

@jketema

jketema commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

We don't seem to be making a lot of progress here. And we seem almost back in the first version of this. This seems to be partially due to the weird decision that was made to only look at a few specific specifiers.

@SkyCipherr

Copy link
Copy Markdown
Contributor Author

In reply to @jketema

I updated the PR in 23ae2bbfd so the diagnostic and replacement suggestions share the existing conversion-type model in Printf.qll.

Suggestions now cover decimal, octal, hexadecimal, floating-point, character, and string conversions. For example, %#08x with an unsigned long long argument now suggests %#08llx, preserving the hexadecimal output, flags, and width. The query adds a suggestion only when it can select a single best valid replacement.

All 20 local regression tests passed. The Linux and Microsoft test configurations still produce 133 and 110 alerts respectively, at the same locations and with the same original diagnostic text. Upstream CI and broader DCA are still pending.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants