[clang] [Clang] Avoid crash with wide string CPU builtin argument (PR #245515)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 23 23:07:29 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Bhavya Gautam (gautam899)
<details>
<summary>Changes</summary>
This fixes #<!-- -->244868 by checking the character width before calling
getstring.
To validate the fix, I ran clang-check and found no new fails.
---
Full diff: https://github.com/llvm/llvm-project/pull/245515.diff
2 Files Affected:
- (modified) clang/lib/Sema/SemaChecking.cpp (+8-2)
- (modified) clang/test/Sema/builtin-cpu-supports.c (+2)
``````````diff
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index ccb303624182f..8368ee0e21092 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -2572,13 +2572,19 @@ static bool BuiltinCpu(Sema &S, const TargetInfo &TI, CallExpr *TheCall,
<< SourceRange(TheCall->getBeginLoc(), TheCall->getEndLoc());
Expr *Arg = TheCall->getArg(0)->IgnoreParenImpCasts();
+
+ StringLiteral *Literal = dyn_cast<StringLiteral>(Arg);
// Check if the argument is a string literal.
- if (!isa<StringLiteral>(Arg))
+ if (!Literal)
return S.Diag(TheCall->getBeginLoc(), diag::err_expr_not_string_literal)
<< Arg->getSourceRange();
+ // Validate the Character Width
+ if (Literal->getCharByteWidth() != 1)
+ return false;
+
// Check the contents of the string.
- StringRef Feature = cast<StringLiteral>(Arg)->getString();
+ StringRef Feature = Literal->getString();
if (IsCPUSupports && !TheTI->validateCpuSupports(Feature)) {
S.Diag(TheCall->getBeginLoc(), diag::warn_invalid_cpu_supports)
<< Arg->getSourceRange();
diff --git a/clang/test/Sema/builtin-cpu-supports.c b/clang/test/Sema/builtin-cpu-supports.c
index 4dace25b42a97..4a33f223893f1 100644
--- a/clang/test/Sema/builtin-cpu-supports.c
+++ b/clang/test/Sema/builtin-cpu-supports.c
@@ -18,6 +18,8 @@ int main(void) {
if (__builtin_cpu_supports(str)) // expected-error {{expression is not a string literal}}
a(str);
+ (void)__builtin_cpu_supports(L"wide"); // expected-error {{incompatible pointer types}}
+
if (__builtin_cpu_is("int")) // expected-error {{invalid cpu name for builtin}}
a("intel");
``````````
</details>
https://github.com/llvm/llvm-project/pull/245515
More information about the cfe-commits
mailing list