[clang] [Sema] -Wformat: warn about C23 %b %B when not in C23 language mode (PR #126694)

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 12 05:09:00 PST 2025


AaronBallman wrote:

> > /cc @SharonXSharon
> 
> thanks for submitting the fix! A noob question, I wonder in general why does clang choose to warn on the invalid formatter instead of erroring out given it'll be undefined behavior for invalid formatter?

Functionally, some kinds of UB are worse than other kinds. For example, if the size and alignment are the same between the format specifier and the data, the UB could be benign, as with: https://godbolt.org/z/dGbzx3PYW so a warning sometimes may be preferable. Other situations are like the one above where C23 adds `%b`, but the code could be compiled in C17 mode but linked against a C23-compatible standard library.

That said, it *might* make sense to make some of the diagnostics a warning which default to an error because plenty of forms of UB (especially for things like `sprintf`) can be dangerous.

https://github.com/llvm/llvm-project/pull/126694


More information about the cfe-commits mailing list