Skip to contentSkip to conversation

driver core: attribute_container: Unwind device_add() on attr failure

PATCH New Yuho Choi 1 participant 1 message Jun 8 Driver core

lore
Share & tools
lore

Yuho Choi

author
#0 lore raw

PATCH 1/1 driver core: attribute_container: Unwind device_add() on attr failure

attribute_container_add_class_device() first calls device_add() and then creates the container attributes. If attribute_container_add_attrs() fails, the helper returns the error with the class device still registered.

Callers use an error from this helper as a failed add and do not call device_del() on that path. Undo the successful device_add() before returning the attribute creation error.

---
 drivers/base/attribute_container.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/base/attribute_container.c +6 -1
index 4ad26b8dd6a5..3c456374247b 100644
@@ -425,7 +425,12 @@ attribute_container_add_class_device(struct device *classdev)
 
 	if (error)
 		return error;
-	return attribute_container_add_attrs(classdev);
+
+	error = attribute_container_add_attrs(classdev);
+	if (error)
+		device_del(classdev);
+
+	return error;
 }
 
 /**
Signature · 1 line
2.43.0