[PATCH] regmap: guard DEFINE_FREE(regmap_field) against ERR_PTR values
From: Elkin Cruz
Date: Fri Nov 20 2026 - 14:05:05 EST
regmap_field_alloc() returns ERR_PTR() on allocation failure, never NULL.
DEFINE_FREE(regmap_field, ...) currently checks only if (_T), which is
true for ERR_PTR() values and causes __free(regmap_field) variables on
error paths to pass an error pointer into regmap_field_free() -> kfree().
Guard the cleanup macro with !IS_ERR_OR_NULL(_T).
Fixes: 37983fad7f3e ("regmap: define cleanup helper for regmap_field")
Signed-off-by: Elkin Cruz <elkin@xxxxxxxxxx>
---
I am trying out a new Clang static analysis linter for ERR_PTR/NULL
handling, and it flagged this code as possibly buggy. The findings were triaged
and fixed with an AI coding assistant, then checked against linux-next (not
runtime-tested on hardware).
Thanks,
Elkin
include/linux/regmap.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/include/linux/regmap.h b/include/linux/regmap.h
index 8f3034881f8a..572dd6f0083d 100644
--- a/include/linux/regmap.h
+++ b/include/linux/regmap.h
@@ -1484,7 +1484,7 @@ struct regmap_field *regmap_field_alloc(struct regmap *regmap,
struct reg_field reg_field);
void regmap_field_free(struct regmap_field *field);
-DEFINE_FREE(regmap_field, struct regmap_field *, if (_T) regmap_field_free(_T))
+DEFINE_FREE(regmap_field, struct regmap_field *, if (!IS_ERR_OR_NULL(_T)) regmap_field_free(_T))
struct regmap_field *devm_regmap_field_alloc(struct device *dev,
struct regmap *regmap, struct reg_field reg_field);
--
2.56.1.750.gf70dc682cf-goog