[llvm] [SPIR-V] Diagnose out-of-bounds argument index in function type metadata (PR #200601)

Juan Manuel Martinez CaamaƱo via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 3 02:42:32 PDT 2026


================
@@ -66,13 +66,15 @@ static FunctionType *extractFunctionTypeFromMetadata(NamedMDNode *NMD,
     if (auto *Const = getConstInt(MD, 0)) {
       auto *CMeta = dyn_cast<ConstantAsMetadata>(MD->getOperand(1));
       assert(CMeta && "ConstantAsMetadata operand is expected");
-      assert(Const->getSExtValue() >= -1);
+      int64_t Idx = Const->getSExtValue();
       // Currently -1 indicates return value, greater values mean
       // argument numbers.
-      if (Const->getSExtValue() == -1)
+      if (Idx < -1 || (Idx >= 0 && static_cast<uint64_t>(Idx) >= PTys.size()))
----------------
jmmartinez wrote:

Why not just `Idx < -1 || Ids >= PTys.size()` ? (There may be an edge-case I'm not seeing).

Maybe the conditions would be easier to reason about if we cover all the "well formed" cases and fatal-error on the fallthrough case.

Something like:

```cpp
if(Idx == -1) {
  RetTy = CMeta->getType();
  continue;
}

if(Idx >= 0 && Idx < PTys.size()) {
  PTys[Idx] = CMeta->getType();
  continue;
}
 
report_fatal_error("invalid argument index in function type metadata");
```

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


More information about the llvm-commits mailing list