[llvm] Initialize function_ref::callable (PR #216686)

Sylvestre Ledru via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 17 03:28:40 PDT 2026


https://github.com/sylvestre created https://github.com/llvm/llvm-project/pull/216686

A default-constructed function_ref initialized callback but left the callable field indeterminate. The value is then copied whenever such an empty function_ref is copied or moved (e.g. OperationState's propertiesDeleter/propertiesSetter members in every generated MLIR Op::create method), and operator== reads it, which is undefined behavior for empty refs.

Coverity reports this as 'Uninitialized scalar variable' at every use site: 7,201 outstanding findings in the LLVM Coverity project, 6,436 of them in tblgen-generated Op::create methods. Initializing the field fixes the entire class at the root for one store.

>From f8a0eba0eb79068cf402c45ef2d1e08c3c6addc1 Mon Sep 17 00:00:00 2001
From: Sylvestre Ledru <sylvestre at debian.org>
Date: Mon, 10 Aug 2026 09:23:11 +0200
Subject: [PATCH] Initialize function_ref::callable

A default-constructed function_ref initialized callback but left the
callable field indeterminate. The value is then copied whenever such an
empty function_ref is copied or moved (e.g. OperationState's
propertiesDeleter/propertiesSetter members in every generated MLIR
Op::create method), and operator== reads it, which is undefined
behavior for empty refs.

Coverity reports this as 'Uninitialized scalar variable' at every use
site: 7,201 outstanding findings in the LLVM Coverity project, 6,436 of
them in tblgen-generated Op::create methods. Initializing the field
fixes the entire class at the root for one store.
---
 llvm/include/llvm/ADT/STLFunctionalExtras.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/include/llvm/ADT/STLFunctionalExtras.h b/llvm/include/llvm/ADT/STLFunctionalExtras.h
index a4d50dc3648be..f6dbd8b1840ac 100644
--- a/llvm/include/llvm/ADT/STLFunctionalExtras.h
+++ b/llvm/include/llvm/ADT/STLFunctionalExtras.h
@@ -39,7 +39,7 @@ template<typename Fn> class function_ref;
 template <typename Ret, typename... Params>
 class LLVM_GSL_POINTER function_ref<Ret(Params...)> {
   Ret (*callback)(intptr_t callable, Params ...params) = nullptr;
-  intptr_t callable;
+  intptr_t callable = 0;
 
   template<typename Callable>
   static Ret callback_fn(intptr_t callable, Params ...params) {



More information about the llvm-commits mailing list