[clang] [Clang][Sema] Avoid unsigned overflow in sentinel call checks (PR #245549)

via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 24 01:17:15 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: woruyu (woruyu)

<details>
<summary>Changes</summary>

This fixes https://github.com/llvm/llvm-project/issues/245437. Adding the sentinel offset to the fixed parameter count can wrap for large offsets, allowing an out-of-bounds access to the argument list.

Check that the number of remaining arguments exceeds the offset before looking up the sentinel. This preserves the existing warning when there are too few variadic arguments.

---
Full diff: https://github.com/llvm/llvm-project/pull/245549.diff


2 Files Affected:

- (modified) clang/lib/Sema/SemaExpr.cpp (+5-4) 
- (modified) clang/test/Sema/attr-sentinel.c (+13) 


``````````diff
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index d3deecb3ee74c6..27a64261fe9e12 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -473,16 +473,17 @@ void Sema::DiagnoseSentinelCalls(const NamedDecl *D, SourceLocation Loc,
   // The number of arguments which should follow the sentinel.
   unsigned NumArgsAfterSentinel = Attr->getSentinel();
 
-  // If there aren't enough arguments for all the formal parameters,
-  // the sentinel, and the args after the sentinel, complain.
-  if (Args.size() < NumFormalParams + NumArgsAfterSentinel + 1) {
+  // The variadic arguments must include the sentinel and everything after it.
+  // Subtract the formal count since adding the sentinel offset can overflow.
+  if (Args.size() <= NumFormalParams ||
+      Args.size() - NumFormalParams <= NumArgsAfterSentinel) {
     Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName();
     Diag(D->getLocation(), diag::note_sentinel_here) << int(CalleeKind);
     return;
   }
 
   // Otherwise, find the sentinel expression.
-  const Expr *SentinelExpr = Args[Args.size() - NumArgsAfterSentinel - 1];
+  const Expr *SentinelExpr = Args.drop_back(NumArgsAfterSentinel).back();
   if (!SentinelExpr)
     return;
   if (SentinelExpr->isValueDependent())
diff --git a/clang/test/Sema/attr-sentinel.c b/clang/test/Sema/attr-sentinel.c
index 9af11296b07f60..0597bf6cd3d298 100644
--- a/clang/test/Sema/attr-sentinel.c
+++ b/clang/test/Sema/attr-sentinel.c
@@ -40,6 +40,7 @@ void test1(void) {
 
 
 void (*e) (int arg, const char * format, ...) __attribute__ ((__sentinel__ (1,1)));
+// expected-note at -1 {{function has been explicitly marked sentinel here}}
 
 void test2(void) {
   void (*b) (int arg, const char * format, ...) __attribute__ ((__sentinel__));  // expected-note {{function has been explicitly marked sentinel here}}
@@ -56,4 +57,16 @@ void test2(void) {
   y(1, "%s", 1,2,3,4,5,6,7);  // expected-warning {{missing sentinel in function call}}
   
   y(1, "%s", (void*)0,3,4,5,6,7); // OK
+
+  // The last named argument can be the sentinel if one argument follows it.
+  e(1, NULL);
+  // expected-warning at -1 {{not enough variable arguments in 'e' declaration to fit a sentinel}}
+  e(1, NULL, 2); // OK
+}
+
+void test_large_sentinel(void) {
+  void f(int, ...) __attribute__((sentinel(~0U)));
+  // expected-note at -1 {{function has been explicitly marked sentinel here}}
+  f(1, 2);
+  // expected-warning at -1 {{not enough variable arguments in 'f' declaration to fit a sentinel}}
 }

``````````

</details>


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


More information about the cfe-commits mailing list