[llvm-branch-commits] [clang] release/23.x: [Clang][Sema] Avoid APInt crash on narrow umask parameter types (#244358) (PR #244681)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri Nov 20 11:03:43 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: llvmbot

<details>
<summary>Changes</summary>

Backport 1d060b88de7caf2cd6ddeebc269d7415aad54bb6

Requested by: @<!-- -->venk-ks

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


2 Files Affected:

- (modified) clang/lib/Sema/SemaChecking.cpp (+5-1) 
- (modified) clang/test/Sema/warn-fortify-source-umask-not-builtin.c (+15-4) 


``````````diff
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 2a8395e26d0bd1..daeb93edc401b4 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -1508,7 +1508,11 @@ void Sema::checkFortifiedLibcArgument(FunctionDecl *FD, CallExpr *TheCall) {
   if (TheCall->getNumArgs() != 1)
     return;
   Expr *Arg = TheCall->getArg(0);
-  if (!Arg->getType()->isIntegerType())
+  // POSIX mode_t is at least 16 bits; bail out if the argument has a narrower
+  // integer type (which is not POSIX umask and would otherwise assert
+  // constructing the 0777 bitmask for widths under 9 bits).
+  if (!Arg->getType()->isIntegerType() ||
+      getASTContext().getIntWidth(Arg->getType()) < 16)
     return;
   Expr::EvalResult R;
   if (!Arg->EvaluateAsInt(R, getASTContext()))
diff --git a/clang/test/Sema/warn-fortify-source-umask-not-builtin.c b/clang/test/Sema/warn-fortify-source-umask-not-builtin.c
index 57e3706a5f1b1e..3d9e962e91ea22 100644
--- a/clang/test/Sema/warn-fortify-source-umask-not-builtin.c
+++ b/clang/test/Sema/warn-fortify-source-umask-not-builtin.c
@@ -1,12 +1,22 @@
 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -x c %s -verify
 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -x c++ %s -verify
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -x c %s -DNARROW_UMASK -verify
 // expected-no-diagnostics
 
-// Declarations that are not the POSIX umask keep a zero builtin id, so the
-// fortify check is skipped even for out-of-range constants:
-//   * a file-local umask with internal linkage;
-//   * in C++, a umask without C language linkage (e.g. a user's own overload).
+// Declarations that are not the POSIX umask are skipped by the fortify check:
+//   * a file-local umask with internal linkage (zero builtin id);
+//   * in C++, a umask without C language linkage (zero builtin id);
+//   * a umask whose integer parameter is narrower than 16 bits (GH244171).
 
+#ifdef NARROW_UMASK
+typedef unsigned char mode_t;
+mode_t umask(mode_t);
+
+void call_narrow_umask(void) {
+  (void)umask(0);
+  (void)umask(0xFF);
+}
+#else
 static int umask(int m) { return m; }
 
 void call_static_umask(void) {
@@ -19,3 +29,4 @@ int umask(int);
 void call(void) { (void)umask(0xFFFF); }
 } // namespace user
 #endif
+#endif

``````````

</details>


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


More information about the llvm-branch-commits mailing list