[clang] [-Wunsafe-buffer-usage] Improve null-termination analysis on conditionals (PR #176262)
Ziqing Luo via cfe-commits
cfe-commits at lists.llvm.org
Fri Jan 16 14:13:51 PST 2026
================
@@ -29,6 +29,20 @@ void f(int x, int y) {
return;
}
+// Test nested conditional expressions:
+void testNested(char * message) {
+ fprintf(stderr, "AssertMacros: %s", (0!=0) ? message : ((0!=0) ? message : ""));
+}
+
+// If the conditional cannot be constant-folded, try analyze both branches:
+void testConditionalAnalysis(char * message, int x) {
+ fprintf(stderr, "AssertMacros: %s", (x!=0) ? "hello" : "world");
+ fprintf(stderr, "AssertMacros: %s", (0!=0) ? message : ((x!=0) ? "hello" : "world"));
+ fprintf(stderr, "AssertMacros: %s", (x!=0) ? (((x!=0) ? "hello" : "world")) : ((x!=0) ? "hello" : "world"));
+ fprintf(stderr, "AssertMacros: %s", (x!=0) ? (((x!=0) ? "hello" : "world")) : ((x!=0) ? "hello" : message)); //\
+ cxx-warning{{function 'fprintf' is unsafe}} cxx-note{{string argument is not guaranteed to be null-terminated}}
----------------
ziqingluo-90 wrote:
> Why is this raised only for C++ mode? I couldn't see any hints for that restriction.
This is a part of warning about unsafe libc functions under the C++ Safe Buffers project. It does not make much sense to emit these warnings on C programs as they don't have safe alternatives.
https://github.com/llvm/llvm-project/pull/176262
More information about the cfe-commits
mailing list