-
Notifications
You must be signed in to change notification settings - Fork 14.6k
[WIP][-Wunsafe-buffer-usage] Add a new option for unsafe buffer warnings on libc functions #105383
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
Closed
ziqingluo-90
wants to merge
5
commits into
llvm:main
from
ziqingluo-90:dev/ziqing/warn-libc-new-option
Closed
[WIP][-Wunsafe-buffer-usage] Add a new option for unsafe buffer warnings on libc functions #105383
ziqingluo-90
wants to merge
5
commits into
llvm:main
from
ziqingluo-90:dev/ziqing/warn-libc-new-option
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
Warning about calls to libc functions involving buffer access. Warned functions are hardcoded by names. (rdar://117182250)
- Predefined name macros such as `__PRETTY_FUNCTION__` are considered safe. - We need to distinguish between `%s` and `%p` as both accept pointers but the latter is not a buffer operation. This leaves us no choice other than parsing the format string. Fortunately, the building blocks of format parsing have already existed and are quite handy.
…rays Take `snprintf(a, sizeof a, ...)` as an example, the first two arguments form a safe pattern if `a` is a constant array.
…c-call The option can be used to turn off warnings on unsafe libc function calls when needed.
@llvm/pr-subscribers-clang-analysis @llvm/pr-subscribers-clang Author: Ziqing Luo (ziqingluo-90) ChangesDepending on #101583 (rdar://117182250) Patch is 41.29 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/105383.diff 9 Files Affected:
|
You can test this locally with the following command:git-clang-format --diff c89e9e7d9e9d7c4b30e2d911f4d68ec66e6c68d8 ddceaaf34a5237744991a077c0a88dca52eddf60 --extensions h,cpp -- clang/test/SemaCXX/warn-unsafe-buffer-usage-libc-functions-inline-namespace.cpp clang/test/SemaCXX/warn-unsafe-buffer-usage-libc-functions.cpp clang/include/clang/Analysis/Analyses/UnsafeBufferUsage.h clang/lib/Analysis/UnsafeBufferUsage.cpp clang/lib/Sema/AnalysisBasedWarnings.cpp clang/test/SemaCXX/warn-unsafe-buffer-usage-test-unreachable.cpp View the diff from clang-format here.diff --git a/clang/lib/Analysis/UnsafeBufferUsage.cpp b/clang/lib/Analysis/UnsafeBufferUsage.cpp
index 02e39161e2..7475845f4b 100644
--- a/clang/lib/Analysis/UnsafeBufferUsage.cpp
+++ b/clang/lib/Analysis/UnsafeBufferUsage.cpp
@@ -473,7 +473,7 @@ namespace libc_func_matchers {
// "__builtin___" + LibcName + "_chk" |
// "__asan_" + LibcName
//
-struct LibcFunNamePrefixSuffixParser {
+struct LibcFunNamePrefixSuffixParser {
StringRef matchName(StringRef FunName, bool isBuiltin) {
// Try to match __builtin_:
if (isBuiltin && FunName.starts_with("__builtin_"))
@@ -660,7 +660,7 @@ AST_MATCHER(FunctionDecl, isPredefinedUnsafeLibcFunc) {
auto *II = Node.getIdentifier();
- if (!II)
+ if (!II)
return false;
StringRef Name = LibcFunNamePrefixSuffixParser().matchName(
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
clang:analysis
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.
Depending on #101583
(rdar://117182250)