[clang] [llvm] [analyzer] New checker: optin.core.UnconditionalVAArg (PR #175602)

DonĂ¡t Nagy via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 14 05:47:13 PST 2026


================
@@ -0,0 +1,261 @@
+// RUN: %clang_analyze_cc1 -triple hexagon-unknown-linux -verify %s \
+// RUN:   -analyzer-checker=core,optin.core.UnconditionalVAArg \
+// RUN:   -analyzer-disable-checker=core.CallAndMessage \
+// RUN:   -analyzer-output=text
+//
+// RUN: %clang_analyze_cc1 -triple x86_64-pc-linux-gnu -verify %s \
+// RUN:   -analyzer-checker=core,optin.core.UnconditionalVAArg \
+// RUN:   -analyzer-disable-checker=core.CallAndMessage \
+// RUN:   -analyzer-output=text
+
+#include "Inputs/system-header-simulator-for-valist.h"
+int printf(const char *format, ...);
+void abort(void);
+
+void log_message(const char *msg, ...) {
+  // This artifical but plausible example function expects that the list of
+  // variadic arguments is terminated by a NULL pointer. Although careful use
+  // of this function is well-defined, passing no variadic arguments would
+  // trigger undefined behavior, so it may be better to choose a different way
+  // to mark the count of variadic arguments.
+  va_list va;
+  const char *arg;
+  printf("%s\n", msg);
+  va_start(va, msg);
+  while ((arg = va_arg(va, const char *))) {
+    // expected-warning at -1 {{Unconditional use of va_arg()}}
+    // expected-note at -2 {{Calls to 'log_message' always reach this va_arg() expression, so calling 'log_message' with no variadic arguments would be undefined behavior}}
+    printf(" * %s\n", arg);
+  }
+  va_end(va);
+}
+
+
+void never_called(int fst, ...) {
+  // This simple test function is not called in this test file.
+  va_list va;
+  va_start(va, fst);
+  (void)va_arg(va, int); // expected-warning{{Unconditional use of va_arg()}}
+  // expected-note at -1 {{Calls to 'never_called' always reach this va_arg() expression, so calling 'never_called' with no variadic arguments would be undefined behavior}}
+  va_end(va);
+}
----------------
NagyDonat wrote:

Done in https://github.com/llvm/llvm-project/pull/175602/commits/efdc24c27e382de782a2cb4aea355c2103a7b140

https://github.com/llvm/llvm-project/pull/175602


More information about the cfe-commits mailing list