Skip to content

Commit e4e56f9

Browse files
committed
[clang] Emit Wformat for bool value and char specifier confusion in scanf
Fixes #64987 Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D159279
1 parent 55dc73a commit e4e56f9

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

clang/docs/ReleaseNotes.rst

+3
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ Bug Fixes in This Version
205205
- For function multi-versioning using the ``target`` or ``target_clones``
206206
attributes, remove comdat for internal linkage functions.
207207
(`#65114 <https://github.com/llvm/llvm-project/issues/65114>`_)
208+
- Clang now reports ``-Wformat`` for bool value and char specifier confusion
209+
in scanf. Fixes
210+
(`#64987 <https://github.com/llvm/llvm-project/issues/64987>`_)
208211

209212
Bug Fixes to Compiler Builtins
210213
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/AST/FormatString.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,11 @@ ArgType::matchesType(ASTContext &C, QualType argTy) const {
368368
case BuiltinType::SChar:
369369
case BuiltinType::UChar:
370370
case BuiltinType::Char_U:
371+
return Match;
371372
case BuiltinType::Bool:
372-
return Match;
373+
if (!Ptr)
374+
return Match;
375+
break;
373376
}
374377
// "Partially matched" because of promotions?
375378
if (!Ptr) {
@@ -410,11 +413,14 @@ ArgType::matchesType(ASTContext &C, QualType argTy) const {
410413
switch (BT->getKind()) {
411414
default:
412415
break;
416+
case BuiltinType::Bool:
417+
if (Ptr && (T == C.UnsignedCharTy || T == C.SignedCharTy))
418+
return NoMatch;
419+
[[fallthrough]];
413420
case BuiltinType::Char_S:
414421
case BuiltinType::SChar:
415422
case BuiltinType::Char_U:
416423
case BuiltinType::UChar:
417-
case BuiltinType::Bool:
418424
if (T == C.UnsignedShortTy || T == C.ShortTy)
419425
return NoMatchTypeConfusion;
420426
if (T == C.UnsignedCharTy || T == C.SignedCharTy)

clang/test/SemaCXX/format-strings-scanf.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ union bag {
2929

3030
void test(void) {
3131
bag b;
32+
// expected-warning@+2 {{format specifies type 'char *' but the argument has type 'bool *'}}
33+
// expected-warning@+1 {{format specifies type 'unsigned char *' but the argument has type 'bool *'}}
3234
scan("%hhi %hhu %hhi %hhu", &b.sc, &b.uc, &b.b, &b.b);
3335
scan("%hi %hu", &b.ss, &b.us);
3436
scan("%i %u", &b.si, &b.ui);

0 commit comments

Comments
 (0)