-
Notifications
You must be signed in to change notification settings - Fork 16k
release/22.x: [Sema] Fix ICE due to incorrect _Bool handling in format string checking #179637
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zyn0217
wants to merge
1
commit into
llvm:release/22.x
Choose a base branch
from
zyn0217:backport-174684
base: release/22.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+43
−1
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…ing (llvm#174684) This cherry-picks 15365d3 to 22.x release branch, together with its follow-up 312078b which fixes the test on ARM32 targets. Co-authored-by: Yexuan Xiao <bizwen@nykz.org> Co-authored-by: Leandro Lupori <leandro.lupori@linaro.org>
Member
|
@llvm/pr-subscribers-clang Author: Younan Zhang (zyn0217) ChangesThis cherry-picks 15365d3 to 22.x release branch, together with its follow-up 312078b which fixes the test on ARM32 targets. Full diff: https://github.com/llvm/llvm-project/pull/179637.diff 2 Files Affected:
diff --git a/clang/lib/AST/FormatString.cpp b/clang/lib/AST/FormatString.cpp
index d4cb89b43ae87..36c5f57671631 100644
--- a/clang/lib/AST/FormatString.cpp
+++ b/clang/lib/AST/FormatString.cpp
@@ -371,7 +371,7 @@ static clang::analyze_format_string::ArgType::MatchKind
matchesSizeTPtrdiffT(ASTContext &C, QualType T, QualType E) {
using MatchKind = clang::analyze_format_string::ArgType::MatchKind;
- if (!T->isIntegerType())
+ if (!T->isIntegerType() || T->isBooleanType())
return MatchKind::NoMatch;
if (C.hasSameType(T, E))
diff --git a/clang/test/Sema/format-strings.c b/clang/test/Sema/format-strings.c
index 5280549adc3d7..3a2c2701cfcfc 100644
--- a/clang/test/Sema/format-strings.c
+++ b/clang/test/Sema/format-strings.c
@@ -3,10 +3,12 @@
// RUN: %clang_cc1 -fblocks -fsyntax-only -verify -Wformat-nonliteral -isystem %S/Inputs -triple=x86_64-unknown-fuchsia %s
// RUN: %clang_cc1 -fblocks -fsyntax-only -verify -Wformat-nonliteral -isystem %S/Inputs -triple=x86_64-linux-android %s
+#include <limits.h>
#include <stdarg.h>
#include <stddef.h>
#define __need_wint_t
#include <stddef.h> // For wint_t and wchar_t
+#include <stdint.h>
typedef struct _FILE FILE;
int fprintf(FILE *, const char *restrict, ...);
@@ -983,3 +985,43 @@ void test_promotion(void) {
// pointers
printf("%s", i); // expected-warning{{format specifies type 'char *' but the argument has type 'int'}}
}
+
+void test_bool(_Bool b, _Bool* bp)
+{
+#if SIZE_MAX != UINT_MAX
+ printf("%zu", b); // expected-warning-re{{format specifies type 'size_t' (aka '{{.+}}') but the argument has type '_Bool'}}
+#else
+ printf("%zu", b); // no-warning
+#endif
+#if PTRDIFF_MAX != INT_MAX
+ printf("%td", b); // expected-warning-re{{format specifies type 'ptrdiff_t' (aka '{{.+}}') but the argument has type '_Bool'}}
+#else
+ printf("%td", b); // no-warning
+#endif
+ printf("%jd", b); // expected-warning-re{{format specifies type 'intmax_t' (aka '{{.+}}') but the argument has type '_Bool'}}
+ printf("%lld", b); // expected-warning{{format specifies type 'long long' but the argument has type '_Bool'}}
+ printf("%ld", b); // expected-warning{{format specifies type 'long' but the argument has type '_Bool'}}
+ printf("%d", b); // promoted from _Bool to int
+ printf("%hhd", b); // promoted from _Bool to int
+ printf("%hd", b); // promoted from _Bool to int
+#if !defined(__Fuchsia__) && !defined(__ANDROID__) //'%n' specifier not supported on this platform
+ // The n conversion specifier only supports signed types
+ printf("%zn", bp); // expected-warning-re{{format specifies type 'signed size_t *' (aka '{{.+}}') but the argument has type '_Bool *'}}
+ printf("%jn", bp); // expected-warning-re{{format specifies type 'intmax_t *' (aka '{{.+}}') but the argument has type '_Bool *'}}
+ printf("%lln", bp); // expected-warning{{format specifies type 'long long *' but the argument has type '_Bool *'}}
+ printf("%ln", bp); // expected-warning{{format specifies type 'long *' but the argument has type '_Bool *'}}
+ printf("%n", bp); // expected-warning{{format specifies type 'int *' but the argument has type '_Bool *'}}
+ printf("%hhn", bp); // expected-warning{{format specifies type 'signed char *' but the argument has type '_Bool *'}}
+ printf("%hn", bp); // belong to -Wformat-type-confusion
+#endif
+ printf("%c", b); // expected-warning{{using '%c' format specifier, but argument has boolean value}}
+ printf("%s", b); // expected-warning{{format specifies type 'char *' but the argument has type '_Bool'}}
+ printf("%d", b); // promoted from _Bool to int
+ printf("%o", b); // promoted from _Bool to int
+ printf("%x", b); // promoted from _Bool to int
+ printf("%u", b); // promoted from _Bool to int
+ printf("%f", b); // expected-warning{{format specifies type 'double' but the argument has type '_Bool'}}
+ printf("%e", b); // expected-warning{{format specifies type 'double' but the argument has type '_Bool'}}
+ printf("%a", b); // expected-warning{{format specifies type 'double' but the argument has type '_Bool'}}
+ printf("%g", b); // expected-warning{{format specifies type 'double' but the argument has type '_Bool'}}
+}
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
clang:frontend
Language frontend issues, e.g. anything involving "Sema"
clang
Clang issues not falling into any other category
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This cherry-picks 15365d3 to 22.x release branch, together with its follow-up 312078b which fixes the test on ARM32 targets.