[clang] [clang-tools-extra] [clang][analyzer] Add ReportInC99AndEarlier option to DeprecatedOrUnsafeBuf… (PR #168704)
steakhal wrote: The failure looks relevant. Thanks for the quick revert. https://github.com/llvm/llvm-project/pull/168704 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang][analyzer] Add ReportInC99AndEarlier option to DeprecatedOrUnsafeBuf… (PR #168704)
Endre =?utf-8?q?Fülöp?= , Endre =?utf-8?q?Fülöp?= , Endre =?utf-8?q?Fülöp?= , Endre =?utf-8?q?Fülöp?= , Endre =?utf-8?q?Fülöp?= , Endre =?utf-8?q?Fülöp?= , Endre =?utf-8?q?Fülöp?= , Endre =?utf-8?q?Fülöp?= , Endre =?utf-8?q?Fülöp?= , Endre =?utf-8?q?Fülöp?= , Endre =?utf-8?q?Fülöp?= , Endre =?utf-8?q?Fülöp?= , Endre =?utf-8?q?Fülöp?= , Endre =?utf-8?q?Fülöp?= , Endre =?utf-8?q?Fülöp?= , Endre =?utf-8?q?Fülöp?= , Endre =?utf-8?q?Fülöp?= , Endre =?utf-8?q?Fülöp?= , Endre =?utf-8?q?Fülöp?= Message-ID: In-Reply-To: llvm-ci wrote: LLVM Buildbot has detected a new failure on builder `bolt-aarch64-ubuntu-clang` running on `bolt-worker-aarch64` while building `clang-tools-extra,clang` at step 5 "build-clang-bolt". Full details are available at: https://lab.llvm.org/buildbot/#/builders/128/builds/10183 Here is the relevant piece of the build log for the reference ``` Step 5 (build-clang-bolt) failure: build (failure) ... 29.648 [7/3/3374] Linking CXX static library lib/libclangDriver.a 29.741 [6/3/3375] Linking CXX static library lib/libclangCrossTU.a 30.375 [5/3/3376] Linking CXX static library lib/libclangStaticAnalyzerCore.a 30.667 [4/3/3377] Linking CXX static library lib/libclangCodeGen.a 30.993 [4/2/3378] Building CXX object tools/clang/lib/StaticAnalyzer/Frontend/CMakeFiles/obj.clangStaticAnalyzerFrontend.dir/CheckerRegistry.cpp.o 31.268 [4/1/3379] Linking CXX static library lib/libclangStaticAnalyzerCheckers.a 31.353 [3/1/3380] Linking CXX static library lib/libclangStaticAnalyzerFrontend.a 31.424 [2/1/3381] Linking CXX static library lib/libclangFrontendTool.a 32.293 [1/1/3382] Linking CXX executable bin/clang-23 427.770 [0/1/3383] Creating executable symlink bin/clang FAILED: bin/clang /usr/bin/cmake -E cmake_symlink_executable bin/clang-23 bin/clang && cd /home/buildbot/workspace/bolt-aarch64-ubuntu-clang/build/tools/clang/tools/driver && /usr/bin/cmake -E create_symlink clang-23 /home/buildbot/workspace/bolt-aarch64-ubuntu-clang/build/./bin/clang++ && cd /home/buildbot/workspace/bolt-aarch64-ubuntu-clang/build/tools/clang/tools/driver && /usr/bin/cmake -E create_symlink clang-23 /home/buildbot/workspace/bolt-aarch64-ubuntu-clang/build/./bin/clang-cl && cd /home/buildbot/workspace/bolt-aarch64-ubuntu-clang/build/tools/clang/tools/driver && /usr/bin/cmake -E create_symlink clang-23 /home/buildbot/workspace/bolt-aarch64-ubuntu-clang/build/./bin/clang-cpp && cd /home/buildbot/workspace/bolt-aarch64-ubuntu-clang/build/tools/clang/tools/driver && /usr/bin/python3 /home/buildbot/workspace/bolt-aarch64-ubuntu-clang/llvm-project/clang/tools/driver/../../utils/perf-training/perf-helper.py bolt-optimize --method INSTRUMENT --input /home/buildbot/workspace/bolt-aarch64-ubuntu-clang/build/bin/clang-23 --instrumented-output /home/buildbot/workspace/bolt-aarch64-ubuntu-clang/build/./bin/clang-bolt.inst --fdata /home/buildbot/workspace/bolt-aarch64-ubuntu-clang/build/tools/clang/tools/driver/../../utils/perf-training/prof.fdata --perf-training-binary-dir /home/buildbot/workspace/bolt-aarch64-ubuntu-clang/build/tools/clang/tools/driver/../../utils/perf-training --readelf /home/buildbot/workspace/bolt-aarch64-ubuntu-clang/build/bin/llvm-readobj --bolt /home/buildbot/workspace/bolt-aarch64-ubuntu-clang/build/bin/llvm-bolt --lit /home/buildbot/workspace/bolt-aarch64-ubuntu-clang/build/./bin/llvm-lit --merge-fdata /home/buildbot/workspace/bolt-aarch64-ubuntu-clang/build/bin/merge-fdata Running: /home/buildbot/workspace/bolt-aarch64-ubuntu-clang/build/bin/llvm-bolt /home/buildbot/workspace/bolt-aarch64-ubuntu-clang/build/bin/clang-23 -o /home/buildbot/workspace/bolt-aarch64-ubuntu-clang/build/./bin/clang-bolt.inst -instrument --instrumentation-file-append-pid --instrumentation-file=/home/buildbot/workspace/bolt-aarch64-ubuntu-clang/build/tools/clang/tools/driver/../../utils/perf-training/prof.fdata BOLT-INFO: shared object or position-independent executable detected BOLT-INFO: Target architecture: aarch64 BOLT-INFO: BOLT version: BOLT-INFO: first alloc address is 0x0 BOLT-INFO: creating new program header table at address 0x8e0, offset 0x8e0 BOLT-INFO: enabling relocation mode BOLT-INFO: forcing -jump-tables=move for instrumentation BOLT-INFO: PointerAuthCFIAnalyzer ran on 2 functions. Ignored 0 functions (0.00%) because of CFI inconsistencies BOLT-INFO: number of removed linker-inserted veneers: 0 BOLT-INFO: 0 out of 158036 functions in the binary (0.0%) have non-empty execution profile BOLT-INSTRUMENTER: Number of indirect call site descriptors: 62143 BOLT-INSTRUMENTER: Number of indirect call target descriptors: 155800 BOLT-INSTRUMENTER: Number of function descriptors: 155778 BOLT-INSTRUMENTER: Number of branch counters: 2049297 BOLT-INSTRUMENTER: Number of ST leaf node counters: 1043131 BOLT-INSTRUMENTER: Number of direct call counters: 0 BOLT-INSTRUMENTER: Total number of counters: 3092428 BOLT-INSTRUMENTER: Total size of counters: 24739424 bytes (static alloc me
[clang] [clang-tools-extra] [clang][analyzer] Add ReportInC99AndEarlier option to DeprecatedOrUnsafeBuf… (PR #168704)
https://github.com/gamesh411 closed https://github.com/llvm/llvm-project/pull/168704 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang][analyzer] Add ReportInC99AndEarlier option to DeprecatedOrUnsafeBuf… (PR #168704)
https://github.com/gamesh411 updated
https://github.com/llvm/llvm-project/pull/168704
From dae8506bc6eafa1fcc10e03e3bf7678c17a0babe Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Endre=20F=C3=BCl=C3=B6p?=
Date: Wed, 19 Nov 2025 10:47:40 +0100
Subject: [PATCH 01/20] [clang][analyzer] Add ReportInC99AndEarlier option to
DeprecatedOrUnsafeBufferHandling checker
The checker may report warnings for deprecated buffer handling functions
(memcpy, memset, memmove, etc.) even when not compiling with C11 standard
if the new option "ReportInC99AndEarlier" is set to true.
These functions became deprecated in C11, but may still be problematic in
earlier C standards.
---
clang/docs/analyzer/checkers.rst | 7 +++
.../clang/StaticAnalyzer/Checkers/Checkers.td | 21 +---
.../Checkers/CheckSecuritySyntaxOnly.cpp | 22 -
.../Analysis/Inputs/system-header-simulator.h | 1 +
clang/test/Analysis/analyzer-config.c | 1 +
...ecated-buffer-handling-allow-without-c11.c | 48 +++
6 files changed, 92 insertions(+), 8 deletions(-)
create mode 100644
clang/test/Analysis/security-deprecated-buffer-handling-allow-without-c11.c
diff --git a/clang/docs/analyzer/checkers.rst b/clang/docs/analyzer/checkers.rst
index 31edf9e99dc7d..d93cfe5806ebb 100644
--- a/clang/docs/analyzer/checkers.rst
+++ b/clang/docs/analyzer/checkers.rst
@@ -1785,6 +1785,13 @@ security.insecureAPI.DeprecatedOrUnsafeBufferHandling (C)
strncpy(buf, "a", 1); // warn
}
+The ``AllowWithoutC11`` option allows reporting warnings for these functions
even when not compiling with C11 standard. These functions are deprecated in
C11, but may still be problematic in earlier C standards.
+
+To enable this option, use:
+``-analyzer-config
security.insecureAPI.DeprecatedOrUnsafeBufferHandling:AllowWithoutC11=true``.
+
+By default, this option is set to *false*.
+
.. _security-MmapWriteExec:
security.MmapWriteExec (C)
diff --git a/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
b/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
index ffae3b9310979..310dac5340a18 100644
--- a/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
+++ b/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
@@ -901,12 +901,21 @@ def UncheckedReturn : Checker<"UncheckedReturn">,
Dependencies<[SecuritySyntaxChecker]>,
Documentation;
-def DeprecatedOrUnsafeBufferHandling :
- Checker<"DeprecatedOrUnsafeBufferHandling">,
- HelpText<"Warn on uses of unsecure or deprecated buffer manipulating "
- "functions">,
- Dependencies<[SecuritySyntaxChecker]>,
- Documentation;
+def DeprecatedOrUnsafeBufferHandling
+: Checker<"DeprecatedOrUnsafeBufferHandling">,
+ HelpText<"Warn on uses of unsecure or deprecated buffer manipulating "
+ "functions">,
+ Dependencies<[SecuritySyntaxChecker]>,
+ CheckerOptions<
+ [CmdLineOption<
+ Boolean, "AllowWithoutC11",
+ "Allow reporting deprecated or unsafe buffer handling "
+ "functions even when not compiling with C11 standard. "
+ "These functions are deprecated in C11, but may still be "
+ "problematic in earlier C standards.",
+ "false", Released>,
+]>,
+ Documentation;
def decodeValueOfObjCType : Checker<"decodeValueOfObjCType">,
HelpText<"Warn on uses of the '-decodeValueOfObjCType:at:' method">,
diff --git a/clang/lib/StaticAnalyzer/Checkers/CheckSecuritySyntaxOnly.cpp
b/clang/lib/StaticAnalyzer/Checkers/CheckSecuritySyntaxOnly.cpp
index 5e75c1c4a3abd..e07c9dcbad9fe 100644
--- a/clang/lib/StaticAnalyzer/Checkers/CheckSecuritySyntaxOnly.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/CheckSecuritySyntaxOnly.cpp
@@ -50,6 +50,8 @@ struct ChecksFilter {
bool check_UncheckedReturn = false;
bool check_decodeValueOfObjCType = false;
+ bool allowDeprecatedOrUnsafeBufferHandlingWithoutC11 = false;
+
CheckerNameRef checkName_bcmp;
CheckerNameRef checkName_bcopy;
CheckerNameRef checkName_bzero;
@@ -754,7 +756,8 @@ void WalkAST::checkDeprecatedOrUnsafeBufferHandling(const
CallExpr *CE,
if (!filter.check_DeprecatedOrUnsafeBufferHandling)
return;
- if (!BR.getContext().getLangOpts().C11)
+ if (!(BR.getContext().getLangOpts().C11 ||
+filter.allowDeprecatedOrUnsafeBufferHandlingWithoutC11))
return;
// Issue a warning. ArgIndex == -1: Deprecated but not unsafe (has size
@@ -1113,5 +1116,20 @@ REGISTER_CHECKER(rand)
REGISTER_CHECKER(vfork)
REGISTER_CHECKER(FloatLoopCounter)
REGISTER_CHECKER(UncheckedReturn)
-REGISTER_CHECKER(DeprecatedOrUnsafeBufferHandling)
+
+void ento::registerDeprecatedOrUnsafeBufferHandling(CheckerManager &mgr) {
+ SecuritySyntaxChecker *checker = mgr.getChecker();
+ checker->filter.check_DeprecatedOrUnsafeBufferHandling = true;
+ checker->filter.checkName_DeprecatedOrUnsafeBufferHandling =
+ mgr.getCurrentCheckerName();
+ checker->filter.allowDeprecatedOrUnsafeBuffer
[clang] [clang-tools-extra] [clang][analyzer] Add ReportInC99AndEarlier option to DeprecatedOrUnsafeBuf… (PR #168704)
https://github.com/gamesh411 edited https://github.com/llvm/llvm-project/pull/168704 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang][analyzer] Add ReportInC99AndEarlier option to DeprecatedOrUnsafeBuf… (PR #168704)
https://github.com/gamesh411 updated
https://github.com/llvm/llvm-project/pull/168704
From dae8506bc6eafa1fcc10e03e3bf7678c17a0babe Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Endre=20F=C3=BCl=C3=B6p?=
Date: Wed, 19 Nov 2025 10:47:40 +0100
Subject: [PATCH 01/18] [clang][analyzer] Add ReportInC99AndEarlier option to
DeprecatedOrUnsafeBufferHandling checker
The checker may report warnings for deprecated buffer handling functions
(memcpy, memset, memmove, etc.) even when not compiling with C11 standard
if the new option "ReportInC99AndEarlier" is set to true.
These functions became deprecated in C11, but may still be problematic in
earlier C standards.
---
clang/docs/analyzer/checkers.rst | 7 +++
.../clang/StaticAnalyzer/Checkers/Checkers.td | 21 +---
.../Checkers/CheckSecuritySyntaxOnly.cpp | 22 -
.../Analysis/Inputs/system-header-simulator.h | 1 +
clang/test/Analysis/analyzer-config.c | 1 +
...ecated-buffer-handling-allow-without-c11.c | 48 +++
6 files changed, 92 insertions(+), 8 deletions(-)
create mode 100644
clang/test/Analysis/security-deprecated-buffer-handling-allow-without-c11.c
diff --git a/clang/docs/analyzer/checkers.rst b/clang/docs/analyzer/checkers.rst
index 31edf9e99dc7d..d93cfe5806ebb 100644
--- a/clang/docs/analyzer/checkers.rst
+++ b/clang/docs/analyzer/checkers.rst
@@ -1785,6 +1785,13 @@ security.insecureAPI.DeprecatedOrUnsafeBufferHandling (C)
strncpy(buf, "a", 1); // warn
}
+The ``AllowWithoutC11`` option allows reporting warnings for these functions
even when not compiling with C11 standard. These functions are deprecated in
C11, but may still be problematic in earlier C standards.
+
+To enable this option, use:
+``-analyzer-config
security.insecureAPI.DeprecatedOrUnsafeBufferHandling:AllowWithoutC11=true``.
+
+By default, this option is set to *false*.
+
.. _security-MmapWriteExec:
security.MmapWriteExec (C)
diff --git a/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
b/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
index ffae3b9310979..310dac5340a18 100644
--- a/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
+++ b/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
@@ -901,12 +901,21 @@ def UncheckedReturn : Checker<"UncheckedReturn">,
Dependencies<[SecuritySyntaxChecker]>,
Documentation;
-def DeprecatedOrUnsafeBufferHandling :
- Checker<"DeprecatedOrUnsafeBufferHandling">,
- HelpText<"Warn on uses of unsecure or deprecated buffer manipulating "
- "functions">,
- Dependencies<[SecuritySyntaxChecker]>,
- Documentation;
+def DeprecatedOrUnsafeBufferHandling
+: Checker<"DeprecatedOrUnsafeBufferHandling">,
+ HelpText<"Warn on uses of unsecure or deprecated buffer manipulating "
+ "functions">,
+ Dependencies<[SecuritySyntaxChecker]>,
+ CheckerOptions<
+ [CmdLineOption<
+ Boolean, "AllowWithoutC11",
+ "Allow reporting deprecated or unsafe buffer handling "
+ "functions even when not compiling with C11 standard. "
+ "These functions are deprecated in C11, but may still be "
+ "problematic in earlier C standards.",
+ "false", Released>,
+]>,
+ Documentation;
def decodeValueOfObjCType : Checker<"decodeValueOfObjCType">,
HelpText<"Warn on uses of the '-decodeValueOfObjCType:at:' method">,
diff --git a/clang/lib/StaticAnalyzer/Checkers/CheckSecuritySyntaxOnly.cpp
b/clang/lib/StaticAnalyzer/Checkers/CheckSecuritySyntaxOnly.cpp
index 5e75c1c4a3abd..e07c9dcbad9fe 100644
--- a/clang/lib/StaticAnalyzer/Checkers/CheckSecuritySyntaxOnly.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/CheckSecuritySyntaxOnly.cpp
@@ -50,6 +50,8 @@ struct ChecksFilter {
bool check_UncheckedReturn = false;
bool check_decodeValueOfObjCType = false;
+ bool allowDeprecatedOrUnsafeBufferHandlingWithoutC11 = false;
+
CheckerNameRef checkName_bcmp;
CheckerNameRef checkName_bcopy;
CheckerNameRef checkName_bzero;
@@ -754,7 +756,8 @@ void WalkAST::checkDeprecatedOrUnsafeBufferHandling(const
CallExpr *CE,
if (!filter.check_DeprecatedOrUnsafeBufferHandling)
return;
- if (!BR.getContext().getLangOpts().C11)
+ if (!(BR.getContext().getLangOpts().C11 ||
+filter.allowDeprecatedOrUnsafeBufferHandlingWithoutC11))
return;
// Issue a warning. ArgIndex == -1: Deprecated but not unsafe (has size
@@ -1113,5 +1116,20 @@ REGISTER_CHECKER(rand)
REGISTER_CHECKER(vfork)
REGISTER_CHECKER(FloatLoopCounter)
REGISTER_CHECKER(UncheckedReturn)
-REGISTER_CHECKER(DeprecatedOrUnsafeBufferHandling)
+
+void ento::registerDeprecatedOrUnsafeBufferHandling(CheckerManager &mgr) {
+ SecuritySyntaxChecker *checker = mgr.getChecker();
+ checker->filter.check_DeprecatedOrUnsafeBufferHandling = true;
+ checker->filter.checkName_DeprecatedOrUnsafeBufferHandling =
+ mgr.getCurrentCheckerName();
+ checker->filter.allowDeprecatedOrUnsafeBuffer
[clang] [clang-tools-extra] [clang][analyzer] Add ReportInC99AndEarlier option to DeprecatedOrUnsafeBuf… (PR #168704)
@@ -1113,5 +1141,32 @@ REGISTER_CHECKER(rand)
REGISTER_CHECKER(vfork)
REGISTER_CHECKER(FloatLoopCounter)
REGISTER_CHECKER(UncheckedReturn)
-REGISTER_CHECKER(DeprecatedOrUnsafeBufferHandling)
+
+void ento::registerDeprecatedOrUnsafeBufferHandling(CheckerManager &Mgr) {
+ SecuritySyntaxChecker *Checker = Mgr.getChecker();
+ Checker->filter.check_DeprecatedOrUnsafeBufferHandling = true;
+ Checker->filter.checkName_DeprecatedOrUnsafeBufferHandling =
+ Mgr.getCurrentCheckerName();
+
+ // Parse ReportMode option (defaults to C11Only for backward compatibility)
+ StringRef ReportModeStr = Mgr.getAnalyzerOptions().getCheckerStringOption(
+ Mgr.getCurrentCheckerName(), "ReportMode");
+ if (ReportModeStr == "all")
+Checker->filter.ReportMode = ReportPolicy::All;
+ else if (ReportModeStr == "actionable")
+Checker->filter.ReportMode = ReportPolicy::Actionable;
+ else if (ReportModeStr == "c11-only")
+Checker->filter.ReportMode = ReportPolicy::C11Only;
gamesh411 wrote:
I have introduced the StringSwitch and also wrapped the parsing in an optional
because of the limitations of the StringSwitch API.
https://github.com/llvm/llvm-project/pull/168704
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang][analyzer] Add ReportInC99AndEarlier option to DeprecatedOrUnsafeBuf… (PR #168704)
@@ -0,0 +1,120 @@
+// DEFINE: %{base_cmd} = %clang_analyze_cc1 %s \
+// DEFINE:
-analyzer-checker=security.insecureAPI.DeprecatedOrUnsafeBufferHandling
+
+// DEFINE: %{verify_flag} =
+
+// DEFINE: %{config_flag_unset} =
+// DEFINE: %{config_flag_all} = -analyzer-config
security.insecureAPI.DeprecatedOrUnsafeBufferHandling:ReportMode=all
+// DEFINE: %{config_flag_actionable} = -analyzer-config
security.insecureAPI.DeprecatedOrUnsafeBufferHandling:ReportMode=actionable
+// DEFINE: %{config_flag_c11_only} = -analyzer-config
security.insecureAPI.DeprecatedOrUnsafeBufferHandling:ReportMode=c11-only
+// DEFINE: %{config_flag} = %{config_flag_unset}
+
+// DEFINE: %{std_flag_c99} = -std=gnu99
+// DEFINE: %{std_flag_c11} = -std=gnu11
+// DEFINE: %{std_flag} = %{std_flag_c99}
+
+// DEFINE: %{annexk_defines_unset} =
+// DEFINE: %{annexk_defines_set} = -D__STDC_LIB_EXT1__=200509L
-D__STDC_WANT_LIB_EXT1__=1
+// DEFINE: %{annexk_flag} = %{annexk_defines_unset}
+
+// DEFINE: %{run_cmd} = %{base_cmd} %{verify_flag} %{std_flag} %{annexk_flag}
%{config_flag}
+
+// These cases should warn
+
+// REDEFINE: %{verify_flag} = -verify=common
+// REDEFINE: %{std_flag} = %{std_flag_c99}
+// REDEFINE: %{annexk_flag} = %{annexk_defines_unset}
+// REDEFINE: %{config_flag} = %{config_flag_all}
+// RUN: %{run_cmd}
+
+// C99 with "all" mode
+// REDEFINE: %{verify_flag} = -verify=common
+// REDEFINE: %{std_flag} = %{std_flag_c99}
+// REDEFINE: %{annexk_flag} = %{annexk_defines_unset}
+// REDEFINE: %{config_flag} = %{config_flag_all}
+// RUN: %{run_cmd}
+
+// C11 with default mode
+// REDEFINE: %{verify_flag} = -verify=common
+// REDEFINE: %{std_flag} = %{std_flag_c11}
+// REDEFINE: %{annexk_flag} = %{annexk_defines_unset}
+// REDEFINE: %{config_flag} = %{config_flag_unset}
+// RUN: %{run_cmd}
+
+// C11 with "all" mode
+// REDEFINE: %{verify_flag} = -verify=common
+// REDEFINE: %{std_flag} = %{std_flag_c11}
+// REDEFINE: %{annexk_flag} = %{annexk_defines_unset}
+// REDEFINE: %{config_flag} = %{config_flag_all}
+// RUN: %{run_cmd}
+
+// C11 with "c11-only" mode
+// REDEFINE: %{verify_flag} = -verify=common
+// REDEFINE: %{std_flag} = %{std_flag_c11}
+// REDEFINE: %{annexk_flag} = %{annexk_defines_unset}
+// REDEFINE: %{config_flag} = %{config_flag_c11_only}
+// RUN: %{run_cmd}
+
+// C11 with "actionable" mode and Annex K available
+// REDEFINE: %{verify_flag} = -verify=common
+// REDEFINE: %{std_flag} = %{std_flag_c11}
+// REDEFINE: %{annexk_flag} = %{annexk_defines_set}
+// REDEFINE: %{config_flag} = %{config_flag_actionable}
+// RUN: %{run_cmd}
+
+// These cases should not warn
+
+// C99 with default mode
+// REDEFINE: %{verify_flag} = -verify=c99-default
+// REDEFINE: %{std_flag} = %{std_flag_c99}
+// REDEFINE: %{annexk_flag} = %{annexk_defines_unset}
+// REDEFINE: %{config_flag} = %{config_flag_unset}
+// RUN: %{run_cmd}
+
+// C99 with "actionable" mode and no Annex K
+// REDEFINE: %{verify_flag} = -verify=c99-actionable
+// REDEFINE: %{std_flag} = %{std_flag_c99}
+// REDEFINE: %{annexk_flag} = %{annexk_defines_unset}
+// REDEFINE: %{config_flag} = %{config_flag_actionable}
+// RUN: %{run_cmd}
+
+// C99 with "c11-only" mode
+// REDEFINE: %{verify_flag} = -verify=c99-c11only
+// REDEFINE: %{std_flag} = %{std_flag_c99}
+// REDEFINE: %{annexk_flag} = %{annexk_defines_unset}
+// REDEFINE: %{config_flag} = %{config_flag_c11_only}
+// RUN: %{run_cmd}
+
+// C11 with "actionable" mode and no Annex K
+// REDEFINE: %{verify_flag} = -verify=c11-actionable-noannex
+// REDEFINE: %{std_flag} = %{std_flag_c11}
+// REDEFINE: %{annexk_flag} = %{annexk_defines_unset}
+// REDEFINE: %{config_flag} = %{config_flag_actionable}
+// RUN: %{run_cmd}
gamesh411 wrote:
I think this now is closer to what you meant. I went overboard with the
features of lit, this is much more readable now I agree. Thanks for the
feedback!
https://github.com/llvm/llvm-project/pull/168704
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang][analyzer] Add ReportInC99AndEarlier option to DeprecatedOrUnsafeBuf… (PR #168704)
https://github.com/gamesh411 updated
https://github.com/llvm/llvm-project/pull/168704
From dae8506bc6eafa1fcc10e03e3bf7678c17a0babe Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Endre=20F=C3=BCl=C3=B6p?=
Date: Wed, 19 Nov 2025 10:47:40 +0100
Subject: [PATCH 01/18] [clang][analyzer] Add ReportInC99AndEarlier option to
DeprecatedOrUnsafeBufferHandling checker
The checker may report warnings for deprecated buffer handling functions
(memcpy, memset, memmove, etc.) even when not compiling with C11 standard
if the new option "ReportInC99AndEarlier" is set to true.
These functions became deprecated in C11, but may still be problematic in
earlier C standards.
---
clang/docs/analyzer/checkers.rst | 7 +++
.../clang/StaticAnalyzer/Checkers/Checkers.td | 21 +---
.../Checkers/CheckSecuritySyntaxOnly.cpp | 22 -
.../Analysis/Inputs/system-header-simulator.h | 1 +
clang/test/Analysis/analyzer-config.c | 1 +
...ecated-buffer-handling-allow-without-c11.c | 48 +++
6 files changed, 92 insertions(+), 8 deletions(-)
create mode 100644
clang/test/Analysis/security-deprecated-buffer-handling-allow-without-c11.c
diff --git a/clang/docs/analyzer/checkers.rst b/clang/docs/analyzer/checkers.rst
index 31edf9e99dc7d..d93cfe5806ebb 100644
--- a/clang/docs/analyzer/checkers.rst
+++ b/clang/docs/analyzer/checkers.rst
@@ -1785,6 +1785,13 @@ security.insecureAPI.DeprecatedOrUnsafeBufferHandling (C)
strncpy(buf, "a", 1); // warn
}
+The ``AllowWithoutC11`` option allows reporting warnings for these functions
even when not compiling with C11 standard. These functions are deprecated in
C11, but may still be problematic in earlier C standards.
+
+To enable this option, use:
+``-analyzer-config
security.insecureAPI.DeprecatedOrUnsafeBufferHandling:AllowWithoutC11=true``.
+
+By default, this option is set to *false*.
+
.. _security-MmapWriteExec:
security.MmapWriteExec (C)
diff --git a/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
b/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
index ffae3b9310979..310dac5340a18 100644
--- a/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
+++ b/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
@@ -901,12 +901,21 @@ def UncheckedReturn : Checker<"UncheckedReturn">,
Dependencies<[SecuritySyntaxChecker]>,
Documentation;
-def DeprecatedOrUnsafeBufferHandling :
- Checker<"DeprecatedOrUnsafeBufferHandling">,
- HelpText<"Warn on uses of unsecure or deprecated buffer manipulating "
- "functions">,
- Dependencies<[SecuritySyntaxChecker]>,
- Documentation;
+def DeprecatedOrUnsafeBufferHandling
+: Checker<"DeprecatedOrUnsafeBufferHandling">,
+ HelpText<"Warn on uses of unsecure or deprecated buffer manipulating "
+ "functions">,
+ Dependencies<[SecuritySyntaxChecker]>,
+ CheckerOptions<
+ [CmdLineOption<
+ Boolean, "AllowWithoutC11",
+ "Allow reporting deprecated or unsafe buffer handling "
+ "functions even when not compiling with C11 standard. "
+ "These functions are deprecated in C11, but may still be "
+ "problematic in earlier C standards.",
+ "false", Released>,
+]>,
+ Documentation;
def decodeValueOfObjCType : Checker<"decodeValueOfObjCType">,
HelpText<"Warn on uses of the '-decodeValueOfObjCType:at:' method">,
diff --git a/clang/lib/StaticAnalyzer/Checkers/CheckSecuritySyntaxOnly.cpp
b/clang/lib/StaticAnalyzer/Checkers/CheckSecuritySyntaxOnly.cpp
index 5e75c1c4a3abd..e07c9dcbad9fe 100644
--- a/clang/lib/StaticAnalyzer/Checkers/CheckSecuritySyntaxOnly.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/CheckSecuritySyntaxOnly.cpp
@@ -50,6 +50,8 @@ struct ChecksFilter {
bool check_UncheckedReturn = false;
bool check_decodeValueOfObjCType = false;
+ bool allowDeprecatedOrUnsafeBufferHandlingWithoutC11 = false;
+
CheckerNameRef checkName_bcmp;
CheckerNameRef checkName_bcopy;
CheckerNameRef checkName_bzero;
@@ -754,7 +756,8 @@ void WalkAST::checkDeprecatedOrUnsafeBufferHandling(const
CallExpr *CE,
if (!filter.check_DeprecatedOrUnsafeBufferHandling)
return;
- if (!BR.getContext().getLangOpts().C11)
+ if (!(BR.getContext().getLangOpts().C11 ||
+filter.allowDeprecatedOrUnsafeBufferHandlingWithoutC11))
return;
// Issue a warning. ArgIndex == -1: Deprecated but not unsafe (has size
@@ -1113,5 +1116,20 @@ REGISTER_CHECKER(rand)
REGISTER_CHECKER(vfork)
REGISTER_CHECKER(FloatLoopCounter)
REGISTER_CHECKER(UncheckedReturn)
-REGISTER_CHECKER(DeprecatedOrUnsafeBufferHandling)
+
+void ento::registerDeprecatedOrUnsafeBufferHandling(CheckerManager &mgr) {
+ SecuritySyntaxChecker *checker = mgr.getChecker();
+ checker->filter.check_DeprecatedOrUnsafeBufferHandling = true;
+ checker->filter.checkName_DeprecatedOrUnsafeBufferHandling =
+ mgr.getCurrentCheckerName();
+ checker->filter.allowDeprecatedOrUnsafeBuffer
[clang] [clang-tools-extra] [clang][analyzer] Add ReportInC99AndEarlier option to DeprecatedOrUnsafeBuf… (PR #168704)
https://github.com/gamesh411 updated
https://github.com/llvm/llvm-project/pull/168704
From dae8506bc6eafa1fcc10e03e3bf7678c17a0babe Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Endre=20F=C3=BCl=C3=B6p?=
Date: Wed, 19 Nov 2025 10:47:40 +0100
Subject: [PATCH 01/17] [clang][analyzer] Add ReportInC99AndEarlier option to
DeprecatedOrUnsafeBufferHandling checker
The checker may report warnings for deprecated buffer handling functions
(memcpy, memset, memmove, etc.) even when not compiling with C11 standard
if the new option "ReportInC99AndEarlier" is set to true.
These functions became deprecated in C11, but may still be problematic in
earlier C standards.
---
clang/docs/analyzer/checkers.rst | 7 +++
.../clang/StaticAnalyzer/Checkers/Checkers.td | 21 +---
.../Checkers/CheckSecuritySyntaxOnly.cpp | 22 -
.../Analysis/Inputs/system-header-simulator.h | 1 +
clang/test/Analysis/analyzer-config.c | 1 +
...ecated-buffer-handling-allow-without-c11.c | 48 +++
6 files changed, 92 insertions(+), 8 deletions(-)
create mode 100644
clang/test/Analysis/security-deprecated-buffer-handling-allow-without-c11.c
diff --git a/clang/docs/analyzer/checkers.rst b/clang/docs/analyzer/checkers.rst
index 31edf9e99dc7d..d93cfe5806ebb 100644
--- a/clang/docs/analyzer/checkers.rst
+++ b/clang/docs/analyzer/checkers.rst
@@ -1785,6 +1785,13 @@ security.insecureAPI.DeprecatedOrUnsafeBufferHandling (C)
strncpy(buf, "a", 1); // warn
}
+The ``AllowWithoutC11`` option allows reporting warnings for these functions
even when not compiling with C11 standard. These functions are deprecated in
C11, but may still be problematic in earlier C standards.
+
+To enable this option, use:
+``-analyzer-config
security.insecureAPI.DeprecatedOrUnsafeBufferHandling:AllowWithoutC11=true``.
+
+By default, this option is set to *false*.
+
.. _security-MmapWriteExec:
security.MmapWriteExec (C)
diff --git a/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
b/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
index ffae3b9310979..310dac5340a18 100644
--- a/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
+++ b/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
@@ -901,12 +901,21 @@ def UncheckedReturn : Checker<"UncheckedReturn">,
Dependencies<[SecuritySyntaxChecker]>,
Documentation;
-def DeprecatedOrUnsafeBufferHandling :
- Checker<"DeprecatedOrUnsafeBufferHandling">,
- HelpText<"Warn on uses of unsecure or deprecated buffer manipulating "
- "functions">,
- Dependencies<[SecuritySyntaxChecker]>,
- Documentation;
+def DeprecatedOrUnsafeBufferHandling
+: Checker<"DeprecatedOrUnsafeBufferHandling">,
+ HelpText<"Warn on uses of unsecure or deprecated buffer manipulating "
+ "functions">,
+ Dependencies<[SecuritySyntaxChecker]>,
+ CheckerOptions<
+ [CmdLineOption<
+ Boolean, "AllowWithoutC11",
+ "Allow reporting deprecated or unsafe buffer handling "
+ "functions even when not compiling with C11 standard. "
+ "These functions are deprecated in C11, but may still be "
+ "problematic in earlier C standards.",
+ "false", Released>,
+]>,
+ Documentation;
def decodeValueOfObjCType : Checker<"decodeValueOfObjCType">,
HelpText<"Warn on uses of the '-decodeValueOfObjCType:at:' method">,
diff --git a/clang/lib/StaticAnalyzer/Checkers/CheckSecuritySyntaxOnly.cpp
b/clang/lib/StaticAnalyzer/Checkers/CheckSecuritySyntaxOnly.cpp
index 5e75c1c4a3abd..e07c9dcbad9fe 100644
--- a/clang/lib/StaticAnalyzer/Checkers/CheckSecuritySyntaxOnly.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/CheckSecuritySyntaxOnly.cpp
@@ -50,6 +50,8 @@ struct ChecksFilter {
bool check_UncheckedReturn = false;
bool check_decodeValueOfObjCType = false;
+ bool allowDeprecatedOrUnsafeBufferHandlingWithoutC11 = false;
+
CheckerNameRef checkName_bcmp;
CheckerNameRef checkName_bcopy;
CheckerNameRef checkName_bzero;
@@ -754,7 +756,8 @@ void WalkAST::checkDeprecatedOrUnsafeBufferHandling(const
CallExpr *CE,
if (!filter.check_DeprecatedOrUnsafeBufferHandling)
return;
- if (!BR.getContext().getLangOpts().C11)
+ if (!(BR.getContext().getLangOpts().C11 ||
+filter.allowDeprecatedOrUnsafeBufferHandlingWithoutC11))
return;
// Issue a warning. ArgIndex == -1: Deprecated but not unsafe (has size
@@ -1113,5 +1116,20 @@ REGISTER_CHECKER(rand)
REGISTER_CHECKER(vfork)
REGISTER_CHECKER(FloatLoopCounter)
REGISTER_CHECKER(UncheckedReturn)
-REGISTER_CHECKER(DeprecatedOrUnsafeBufferHandling)
+
+void ento::registerDeprecatedOrUnsafeBufferHandling(CheckerManager &mgr) {
+ SecuritySyntaxChecker *checker = mgr.getChecker();
+ checker->filter.check_DeprecatedOrUnsafeBufferHandling = true;
+ checker->filter.checkName_DeprecatedOrUnsafeBufferHandling =
+ mgr.getCurrentCheckerName();
+ checker->filter.allowDeprecatedOrUnsafeBuffer
[clang] [clang-tools-extra] [clang][analyzer] Add ReportInC99AndEarlier option to DeprecatedOrUnsafeBuf… (PR #168704)
@@ -0,0 +1,44 @@ +//==- AnnexKDetection.cpp - Annex K availability detection *- C++ +//-*-==// steakhal wrote: ```suggestion /==- AnnexKDetection.cpp - Annex K availability detection *- C++ -*-==// ``` https://github.com/llvm/llvm-project/pull/168704 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang][analyzer] Add ReportInC99AndEarlier option to DeprecatedOrUnsafeBuf… (PR #168704)
@@ -0,0 +1,120 @@
+// DEFINE: %{base_cmd} = %clang_analyze_cc1 %s \
+// DEFINE:
-analyzer-checker=security.insecureAPI.DeprecatedOrUnsafeBufferHandling
+
+// DEFINE: %{verify_flag} =
+
+// DEFINE: %{config_flag_unset} =
+// DEFINE: %{config_flag_all} = -analyzer-config
security.insecureAPI.DeprecatedOrUnsafeBufferHandling:ReportMode=all
+// DEFINE: %{config_flag_actionable} = -analyzer-config
security.insecureAPI.DeprecatedOrUnsafeBufferHandling:ReportMode=actionable
+// DEFINE: %{config_flag_c11_only} = -analyzer-config
security.insecureAPI.DeprecatedOrUnsafeBufferHandling:ReportMode=c11-only
+// DEFINE: %{config_flag} = %{config_flag_unset}
+
+// DEFINE: %{std_flag_c99} = -std=gnu99
+// DEFINE: %{std_flag_c11} = -std=gnu11
+// DEFINE: %{std_flag} = %{std_flag_c99}
+
+// DEFINE: %{annexk_defines_unset} =
+// DEFINE: %{annexk_defines_set} = -D__STDC_LIB_EXT1__=200509L
-D__STDC_WANT_LIB_EXT1__=1
+// DEFINE: %{annexk_flag} = %{annexk_defines_unset}
+
+// DEFINE: %{run_cmd} = %{base_cmd} %{verify_flag} %{std_flag} %{annexk_flag}
%{config_flag}
+
+// These cases should warn
+
+// REDEFINE: %{verify_flag} = -verify=common
+// REDEFINE: %{std_flag} = %{std_flag_c99}
+// REDEFINE: %{annexk_flag} = %{annexk_defines_unset}
+// REDEFINE: %{config_flag} = %{config_flag_all}
+// RUN: %{run_cmd}
+
+// C99 with "all" mode
+// REDEFINE: %{verify_flag} = -verify=common
+// REDEFINE: %{std_flag} = %{std_flag_c99}
+// REDEFINE: %{annexk_flag} = %{annexk_defines_unset}
+// REDEFINE: %{config_flag} = %{config_flag_all}
+// RUN: %{run_cmd}
+
+// C11 with default mode
+// REDEFINE: %{verify_flag} = -verify=common
+// REDEFINE: %{std_flag} = %{std_flag_c11}
+// REDEFINE: %{annexk_flag} = %{annexk_defines_unset}
+// REDEFINE: %{config_flag} = %{config_flag_unset}
+// RUN: %{run_cmd}
+
+// C11 with "all" mode
+// REDEFINE: %{verify_flag} = -verify=common
+// REDEFINE: %{std_flag} = %{std_flag_c11}
+// REDEFINE: %{annexk_flag} = %{annexk_defines_unset}
+// REDEFINE: %{config_flag} = %{config_flag_all}
+// RUN: %{run_cmd}
+
+// C11 with "c11-only" mode
+// REDEFINE: %{verify_flag} = -verify=common
+// REDEFINE: %{std_flag} = %{std_flag_c11}
+// REDEFINE: %{annexk_flag} = %{annexk_defines_unset}
+// REDEFINE: %{config_flag} = %{config_flag_c11_only}
+// RUN: %{run_cmd}
+
+// C11 with "actionable" mode and Annex K available
+// REDEFINE: %{verify_flag} = -verify=common
+// REDEFINE: %{std_flag} = %{std_flag_c11}
+// REDEFINE: %{annexk_flag} = %{annexk_defines_set}
+// REDEFINE: %{config_flag} = %{config_flag_actionable}
+// RUN: %{run_cmd}
+
+// These cases should not warn
+
+// C99 with default mode
+// REDEFINE: %{verify_flag} = -verify=c99-default
+// REDEFINE: %{std_flag} = %{std_flag_c99}
+// REDEFINE: %{annexk_flag} = %{annexk_defines_unset}
+// REDEFINE: %{config_flag} = %{config_flag_unset}
+// RUN: %{run_cmd}
+
+// C99 with "actionable" mode and no Annex K
+// REDEFINE: %{verify_flag} = -verify=c99-actionable
+// REDEFINE: %{std_flag} = %{std_flag_c99}
+// REDEFINE: %{annexk_flag} = %{annexk_defines_unset}
+// REDEFINE: %{config_flag} = %{config_flag_actionable}
+// RUN: %{run_cmd}
+
+// C99 with "c11-only" mode
+// REDEFINE: %{verify_flag} = -verify=c99-c11only
+// REDEFINE: %{std_flag} = %{std_flag_c99}
+// REDEFINE: %{annexk_flag} = %{annexk_defines_unset}
+// REDEFINE: %{config_flag} = %{config_flag_c11_only}
+// RUN: %{run_cmd}
+
+// C11 with "actionable" mode and no Annex K
+// REDEFINE: %{verify_flag} = -verify=c11-actionable-noannex
+// REDEFINE: %{std_flag} = %{std_flag_c11}
+// REDEFINE: %{annexk_flag} = %{annexk_defines_unset}
+// REDEFINE: %{config_flag} = %{config_flag_actionable}
+// RUN: %{run_cmd}
steakhal wrote:
I had something else in mind.
I probably messed up the translation, but you should get my intentions:
```c++
// DEFINE: %{analyze-cmd} = %clang_analyze_cc1 %s \
// DEFINE:
-analyzer-checker=security.insecureAPI.DeprecatedOrUnsafeBufferHandling
// DEFINE: %{ReportMode} = -analyzer-config
security.insecureAPI.DeprecatedOrUnsafeBufferHandling:ReportMode
// DEFINE: %{EnableAnnexK} = -D__STDC_LIB_EXT1__=200509L
-D__STDC_WANT_LIB_EXT1__=1
// These cases should warn:
// RUN: %{analyze-cmd} -std=gnu99 %{ReportMode}=all
-verify=common
// RUN: %{analyze-cmd} -std=gnu11
-verify=common
// RUN: %{analyze-cmd} -std=gnu11 %{ReportMode}=all
-verify=common
// RUN: %{analyze-cmd} -std=gnu11 %{ReportMode}=c11-only
-verify=common
// RUN: %{analyze-cmd} -std=gnu11 %{ReportMode}=actionable %{EnableAnnexK}
-verify=common
// These cases should not warn:
// RUN: %{analyze-cmd} -std=gnu99 %{ReportMode}=all
-verify=c99-default
// RUN: %{analyze-cmd} -std=gnu99 %{ReportMode}=actionable
-verify=c99-actionable
// RUN: %{analyze-cmd} -std=gnu11 %{ReportMode}=actionable
-verify=c99-c11only
// RUN: %{analyze-cmd} -st
[clang] [clang-tools-extra] [clang][analyzer] Add ReportInC99AndEarlier option to DeprecatedOrUnsafeBuf… (PR #168704)
https://github.com/steakhal requested changes to this pull request. https://github.com/llvm/llvm-project/pull/168704 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang][analyzer] Add ReportInC99AndEarlier option to DeprecatedOrUnsafeBuf… (PR #168704)
@@ -1113,5 +1141,32 @@ REGISTER_CHECKER(rand)
REGISTER_CHECKER(vfork)
REGISTER_CHECKER(FloatLoopCounter)
REGISTER_CHECKER(UncheckedReturn)
-REGISTER_CHECKER(DeprecatedOrUnsafeBufferHandling)
+
+void ento::registerDeprecatedOrUnsafeBufferHandling(CheckerManager &Mgr) {
+ SecuritySyntaxChecker *Checker = Mgr.getChecker();
+ Checker->filter.check_DeprecatedOrUnsafeBufferHandling = true;
+ Checker->filter.checkName_DeprecatedOrUnsafeBufferHandling =
+ Mgr.getCurrentCheckerName();
+
+ // Parse ReportMode option (defaults to C11Only for backward compatibility)
+ StringRef ReportModeStr = Mgr.getAnalyzerOptions().getCheckerStringOption(
+ Mgr.getCurrentCheckerName(), "ReportMode");
+ if (ReportModeStr == "all")
+Checker->filter.ReportMode = ReportPolicy::All;
+ else if (ReportModeStr == "actionable")
+Checker->filter.ReportMode = ReportPolicy::Actionable;
+ else if (ReportModeStr == "c11-only")
+Checker->filter.ReportMode = ReportPolicy::C11Only;
steakhal wrote:
This reminded me of `llvm::SwtringSwitch`
https://github.com/llvm/llvm-project/pull/168704
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang][analyzer] Add ReportInC99AndEarlier option to DeprecatedOrUnsafeBuf… (PR #168704)
@@ -0,0 +1,120 @@
+// DEFINE: %{base_cmd} = %clang_analyze_cc1 %s \
+// DEFINE:
-analyzer-checker=security.insecureAPI.DeprecatedOrUnsafeBufferHandling
+
+// DEFINE: %{verify_flag} =
+
+// DEFINE: %{config_flag_unset} =
+// DEFINE: %{config_flag_all} = -analyzer-config
security.insecureAPI.DeprecatedOrUnsafeBufferHandling:ReportMode=all
+// DEFINE: %{config_flag_actionable} = -analyzer-config
security.insecureAPI.DeprecatedOrUnsafeBufferHandling:ReportMode=actionable
+// DEFINE: %{config_flag_c11_only} = -analyzer-config
security.insecureAPI.DeprecatedOrUnsafeBufferHandling:ReportMode=c11-only
+// DEFINE: %{config_flag} = %{config_flag_unset}
+
+// DEFINE: %{std_flag_c99} = -std=gnu99
+// DEFINE: %{std_flag_c11} = -std=gnu11
+// DEFINE: %{std_flag} = %{std_flag_c99}
+
+// DEFINE: %{annexk_defines_unset} =
+// DEFINE: %{annexk_defines_set} = -D__STDC_LIB_EXT1__=200509L
-D__STDC_WANT_LIB_EXT1__=1
+// DEFINE: %{annexk_flag} = %{annexk_defines_unset}
+
+// DEFINE: %{run_cmd} = %{base_cmd} %{verify_flag} %{std_flag} %{annexk_flag}
%{config_flag}
+
+// These cases should warn
+
+// REDEFINE: %{verify_flag} = -verify=common
+// REDEFINE: %{std_flag} = %{std_flag_c99}
+// REDEFINE: %{annexk_flag} = %{annexk_defines_unset}
+// REDEFINE: %{config_flag} = %{config_flag_all}
+// RUN: %{run_cmd}
+
+// C99 with "all" mode
+// REDEFINE: %{verify_flag} = -verify=common
+// REDEFINE: %{std_flag} = %{std_flag_c99}
+// REDEFINE: %{annexk_flag} = %{annexk_defines_unset}
+// REDEFINE: %{config_flag} = %{config_flag_all}
+// RUN: %{run_cmd}
+
+// C11 with default mode
+// REDEFINE: %{verify_flag} = -verify=common
+// REDEFINE: %{std_flag} = %{std_flag_c11}
+// REDEFINE: %{annexk_flag} = %{annexk_defines_unset}
+// REDEFINE: %{config_flag} = %{config_flag_unset}
+// RUN: %{run_cmd}
+
+// C11 with "all" mode
+// REDEFINE: %{verify_flag} = -verify=common
+// REDEFINE: %{std_flag} = %{std_flag_c11}
+// REDEFINE: %{annexk_flag} = %{annexk_defines_unset}
+// REDEFINE: %{config_flag} = %{config_flag_all}
+// RUN: %{run_cmd}
+
+// C11 with "c11-only" mode
+// REDEFINE: %{verify_flag} = -verify=common
+// REDEFINE: %{std_flag} = %{std_flag_c11}
+// REDEFINE: %{annexk_flag} = %{annexk_defines_unset}
+// REDEFINE: %{config_flag} = %{config_flag_c11_only}
+// RUN: %{run_cmd}
+
+// C11 with "actionable" mode and Annex K available
+// REDEFINE: %{verify_flag} = -verify=common
+// REDEFINE: %{std_flag} = %{std_flag_c11}
+// REDEFINE: %{annexk_flag} = %{annexk_defines_set}
+// REDEFINE: %{config_flag} = %{config_flag_actionable}
+// RUN: %{run_cmd}
+
+// These cases should not warn
+
+// C99 with default mode
+// REDEFINE: %{verify_flag} = -verify=c99-default
+// REDEFINE: %{std_flag} = %{std_flag_c99}
+// REDEFINE: %{annexk_flag} = %{annexk_defines_unset}
+// REDEFINE: %{config_flag} = %{config_flag_unset}
+// RUN: %{run_cmd}
+
+// C99 with "actionable" mode and no Annex K
+// REDEFINE: %{verify_flag} = -verify=c99-actionable
+// REDEFINE: %{std_flag} = %{std_flag_c99}
+// REDEFINE: %{annexk_flag} = %{annexk_defines_unset}
+// REDEFINE: %{config_flag} = %{config_flag_actionable}
+// RUN: %{run_cmd}
+
+// C99 with "c11-only" mode
+// REDEFINE: %{verify_flag} = -verify=c99-c11only
+// REDEFINE: %{std_flag} = %{std_flag_c99}
+// REDEFINE: %{annexk_flag} = %{annexk_defines_unset}
+// REDEFINE: %{config_flag} = %{config_flag_c11_only}
+// RUN: %{run_cmd}
+
+// C11 with "actionable" mode and no Annex K
+// REDEFINE: %{verify_flag} = -verify=c11-actionable-noannex
+// REDEFINE: %{std_flag} = %{std_flag_c11}
+// REDEFINE: %{annexk_flag} = %{annexk_defines_unset}
+// REDEFINE: %{config_flag} = %{config_flag_actionable}
+// RUN: %{run_cmd}
steakhal wrote:
Frankly, if only a handful of the lines set the `%{EnableAnnexK}` then we could
squash those whitespaces without loosing much.
https://github.com/llvm/llvm-project/pull/168704
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang][analyzer] Add ReportInC99AndEarlier option to DeprecatedOrUnsafeBuf… (PR #168704)
@@ -754,7 +760,22 @@ void WalkAST::checkDeprecatedOrUnsafeBufferHandling(const
CallExpr *CE,
if (!filter.check_DeprecatedOrUnsafeBufferHandling)
return;
- if (!BR.getContext().getLangOpts().C11)
+ const bool ShouldReport = [this] {
+const bool IsAnnexKAvailable = analysis::isAnnexKAvailable(
+&BR.getPreprocessor(), BR.getContext().getLangOpts());
+const bool IsC11OrLaterStandard = BR.getContext().getLangOpts().C11;
steakhal wrote:
A walker is constructed inside the `checkASTCodeBody` which is per fn
definition; so it's not cached per TU, but rather per definition - which is
different.
https://github.com/llvm/llvm-project/pull/168704
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang][analyzer] Add ReportInC99AndEarlier option to DeprecatedOrUnsafeBuf… (PR #168704)
@@ -0,0 +1,40 @@ +//==- AnnexKDetection.h - Annex K availability detection *- C++ -*-==// steakhal wrote: ```suggestion //==- AnnexKDetection.h - Annex K availability detection -*- C++ -*-==// ``` https://github.com/llvm/llvm-project/pull/168704 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang][analyzer] Add ReportInC99AndEarlier option to DeprecatedOrUnsafeBuf… (PR #168704)
https://github.com/NagyDonat commented: Thanks for the commit! At first glance LGTM, I didn't see any obvious issue (but I didn't ponder about details too much). https://github.com/llvm/llvm-project/pull/168704 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang][analyzer] Add ReportInC99AndEarlier option to DeprecatedOrUnsafeBuf… (PR #168704)
https://github.com/gamesh411 updated
https://github.com/llvm/llvm-project/pull/168704
From dae8506bc6eafa1fcc10e03e3bf7678c17a0babe Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Endre=20F=C3=BCl=C3=B6p?=
Date: Wed, 19 Nov 2025 10:47:40 +0100
Subject: [PATCH 01/13] [clang][analyzer] Add ReportInC99AndEarlier option to
DeprecatedOrUnsafeBufferHandling checker
The checker may report warnings for deprecated buffer handling functions
(memcpy, memset, memmove, etc.) even when not compiling with C11 standard
if the new option "ReportInC99AndEarlier" is set to true.
These functions became deprecated in C11, but may still be problematic in
earlier C standards.
---
clang/docs/analyzer/checkers.rst | 7 +++
.../clang/StaticAnalyzer/Checkers/Checkers.td | 21 +---
.../Checkers/CheckSecuritySyntaxOnly.cpp | 22 -
.../Analysis/Inputs/system-header-simulator.h | 1 +
clang/test/Analysis/analyzer-config.c | 1 +
...ecated-buffer-handling-allow-without-c11.c | 48 +++
6 files changed, 92 insertions(+), 8 deletions(-)
create mode 100644
clang/test/Analysis/security-deprecated-buffer-handling-allow-without-c11.c
diff --git a/clang/docs/analyzer/checkers.rst b/clang/docs/analyzer/checkers.rst
index 31edf9e99dc7d..d93cfe5806ebb 100644
--- a/clang/docs/analyzer/checkers.rst
+++ b/clang/docs/analyzer/checkers.rst
@@ -1785,6 +1785,13 @@ security.insecureAPI.DeprecatedOrUnsafeBufferHandling (C)
strncpy(buf, "a", 1); // warn
}
+The ``AllowWithoutC11`` option allows reporting warnings for these functions
even when not compiling with C11 standard. These functions are deprecated in
C11, but may still be problematic in earlier C standards.
+
+To enable this option, use:
+``-analyzer-config
security.insecureAPI.DeprecatedOrUnsafeBufferHandling:AllowWithoutC11=true``.
+
+By default, this option is set to *false*.
+
.. _security-MmapWriteExec:
security.MmapWriteExec (C)
diff --git a/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
b/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
index ffae3b9310979..310dac5340a18 100644
--- a/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
+++ b/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
@@ -901,12 +901,21 @@ def UncheckedReturn : Checker<"UncheckedReturn">,
Dependencies<[SecuritySyntaxChecker]>,
Documentation;
-def DeprecatedOrUnsafeBufferHandling :
- Checker<"DeprecatedOrUnsafeBufferHandling">,
- HelpText<"Warn on uses of unsecure or deprecated buffer manipulating "
- "functions">,
- Dependencies<[SecuritySyntaxChecker]>,
- Documentation;
+def DeprecatedOrUnsafeBufferHandling
+: Checker<"DeprecatedOrUnsafeBufferHandling">,
+ HelpText<"Warn on uses of unsecure or deprecated buffer manipulating "
+ "functions">,
+ Dependencies<[SecuritySyntaxChecker]>,
+ CheckerOptions<
+ [CmdLineOption<
+ Boolean, "AllowWithoutC11",
+ "Allow reporting deprecated or unsafe buffer handling "
+ "functions even when not compiling with C11 standard. "
+ "These functions are deprecated in C11, but may still be "
+ "problematic in earlier C standards.",
+ "false", Released>,
+]>,
+ Documentation;
def decodeValueOfObjCType : Checker<"decodeValueOfObjCType">,
HelpText<"Warn on uses of the '-decodeValueOfObjCType:at:' method">,
diff --git a/clang/lib/StaticAnalyzer/Checkers/CheckSecuritySyntaxOnly.cpp
b/clang/lib/StaticAnalyzer/Checkers/CheckSecuritySyntaxOnly.cpp
index 5e75c1c4a3abd..e07c9dcbad9fe 100644
--- a/clang/lib/StaticAnalyzer/Checkers/CheckSecuritySyntaxOnly.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/CheckSecuritySyntaxOnly.cpp
@@ -50,6 +50,8 @@ struct ChecksFilter {
bool check_UncheckedReturn = false;
bool check_decodeValueOfObjCType = false;
+ bool allowDeprecatedOrUnsafeBufferHandlingWithoutC11 = false;
+
CheckerNameRef checkName_bcmp;
CheckerNameRef checkName_bcopy;
CheckerNameRef checkName_bzero;
@@ -754,7 +756,8 @@ void WalkAST::checkDeprecatedOrUnsafeBufferHandling(const
CallExpr *CE,
if (!filter.check_DeprecatedOrUnsafeBufferHandling)
return;
- if (!BR.getContext().getLangOpts().C11)
+ if (!(BR.getContext().getLangOpts().C11 ||
+filter.allowDeprecatedOrUnsafeBufferHandlingWithoutC11))
return;
// Issue a warning. ArgIndex == -1: Deprecated but not unsafe (has size
@@ -1113,5 +1116,20 @@ REGISTER_CHECKER(rand)
REGISTER_CHECKER(vfork)
REGISTER_CHECKER(FloatLoopCounter)
REGISTER_CHECKER(UncheckedReturn)
-REGISTER_CHECKER(DeprecatedOrUnsafeBufferHandling)
+
+void ento::registerDeprecatedOrUnsafeBufferHandling(CheckerManager &mgr) {
+ SecuritySyntaxChecker *checker = mgr.getChecker();
+ checker->filter.check_DeprecatedOrUnsafeBufferHandling = true;
+ checker->filter.checkName_DeprecatedOrUnsafeBufferHandling =
+ mgr.getCurrentCheckerName();
+ checker->filter.allowDeprecatedOrUnsafeBuffer
[clang] [clang-tools-extra] [clang][analyzer] Add ReportInC99AndEarlier option to DeprecatedOrUnsafeBuf… (PR #168704)
@@ -0,0 +1,74 @@ +// These cases should not warn + +// C99 with "all" mode +// RUN: %clang_analyze_cc1 %s -verify=common -std=gnu99 \ +// RUN: -analyzer-checker=security.insecureAPI.DeprecatedOrUnsafeBufferHandling \ +// RUN: -analyzer-config security.insecureAPI.DeprecatedOrUnsafeBufferHandling:ReportMode=all gamesh411 wrote: I have introduced the `DEFINE:` and `REDEFINE:` lit test suite primitive. I think it is a bit more readable now. https://github.com/llvm/llvm-project/pull/168704 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang][analyzer] Add ReportInC99AndEarlier option to DeprecatedOrUnsafeBuf… (PR #168704)
@@ -1113,5 +1134,32 @@ REGISTER_CHECKER(rand)
REGISTER_CHECKER(vfork)
REGISTER_CHECKER(FloatLoopCounter)
REGISTER_CHECKER(UncheckedReturn)
-REGISTER_CHECKER(DeprecatedOrUnsafeBufferHandling)
+
+void ento::registerDeprecatedOrUnsafeBufferHandling(CheckerManager &Mgr) {
+ SecuritySyntaxChecker *Checker = Mgr.getChecker();
+ Checker->filter.check_DeprecatedOrUnsafeBufferHandling = true;
+ Checker->filter.checkName_DeprecatedOrUnsafeBufferHandling =
+ Mgr.getCurrentCheckerName();
+
+ // Parse ReportMode option (defaults to C11Only for backward compatibility)
+ StringRef ReportModeStr = Mgr.getAnalyzerOptions().getCheckerStringOption(
+ Mgr.getCurrentCheckerName(), "ReportMode");
+ if (ReportModeStr == "all")
+Checker->filter.ReportMode = ReportPolicy::All;
+ else if (ReportModeStr == "actionable")
+Checker->filter.ReportMode = ReportPolicy::Actionable;
+ else if (ReportModeStr == "c11-only")
+Checker->filter.ReportMode = ReportPolicy::C11Only;
+ else
+Mgr.reportInvalidCheckerOptionValue(
+Checker, "ReportMode",
+"ReportMode should be one of the folliwing values: \"all\", "
gamesh411 wrote:
fixed
https://github.com/llvm/llvm-project/pull/168704
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang][analyzer] Add ReportInC99AndEarlier option to DeprecatedOrUnsafeBuf… (PR #168704)
@@ -754,7 +760,22 @@ void WalkAST::checkDeprecatedOrUnsafeBufferHandling(const
CallExpr *CE,
if (!filter.check_DeprecatedOrUnsafeBufferHandling)
return;
- if (!BR.getContext().getLangOpts().C11)
+ const bool ShouldReport = [this] {
+const bool IsAnnexKAvailable = analysis::isAnnexKAvailable(
+&BR.getPreprocessor(), BR.getContext().getLangOpts());
+const bool IsC11OrLaterStandard = BR.getContext().getLangOpts().C11;
gamesh411 wrote:
True, we do not have to recompute this every time. I have cached this value
inside of the visitor class, so it is cached per TU.
https://github.com/llvm/llvm-project/pull/168704
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang][analyzer] Add ReportInC99AndEarlier option to DeprecatedOrUnsafeBuf… (PR #168704)
@@ -901,12 +901,25 @@ def UncheckedReturn : Checker<"UncheckedReturn">, Dependencies<[SecuritySyntaxChecker]>, Documentation; -def DeprecatedOrUnsafeBufferHandling : - Checker<"DeprecatedOrUnsafeBufferHandling">, - HelpText<"Warn on uses of unsecure or deprecated buffer manipulating " - "functions">, - Dependencies<[SecuritySyntaxChecker]>, - Documentation; +def DeprecatedOrUnsafeBufferHandling +: Checker<"DeprecatedOrUnsafeBufferHandling">, + HelpText<"Warn on uses of unsecure or deprecated buffer manipulating " + "functions">, + Dependencies<[SecuritySyntaxChecker]>, + CheckerOptions< + [CmdLineOption< + String, "ReportMode", + "Controls when warnings are reported. \"all\" reports all " + "unsafe " + "functions regardless of C standard or Annex K availability. " + "\"actionable\" only reports when Annex K is available (C11 " + "with " gamesh411 wrote: Reflowed the description. https://github.com/llvm/llvm-project/pull/168704 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang][analyzer] Add ReportInC99AndEarlier option to DeprecatedOrUnsafeBuf… (PR #168704)
@@ -0,0 +1,61 @@
+//==- AnnexKDetection.h - Annex K availability detection *- C++ -*-==//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// This file provides utilities for detecting C11 Annex K (Bounds-checking
+// interfaces) availability.
+//
+//===--===//
+
+#ifndef LLVM_CLANG_ANALYSIS_ANNEXKDETECTION_H
+#define LLVM_CLANG_ANALYSIS_ANNEXKDETECTION_H
+
+#include "clang/Basic/LangOptions.h"
+#include "clang/Lex/Preprocessor.h"
+#include "llvm/ADT/StringRef.h"
+
+namespace clang {
+namespace analysis {
+
+/// Calculates whether Annex K is available for the current translation unit
+/// based on the macro definitions and the language options.
+///
+/// Annex K (Bounds-checking interfaces) is available when:
+/// 1. C11 standard is enabled
+/// 2. __STDC_LIB_EXT1__ macro is defined (indicates library support)
+/// 3. __STDC_WANT_LIB_EXT1__ macro is defined and equals "1" (indicates user
+///opt-in)
+///
+/// \param PP The preprocessor instance to check macro definitions.
+/// \param LO The language options to check C11 standard.
+/// \returns true if Annex K is available, false otherwise.
+inline bool isAnnexKAvailable(Preprocessor *PP, const LangOptions &LO) {
gamesh411 wrote:
I have moved it into the clangAnalysis module. That might be a good place for
this functionality IMO.
https://github.com/llvm/llvm-project/pull/168704
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang][analyzer] Add ReportInC99AndEarlier option to DeprecatedOrUnsafeBuf… (PR #168704)
@@ -0,0 +1,61 @@
+//==- AnnexKDetection.h - Annex K availability detection *- C++ -*-==//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// This file provides utilities for detecting C11 Annex K (Bounds-checking
+// interfaces) availability.
+//
+//===--===//
+
+#ifndef LLVM_CLANG_ANALYSIS_ANNEXKDETECTION_H
+#define LLVM_CLANG_ANALYSIS_ANNEXKDETECTION_H
+
+#include "clang/Basic/LangOptions.h"
+#include "clang/Lex/Preprocessor.h"
+#include "llvm/ADT/StringRef.h"
+
+namespace clang {
+namespace analysis {
gamesh411 wrote:
fixed
https://github.com/llvm/llvm-project/pull/168704
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang][analyzer] Add ReportInC99AndEarlier option to DeprecatedOrUnsafeBuf… (PR #168704)
https://github.com/gamesh411 updated
https://github.com/llvm/llvm-project/pull/168704
From dae8506bc6eafa1fcc10e03e3bf7678c17a0babe Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Endre=20F=C3=BCl=C3=B6p?=
Date: Wed, 19 Nov 2025 10:47:40 +0100
Subject: [PATCH 01/13] [clang][analyzer] Add ReportInC99AndEarlier option to
DeprecatedOrUnsafeBufferHandling checker
The checker may report warnings for deprecated buffer handling functions
(memcpy, memset, memmove, etc.) even when not compiling with C11 standard
if the new option "ReportInC99AndEarlier" is set to true.
These functions became deprecated in C11, but may still be problematic in
earlier C standards.
---
clang/docs/analyzer/checkers.rst | 7 +++
.../clang/StaticAnalyzer/Checkers/Checkers.td | 21 +---
.../Checkers/CheckSecuritySyntaxOnly.cpp | 22 -
.../Analysis/Inputs/system-header-simulator.h | 1 +
clang/test/Analysis/analyzer-config.c | 1 +
...ecated-buffer-handling-allow-without-c11.c | 48 +++
6 files changed, 92 insertions(+), 8 deletions(-)
create mode 100644
clang/test/Analysis/security-deprecated-buffer-handling-allow-without-c11.c
diff --git a/clang/docs/analyzer/checkers.rst b/clang/docs/analyzer/checkers.rst
index 31edf9e99dc7d..d93cfe5806ebb 100644
--- a/clang/docs/analyzer/checkers.rst
+++ b/clang/docs/analyzer/checkers.rst
@@ -1785,6 +1785,13 @@ security.insecureAPI.DeprecatedOrUnsafeBufferHandling (C)
strncpy(buf, "a", 1); // warn
}
+The ``AllowWithoutC11`` option allows reporting warnings for these functions
even when not compiling with C11 standard. These functions are deprecated in
C11, but may still be problematic in earlier C standards.
+
+To enable this option, use:
+``-analyzer-config
security.insecureAPI.DeprecatedOrUnsafeBufferHandling:AllowWithoutC11=true``.
+
+By default, this option is set to *false*.
+
.. _security-MmapWriteExec:
security.MmapWriteExec (C)
diff --git a/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
b/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
index ffae3b9310979..310dac5340a18 100644
--- a/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
+++ b/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
@@ -901,12 +901,21 @@ def UncheckedReturn : Checker<"UncheckedReturn">,
Dependencies<[SecuritySyntaxChecker]>,
Documentation;
-def DeprecatedOrUnsafeBufferHandling :
- Checker<"DeprecatedOrUnsafeBufferHandling">,
- HelpText<"Warn on uses of unsecure or deprecated buffer manipulating "
- "functions">,
- Dependencies<[SecuritySyntaxChecker]>,
- Documentation;
+def DeprecatedOrUnsafeBufferHandling
+: Checker<"DeprecatedOrUnsafeBufferHandling">,
+ HelpText<"Warn on uses of unsecure or deprecated buffer manipulating "
+ "functions">,
+ Dependencies<[SecuritySyntaxChecker]>,
+ CheckerOptions<
+ [CmdLineOption<
+ Boolean, "AllowWithoutC11",
+ "Allow reporting deprecated or unsafe buffer handling "
+ "functions even when not compiling with C11 standard. "
+ "These functions are deprecated in C11, but may still be "
+ "problematic in earlier C standards.",
+ "false", Released>,
+]>,
+ Documentation;
def decodeValueOfObjCType : Checker<"decodeValueOfObjCType">,
HelpText<"Warn on uses of the '-decodeValueOfObjCType:at:' method">,
diff --git a/clang/lib/StaticAnalyzer/Checkers/CheckSecuritySyntaxOnly.cpp
b/clang/lib/StaticAnalyzer/Checkers/CheckSecuritySyntaxOnly.cpp
index 5e75c1c4a3abd..e07c9dcbad9fe 100644
--- a/clang/lib/StaticAnalyzer/Checkers/CheckSecuritySyntaxOnly.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/CheckSecuritySyntaxOnly.cpp
@@ -50,6 +50,8 @@ struct ChecksFilter {
bool check_UncheckedReturn = false;
bool check_decodeValueOfObjCType = false;
+ bool allowDeprecatedOrUnsafeBufferHandlingWithoutC11 = false;
+
CheckerNameRef checkName_bcmp;
CheckerNameRef checkName_bcopy;
CheckerNameRef checkName_bzero;
@@ -754,7 +756,8 @@ void WalkAST::checkDeprecatedOrUnsafeBufferHandling(const
CallExpr *CE,
if (!filter.check_DeprecatedOrUnsafeBufferHandling)
return;
- if (!BR.getContext().getLangOpts().C11)
+ if (!(BR.getContext().getLangOpts().C11 ||
+filter.allowDeprecatedOrUnsafeBufferHandlingWithoutC11))
return;
// Issue a warning. ArgIndex == -1: Deprecated but not unsafe (has size
@@ -1113,5 +1116,20 @@ REGISTER_CHECKER(rand)
REGISTER_CHECKER(vfork)
REGISTER_CHECKER(FloatLoopCounter)
REGISTER_CHECKER(UncheckedReturn)
-REGISTER_CHECKER(DeprecatedOrUnsafeBufferHandling)
+
+void ento::registerDeprecatedOrUnsafeBufferHandling(CheckerManager &mgr) {
+ SecuritySyntaxChecker *checker = mgr.getChecker();
+ checker->filter.check_DeprecatedOrUnsafeBufferHandling = true;
+ checker->filter.checkName_DeprecatedOrUnsafeBufferHandling =
+ mgr.getCurrentCheckerName();
+ checker->filter.allowDeprecatedOrUnsafeBuffer
[clang] [clang-tools-extra] [clang][analyzer] Add ReportInC99AndEarlier option to DeprecatedOrUnsafeBuf… (PR #168704)
@@ -0,0 +1,74 @@ +// These cases should not warn + +// C99 with "all" mode +// RUN: %clang_analyze_cc1 %s -verify=common -std=gnu99 \ +// RUN: -analyzer-checker=security.insecureAPI.DeprecatedOrUnsafeBufferHandling \ +// RUN: -analyzer-config security.insecureAPI.DeprecatedOrUnsafeBufferHandling:ReportMode=all steakhal wrote: There is quite a bit of repetition in the RUN lines. I wonder if using the DEFINE lit directive would enable us to reduce it, so the tests could focus on the actual differences. WDYT? https://github.com/llvm/llvm-project/pull/168704 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang][analyzer] Add ReportInC99AndEarlier option to DeprecatedOrUnsafeBuf… (PR #168704)
@@ -1113,5 +1134,32 @@ REGISTER_CHECKER(rand)
REGISTER_CHECKER(vfork)
REGISTER_CHECKER(FloatLoopCounter)
REGISTER_CHECKER(UncheckedReturn)
-REGISTER_CHECKER(DeprecatedOrUnsafeBufferHandling)
+
+void ento::registerDeprecatedOrUnsafeBufferHandling(CheckerManager &Mgr) {
+ SecuritySyntaxChecker *Checker = Mgr.getChecker();
+ Checker->filter.check_DeprecatedOrUnsafeBufferHandling = true;
+ Checker->filter.checkName_DeprecatedOrUnsafeBufferHandling =
+ Mgr.getCurrentCheckerName();
+
+ // Parse ReportMode option (defaults to C11Only for backward compatibility)
+ StringRef ReportModeStr = Mgr.getAnalyzerOptions().getCheckerStringOption(
+ Mgr.getCurrentCheckerName(), "ReportMode");
+ if (ReportModeStr == "all")
+Checker->filter.ReportMode = ReportPolicy::All;
+ else if (ReportModeStr == "actionable")
+Checker->filter.ReportMode = ReportPolicy::Actionable;
+ else if (ReportModeStr == "c11-only")
+Checker->filter.ReportMode = ReportPolicy::C11Only;
+ else
+Mgr.reportInvalidCheckerOptionValue(
+Checker, "ReportMode",
+"ReportMode should be one of the folliwing values: \"all\", "
steakhal wrote:
```suggestion
"ReportMode should be one of the following values: \"all\", "
```
https://github.com/llvm/llvm-project/pull/168704
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang][analyzer] Add ReportInC99AndEarlier option to DeprecatedOrUnsafeBuf… (PR #168704)
@@ -0,0 +1,61 @@
+//==- AnnexKDetection.h - Annex K availability detection *- C++ -*-==//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// This file provides utilities for detecting C11 Annex K (Bounds-checking
+// interfaces) availability.
+//
+//===--===//
+
+#ifndef LLVM_CLANG_ANALYSIS_ANNEXKDETECTION_H
+#define LLVM_CLANG_ANALYSIS_ANNEXKDETECTION_H
+
+#include "clang/Basic/LangOptions.h"
+#include "clang/Lex/Preprocessor.h"
+#include "llvm/ADT/StringRef.h"
+
+namespace clang {
+namespace analysis {
steakhal wrote:
```suggestion
namespace clang::analysis {
```
https://github.com/llvm/llvm-project/pull/168704
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang][analyzer] Add ReportInC99AndEarlier option to DeprecatedOrUnsafeBuf… (PR #168704)
@@ -901,12 +901,25 @@ def UncheckedReturn : Checker<"UncheckedReturn">, Dependencies<[SecuritySyntaxChecker]>, Documentation; -def DeprecatedOrUnsafeBufferHandling : - Checker<"DeprecatedOrUnsafeBufferHandling">, - HelpText<"Warn on uses of unsecure or deprecated buffer manipulating " - "functions">, - Dependencies<[SecuritySyntaxChecker]>, - Documentation; +def DeprecatedOrUnsafeBufferHandling +: Checker<"DeprecatedOrUnsafeBufferHandling">, + HelpText<"Warn on uses of unsecure or deprecated buffer manipulating " + "functions">, + Dependencies<[SecuritySyntaxChecker]>, + CheckerOptions< + [CmdLineOption< + String, "ReportMode", + "Controls when warnings are reported. \"all\" reports all " + "unsafe " + "functions regardless of C standard or Annex K availability. " + "\"actionable\" only reports when Annex K is available (C11 " + "with " steakhal wrote: I think you should re-flow the comments. Its weird to see a single word in a dedicated line. https://github.com/llvm/llvm-project/pull/168704 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang][analyzer] Add ReportInC99AndEarlier option to DeprecatedOrUnsafeBuf… (PR #168704)
https://github.com/steakhal edited https://github.com/llvm/llvm-project/pull/168704 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang][analyzer] Add ReportInC99AndEarlier option to DeprecatedOrUnsafeBuf… (PR #168704)
@@ -754,7 +760,22 @@ void WalkAST::checkDeprecatedOrUnsafeBufferHandling(const
CallExpr *CE,
if (!filter.check_DeprecatedOrUnsafeBufferHandling)
return;
- if (!BR.getContext().getLangOpts().C11)
+ const bool ShouldReport = [this] {
+const bool IsAnnexKAvailable = analysis::isAnnexKAvailable(
+&BR.getPreprocessor(), BR.getContext().getLangOpts());
+const bool IsC11OrLaterStandard = BR.getContext().getLangOpts().C11;
steakhal wrote:
Do we cache this, or its gonna trigger every time we call
`checkDeprecatedOrUnsafeBufferHandling`?
Would that happen only once?
https://github.com/llvm/llvm-project/pull/168704
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang][analyzer] Add ReportInC99AndEarlier option to DeprecatedOrUnsafeBuf… (PR #168704)
https://github.com/steakhal commented: I like where this is going. Thank you for the detection logic. It looks solid and matches my understanding of Annex K. https://github.com/llvm/llvm-project/pull/168704 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang][analyzer] Add ReportInC99AndEarlier option to DeprecatedOrUnsafeBuf… (PR #168704)
@@ -0,0 +1,61 @@
+//==- AnnexKDetection.h - Annex K availability detection *- C++ -*-==//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// This file provides utilities for detecting C11 Annex K (Bounds-checking
+// interfaces) availability.
+//
+//===--===//
+
+#ifndef LLVM_CLANG_ANALYSIS_ANNEXKDETECTION_H
+#define LLVM_CLANG_ANALYSIS_ANNEXKDETECTION_H
+
+#include "clang/Basic/LangOptions.h"
+#include "clang/Lex/Preprocessor.h"
+#include "llvm/ADT/StringRef.h"
+
+namespace clang {
+namespace analysis {
+
+/// Calculates whether Annex K is available for the current translation unit
+/// based on the macro definitions and the language options.
+///
+/// Annex K (Bounds-checking interfaces) is available when:
+/// 1. C11 standard is enabled
+/// 2. __STDC_LIB_EXT1__ macro is defined (indicates library support)
+/// 3. __STDC_WANT_LIB_EXT1__ macro is defined and equals "1" (indicates user
+///opt-in)
+///
+/// \param PP The preprocessor instance to check macro definitions.
+/// \param LO The language options to check C11 standard.
+/// \returns true if Annex K is available, false otherwise.
+inline bool isAnnexKAvailable(Preprocessor *PP, const LangOptions &LO) {
steakhal wrote:
Can we move this into a dedicated CPP file?
https://github.com/llvm/llvm-project/pull/168704
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang][analyzer] Add ReportInC99AndEarlier option to DeprecatedOrUnsafeBuf… (PR #168704)
gamesh411 wrote: I have implemented part of the RFC discussion relevant to this checker according to the RFC discussion (https://discourse.llvm.org/t/rfc-report-modes-for-unsafe-function-reporting/88971) What do you think @steakhal? https://github.com/llvm/llvm-project/pull/168704 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang][analyzer] Add ReportInC99AndEarlier option to DeprecatedOrUnsafeBuf… (PR #168704)
https://github.com/gamesh411 updated
https://github.com/llvm/llvm-project/pull/168704
From dae8506bc6eafa1fcc10e03e3bf7678c17a0babe Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Endre=20F=C3=BCl=C3=B6p?=
Date: Wed, 19 Nov 2025 10:47:40 +0100
Subject: [PATCH 1/9] [clang][analyzer] Add ReportInC99AndEarlier option to
DeprecatedOrUnsafeBufferHandling checker
The checker may report warnings for deprecated buffer handling functions
(memcpy, memset, memmove, etc.) even when not compiling with C11 standard
if the new option "ReportInC99AndEarlier" is set to true.
These functions became deprecated in C11, but may still be problematic in
earlier C standards.
---
clang/docs/analyzer/checkers.rst | 7 +++
.../clang/StaticAnalyzer/Checkers/Checkers.td | 21 +---
.../Checkers/CheckSecuritySyntaxOnly.cpp | 22 -
.../Analysis/Inputs/system-header-simulator.h | 1 +
clang/test/Analysis/analyzer-config.c | 1 +
...ecated-buffer-handling-allow-without-c11.c | 48 +++
6 files changed, 92 insertions(+), 8 deletions(-)
create mode 100644
clang/test/Analysis/security-deprecated-buffer-handling-allow-without-c11.c
diff --git a/clang/docs/analyzer/checkers.rst b/clang/docs/analyzer/checkers.rst
index 31edf9e99dc7d..d93cfe5806ebb 100644
--- a/clang/docs/analyzer/checkers.rst
+++ b/clang/docs/analyzer/checkers.rst
@@ -1785,6 +1785,13 @@ security.insecureAPI.DeprecatedOrUnsafeBufferHandling (C)
strncpy(buf, "a", 1); // warn
}
+The ``AllowWithoutC11`` option allows reporting warnings for these functions
even when not compiling with C11 standard. These functions are deprecated in
C11, but may still be problematic in earlier C standards.
+
+To enable this option, use:
+``-analyzer-config
security.insecureAPI.DeprecatedOrUnsafeBufferHandling:AllowWithoutC11=true``.
+
+By default, this option is set to *false*.
+
.. _security-MmapWriteExec:
security.MmapWriteExec (C)
diff --git a/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
b/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
index ffae3b9310979..310dac5340a18 100644
--- a/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
+++ b/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
@@ -901,12 +901,21 @@ def UncheckedReturn : Checker<"UncheckedReturn">,
Dependencies<[SecuritySyntaxChecker]>,
Documentation;
-def DeprecatedOrUnsafeBufferHandling :
- Checker<"DeprecatedOrUnsafeBufferHandling">,
- HelpText<"Warn on uses of unsecure or deprecated buffer manipulating "
- "functions">,
- Dependencies<[SecuritySyntaxChecker]>,
- Documentation;
+def DeprecatedOrUnsafeBufferHandling
+: Checker<"DeprecatedOrUnsafeBufferHandling">,
+ HelpText<"Warn on uses of unsecure or deprecated buffer manipulating "
+ "functions">,
+ Dependencies<[SecuritySyntaxChecker]>,
+ CheckerOptions<
+ [CmdLineOption<
+ Boolean, "AllowWithoutC11",
+ "Allow reporting deprecated or unsafe buffer handling "
+ "functions even when not compiling with C11 standard. "
+ "These functions are deprecated in C11, but may still be "
+ "problematic in earlier C standards.",
+ "false", Released>,
+]>,
+ Documentation;
def decodeValueOfObjCType : Checker<"decodeValueOfObjCType">,
HelpText<"Warn on uses of the '-decodeValueOfObjCType:at:' method">,
diff --git a/clang/lib/StaticAnalyzer/Checkers/CheckSecuritySyntaxOnly.cpp
b/clang/lib/StaticAnalyzer/Checkers/CheckSecuritySyntaxOnly.cpp
index 5e75c1c4a3abd..e07c9dcbad9fe 100644
--- a/clang/lib/StaticAnalyzer/Checkers/CheckSecuritySyntaxOnly.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/CheckSecuritySyntaxOnly.cpp
@@ -50,6 +50,8 @@ struct ChecksFilter {
bool check_UncheckedReturn = false;
bool check_decodeValueOfObjCType = false;
+ bool allowDeprecatedOrUnsafeBufferHandlingWithoutC11 = false;
+
CheckerNameRef checkName_bcmp;
CheckerNameRef checkName_bcopy;
CheckerNameRef checkName_bzero;
@@ -754,7 +756,8 @@ void WalkAST::checkDeprecatedOrUnsafeBufferHandling(const
CallExpr *CE,
if (!filter.check_DeprecatedOrUnsafeBufferHandling)
return;
- if (!BR.getContext().getLangOpts().C11)
+ if (!(BR.getContext().getLangOpts().C11 ||
+filter.allowDeprecatedOrUnsafeBufferHandlingWithoutC11))
return;
// Issue a warning. ArgIndex == -1: Deprecated but not unsafe (has size
@@ -1113,5 +1116,20 @@ REGISTER_CHECKER(rand)
REGISTER_CHECKER(vfork)
REGISTER_CHECKER(FloatLoopCounter)
REGISTER_CHECKER(UncheckedReturn)
-REGISTER_CHECKER(DeprecatedOrUnsafeBufferHandling)
+
+void ento::registerDeprecatedOrUnsafeBufferHandling(CheckerManager &mgr) {
+ SecuritySyntaxChecker *checker = mgr.getChecker();
+ checker->filter.check_DeprecatedOrUnsafeBufferHandling = true;
+ checker->filter.checkName_DeprecatedOrUnsafeBufferHandling =
+ mgr.getCurrentCheckerName();
+ checker->filter.allowDeprecatedOrUnsafeBufferHa
