Skip to contentSkip to conversation

regmap: guard DEFINE_FREE(regmap_field) against ERR_PTR values

PATCH New Elkin Cruz 1 participant 1 message Nov 20 Register map abstraction

lore
Share & tools
lore

Elkin Cruz

author
#0 lore raw

PATCH 1/1 regmap: guard DEFINE_FREE(regmap_field) against ERR_PTR values

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")

---
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(-)
include/linux/regmap.h +1 -1
index 8f3034881f8a..572dd6f0083d 100644
@@ -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);
Signature · 1 line
2.56.1.750.gf70dc682cf-goog