i2c: cp2615: handle allocation failure
Subsystems (3)
PATCH v3 1/1 i2c: cp2615: handle allocation failure
Check the result of kzalloc_obj() and return -ENOMEM when the allocation fails instead of passing a NULL pointer to the message initialization helpers, which would return -EINVAL.
Also separate kzalloc_obj() calls from variable declarations.
---
Changes in v2:
- Clarify the commit message to describe the actual behavior change.
- Fix coding style regression
Changes in v3:
- Separate kzalloc_obj() calls from variable declarations.
--- drivers/i2c/busses/i2c-cp2615.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-)
drivers/i2c/busses/i2c-cp2615.c +16 -5
@@ -124,10 +124,15 @@ static int cp2615_check_status(enum cp2615_i2c_status status)static int cp2615_i2c_send (struct usb_interface *usbif,struct cp2615_i2c_transfer *i2c_w) { -struct cp2615_iop_msg *msg =kzalloc_obj (*msg);struct usb_device *usbdev =interface_to_usbdev (usbif); -int res =cp2615_init_i2c_msg (msg, i2c_w); +struct cp2615_iop_msg *msg; +int res; + + msg =kzalloc_obj (*msg); +if (!msg) +return -ENOMEM ; + res =cp2615_init_i2c_msg (msg, i2c_w);if (!res) res =usb_bulk_msg (usbdev,usb_sndbulkpipe (usbdev,IOP_EP_OUT ), msg,ntohs (msg->length),NULL ,0 );
@@ -171,11 +176,16 @@ cp2615_i2c_recv(struct usb_interface *usbif, unsigned char tag, void *buf)/* Checks if the IOP is functional by querying the part's ID */ static int cp2615_check_iop (struct usb_interface *usbif) { -struct cp2615_iop_msg *msg =kzalloc_obj (*msg); -struct cp2615_iop_accessory_info *info = (struct cp2615_iop_accessory_info *)&msg->data;struct usb_device *usbdev =interface_to_usbdev (usbif); -int res =cp2615_init_iop_msg (msg, iop_GetAccessoryInfo,NULL ,0 ); +struct cp2615_iop_msg *msg; +struct cp2615_iop_accessory_info *info; +int res; + + msg =kzalloc_obj (*msg); +if (!msg) +return -ENOMEM ; + res =cp2615_init_iop_msg (msg, iop_GetAccessoryInfo,NULL ,0 );if (res) goto out;
@@ -194,6 +204,7 @@ static int cp2615_check_iop(struct usb_interface *usbif) goto out; } + info = (struct cp2615_iop_accessory_info *)&msg->data;switch (ntohs (info->part_id)) {case PART_ID_A01 :dev_dbg (&usbif->dev,"Found A01 part. (WARNING: errata exists!)\n" );
Signature · 1 line
2.53.0
Quoted 5 lines
Check the result of
kzalloc_obj()and return-ENOMEMwhen the allocation fails instead of passing a NULL pointer to the message initialization helpers, which would return-EINVAL.Also separate
kzalloc_obj()calls from variable declarations.
* Can such a source code adjustment relieve further development concerns?
https://cmu-sei.github.io/secure-coding-standards/sei-cert-c-coding-standard/rules/expressions-exp/exp34-c/
* How do you think about to add any tags (like “Fixes” and “Cc”) accordingly?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v7.3-rc1#n198
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/stable-kernel-rules.rst?h=v7.3-rc1#n3
* How do you think about to increase the application of scope-based resource management?
Regards,
Markus