drivers/s390/char/raw3270.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-)
Check the return values of kzalloc_obj() and kzalloc() before using the
allocated memory. Free the allocated resources on subsequent initialization
failures to avoid leaking memory.
Signed-off-by: Triet Hoang <triet.hoang.dev@gmail.com>
---
Changes in v2:
- Destroy the console device on allocation failure.
- Do not free rp after raw3270_setup_device() success.
---
drivers/s390/char/raw3270.c | 15 ++++++++++++++-
1 file changed, 14 insertions(+), 1 deletion(-)
diff --git a/drivers/s390/char/raw3270.c b/drivers/s390/char/raw3270.c
index aa9c4d81225c..ff397396b440 100644
--- a/drivers/s390/char/raw3270.c
+++ b/drivers/s390/char/raw3270.c
@@ -814,10 +814,23 @@ struct raw3270 __init *raw3270_setup_console(void)
return ERR_CAST(cdev);
rp = kzalloc_obj(*rp, GFP_KERNEL | GFP_DMA);
+ if (!rp) {
+ ccw_device_destroy_console(cdev);
+ return ERR_PTR(-ENOMEM);
+ }
ascebc = kzalloc(256, GFP_KERNEL);
+ if (!ascebc) {
+ ccw_device_destroy_console(cdev);
+ kfree(rp);
+ return ERR_PTR(-ENOMEM);
+ }
rc = raw3270_setup_device(cdev, rp, ascebc);
- if (rc)
+ if (rc) {
+ ccw_device_destroy_console(cdev);
+ kfree(rp->ascebc);
+ kfree(rp);
return ERR_PTR(rc);
+ }
set_bit(RAW3270_FLAGS_CONSOLE, &rp->flags);
rc = ccw_device_enable_console(cdev);
--
2.53.0
> Check the return values of kzalloc_obj() and kzalloc() before using the > allocated memory. Free the allocated resources on subsequent initialization > failures to avoid leaking memory. * 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.2#n145 https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/stable-kernel-rules.rst?h=v7.2#n34 * Please avoid duplicate source code also for an improved implementation of the function “raw3270_setup_console”. https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?h=v7.2#n526 https://elixir.bootlin.com/linux/v7.2/source/drivers/s390/char/raw3270.c#L802-L839 * How do you think about to increase the application of scope-based resource management? Regards, Markus
© 2016 - 2026 Red Hat, Inc.