From 3fcc4f28ed808d72cb3c6bc45f9bca891ae5ca48 Mon Sep 17 00:00:00 2001 From: Yeting Kuo <46629943+yetingk@users.noreply.github.com> Date: Tue, 30 Jul 2024 13:24:14 +0800 Subject: [PATCH] [clang][CodeGen] Don't crash on output whose size is zero. (#99849) This fixes issue #63878 caused by creating an integer with zero bitwidth. --- clang/lib/CodeGen/CGStmt.cpp | 5 ++++- clang/test/CodeGen/inline-asm-size-zero.c | 6 ++++++ 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 clang/test/CodeGen/inline-asm-size-zero.c diff --git a/clang/lib/CodeGen/CGStmt.cpp b/clang/lib/CodeGen/CGStmt.cpp index aa97f685ac7a9f..e16aa3cdd5506c 100644 --- a/clang/lib/CodeGen/CGStmt.cpp +++ b/clang/lib/CodeGen/CGStmt.cpp @@ -2751,7 +2751,10 @@ void CodeGenFunction::EmitAsmStmt(const AsmStmt &S) { if (RequiresCast) { unsigned Size = getContext().getTypeSize(QTy); - Ty = llvm::IntegerType::get(getLLVMContext(), Size); + if (Size) + Ty = llvm::IntegerType::get(getLLVMContext(), Size); + else + CGM.Error(OutExpr->getExprLoc(), "output size should not be zero"); } ResultRegTypes.push_back(Ty); // If this output is tied to an input, and if the input is larger, then diff --git a/clang/test/CodeGen/inline-asm-size-zero.c b/clang/test/CodeGen/inline-asm-size-zero.c new file mode 100644 index 00000000000000..564f5207d1e711 --- /dev/null +++ b/clang/test/CodeGen/inline-asm-size-zero.c @@ -0,0 +1,6 @@ +// RUN: not %clang_cc1 -S %s -verify -o - + +void foo(void) { + extern long bar[]; + asm ("" : "=r"(bar)); // expected-error{{output size should not be zero}} +}