[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:06 PST 2026


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

Backport 1d060b88de7caf2cd6ddeebc269d7415aad54bb6

Requested by: @venk-ks

>From 4fa3b1a822be53dc29d2f1a9e93069dc65f2e952 Mon Sep 17 00:00:00 2001
From: Venkatesh Srinivasan <156861430+venk-ks at users.noreply.github.com>
Date: Fri, 20 Nov 2026 10:49:56 -0800
Subject: [PATCH] [Clang][Sema] Avoid APInt crash on narrow umask parameter
 types (#244358)

In `Sema::checkFortifiedLibcArgument`, constructing
`llvm::APInt(RawValue.getBitWidth(), 0777)` asserts when `umask` is
declared with an integer parameter narrower than 9 bits (such as `char`,
`bool`, or `_BitInt(8)`), since octal `0777` requires 9 bits.

Bail out alongside the `isIntegerType()` check when
`getASTContext().getIntWidth(Arg->getType()) < 16`, since POSIX `mode_t`
is at least 16 bits and narrower integer types cannot be POSIX `umask`.

Fixes #244171

Assisted-by: Gemini
(cherry picked from commit 1d060b88de7caf2cd6ddeebc269d7415aad54bb6)
---
 clang/lib/Sema/SemaChecking.cpp               |  6 +++++-
 .../warn-fortify-source-umask-not-builtin.c   | 19 +++++++++++++++----
 2 files changed, 20 insertions(+), 5 deletions(-)

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



More information about the llvm-branch-commits mailing list