diff mbox series

[i2c] tiny-usb: Add error handling in i2c_tiny_usb_probe

Message ID 20231203084929.38168-1-liuhaoran14@163.com
State Changes Requested
Delegated to: Andi Shyti
Headers show
Series [i2c] tiny-usb: Add error handling in i2c_tiny_usb_probe | expand

Commit Message

Haoran Liu Dec. 3, 2023, 8:49 a.m. UTC
This patch adds error handling for the i2c_add_adapter. The need for this
error handling was identified through static analysis, which revealed that
the function did not properly address potential failures of
i2c_add_adapter. Previously, a failure in this call could lead to an
incomplete initialization of the I2C adapter, causing unpredictable
behavior.

Although the error addressed by this patch may not occur in the current
environment, I still suggest implementing these error handling routines
if the function is not highly time-sensitive. As the environment evolves
or the code gets reused in different contexts, there's a possibility that
these errors might occur. In case you find this addition unnecessary, I
completely understand and respect your perspective. My intention was to
enhance the robustness of the code, but I acknowledge that practical
considerations and current functionality might not warrant this change
at this point.

Signed-off-by: Haoran Liu <liuhaoran14@163.com>
---
 drivers/i2c/busses/i2c-tiny-usb.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/drivers/i2c/busses/i2c-tiny-usb.c b/drivers/i2c/busses/i2c-tiny-usb.c
index 1bffe36c40ad..f165e20fea53 100644
--- a/drivers/i2c/busses/i2c-tiny-usb.c
+++ b/drivers/i2c/busses/i2c-tiny-usb.c
@@ -264,7 +264,12 @@  static int i2c_tiny_usb_probe(struct usb_interface *interface,
 	dev->adapter.dev.parent = &dev->interface->dev;
 
 	/* and finally attach to i2c layer */
-	i2c_add_adapter(&dev->adapter);
+	retval = i2c_add_adapter(&dev->adapter);
+	if (retval) {
+		dev_err(&interface->dev, "i2c_add_adapter failed: %d\n",
+			retval);
+		goto error;
+	}
 
 	/* inform user about successful attachment to i2c layer */
 	dev_info(&dev->adapter.dev, "connected i2c-tiny-usb device\n");