ti_sci: Address some issues found by Smatch
v3 cover ti_sci: Address some issues found by Smatch
Prevent a memory leak from a non-freed malloc. Return an error code on an error path.
Assign a value to a pointer before it is dereferenced.
---
Changes in v3:
- Pick up review tags
- Change dev_dbg call to dev_err to be consistent with surrounding code in 2/3
- Update subject line of 3/3 to be more explicit
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Add cast when assigning to resp
- Replace malloc with devm_kmalloc
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Andrew Goodbody (3):
ti_sci: Prevent memory leak
ti_sci: Provide error code on error exit
ti_sci: Pointer is never assigned to valid value
drivers/firmware/ti_sci.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)
---
base-commit: c280c4f7e52f08255f491783b3143a3be1fc08b0
change-id: 20250725-ti_sci-7bc55cb0cdba
Best regards,
Signature · 1 line
Andrew Goodbody <[email protected]>
PATCH v3 1/3 ti_sci: Prevent memory leak
temp is assigned the pointer returned by malloc which is used without a NULL check and then never freed. Add a NULL check and ensure temp is freed on all return paths.
This issue was found by Smatch.
--- drivers/firmware/ti_sci.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/firmware/ti_sci.c +5 -1
@@ -3083,7 +3083,10 @@ devm_ti_sci_get_of_resource(const struct ti_sci_handle *handle,dev_err (dev,"%s resource type ids not available\n" , of_prop);return ERR_PTR (sets); } - temp =malloc (sets); + temp =devm_kmalloc (dev, sets,GFP_KERNEL ); +if (!temp) +return ERR_PTR (-ENOMEM ); + sets /=sizeof (u32); res->sets = sets;
@@ -3123,6 +3126,7 @@ devm_ti_sci_get_of_resource(const struct ti_sci_handle *handle,return ERR_PTR (-ENOMEM ); } +devm_kfree (dev, temp);if (valid_set)return res;
Signature · 1 line
2.47.3
PATCH v3 2/3 ti_sci: Provide error code on error exit
In reply to Andrew GoodbodyIn ti_sci_get_response the check for message sequence will return ret on a fail but ret will be 0 at that point. Instead return -EINVAL. Also change dev_dbg call to dev_err to be consistent with other error detection code in the same function.
This issue was found by Smatch.
--- drivers/firmware/ti_sci.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/firmware/ti_sci.c +2 -2
@@ -191,9 +191,9 @@ static int ti_sci_get_response(struct ti_sci_info *info,/* Sanity check for message response */ if (hdr->seq != info->seq) { -dev_dbg (info->dev,"%s: Message for %d is not expected\n" , +dev_err (info->dev,"%s: Message for %d is not expected\n" , __func__, hdr->seq); -return ret; +return -EINVAL ; }if (msg->len > info->desc->max_msg_size) {
Signature · 1 line
2.47.3
PATCH v3 3/3 ti_sci: Pointer is never assigned to valid value
In reply to Andrew GoodbodyThe pointer resp is declared but never assigned a value but is then dereferenced. Fix this by assigning the pointer to the message buffer.
This issue was found by Smatch.
--- drivers/firmware/ti_sci.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/firmware/ti_sci.c +2 -0
@@ -1365,6 +1365,8 @@ static int ti_sci_cmd_clk_get_parent(const struct ti_sci_handle *handle,if (ret)return ret; + resp = (struct ti_sci_msg_resp_get_clock_parent *)xfer->tx_message.buf; + *parent_id = resp->parent_id;return ret;
Signature · 1 line
2.47.3
Quoted 24 lines · Andrew Goodbody
Prevent a memory leak from a non-freed malloc. Return an error code on an error path.
Assign a value to a pointer before it is dereferenced.Signed-off-by Andrew Goodbody <[email protected]>---
Changes in v3:
- Pick up review tags
- Changedev_dbgcall todev_errto be consistent with surrounding code in 2/3
- Update subject line of 3/3 to be more explicit
- Link to v2: https://lore.kernel.org/r/[email protected]Changes in v2:
- Add cast when assigning to resp
- Replace malloc withdevm_kmalloc
- Link to v1: https://lore.kernel.org/r/[email protected]---
Andrew Goodbody (3):
ti_sci: Prevent memory leak
ti_sci: Provide error code on error exit
ti_sci: Pointer is never assigned to valid value
Quoted 7 lines
drivers/firmware/ti_sci.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)---
base-commit:c280c4f7e52f08255f491783b3143a3be1fc08b0
change-id: 20250725-ti_sci-7bc55cb0cdbaBest regards,
For the entire series:
Thanks for the cleanup!
Regards,
Anshul
Quoted 24 lines · Andrew Goodbody
Prevent a memory leak from a non-freed malloc. Return an error code on an error path.
Assign a value to a pointer before it is dereferenced.Signed-off-by Andrew Goodbody <[email protected]>---
Changes in v3:
- Pick up review tags
- Changedev_dbgcall todev_errto be consistent with surrounding code in 2/3
- Update subject line of 3/3 to be more explicit
- Link to v2: https://lore.kernel.org/r/[email protected]Changes in v2:
- Add cast when assigning to resp
- Replace malloc withdevm_kmalloc
- Link to v1: https://lore.kernel.org/r/[email protected]---
Andrew Goodbody (3):
ti_sci: Prevent memory leak
ti_sci: Provide error code on error exit
ti_sci: Pointer is never assigned to valid value
Quoted 10 lines
drivers/firmware/ti_sci.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)---
base-commit:c280c4f7e52f08255f491783b3143a3be1fc08b0
change-id: 20250725-ti_sci-7bc55cb0cdbaBest regards,
Signature · 1 line
Andrew Goodbody <[email protected]>
Also tested with UART boot against u-boot master commit 92dcb3ad5d98 ("cmd/dma: implement dmareset command")
https://gist.github.com/nmenon/1f569681f43cfed456fcce729e0c6e29
Signature · 4 lines
Regards, Nishanth Menon Key (0xDDB5849D1736249D) / Fingerprint: F8A2 8693 54EB 8232 17A3 1A34 DDB5 849D 1736 249D https://ti.com/opensource
Quoted 5 lines · Andrew Goodbody
Prevent a memory leak from a non-freed malloc. Return an error code on an error path.
Assign a value to a pointer before it is dereferenced.
Applied to u-boot/master, thanks!
[1/3] ti_sci: Prevent memory leak
commit: 4b8bc5af80f1637cd71a1b6a0dffa7588a03f27c
[2/3] ti_sci: Provide error code on error exit
commit: 4602fa92202388665ddfbb622f001d62b1473a08
[3/3] ti_sci: Pointer is never assigned to valid value
commit: 5b9125746dfde0a29911ccd542801da6b8f94239Signature · 1 line
Tom