From: Wander Lairson Costa <wander@redhat.com>
To: Brendan Higgins <brendan.higgins@linux.dev>,
David Gow <davidgow@google.com>, Rae Moar <rmoar@google.com>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Shuah Khan <skhan@linuxfoundation.org>,
Maxime Ripard <mripard@kernel.org>,
Matti Vaittinen <mazziesaccount@gmail.com>,
linux-kselftest@vger.kernel.org (open list:KERNEL UNIT TESTING
FRAMEWORK (KUnit)),
kunit-dev@googlegroups.com (open list:KERNEL UNIT TESTING
FRAMEWORK (KUnit)), linux-kernel@vger.kernel.org (open list)
Cc: Wander Lairson Costa <wander@redhat.com>
Subject: [PATCH v3 2/2] kunit: avoid memory leak on device register error
Date: Thu, 18 Apr 2024 18:02:35 -0300 [thread overview]
Message-ID: <20240418210236.194190-3-wander@redhat.com> (raw)
In-Reply-To: <20240418210236.194190-1-wander@redhat.com>
If the device register fails, free the allocated memory before
returning.
Signed-off-by: Wander Lairson Costa <wander@redhat.com>
Fixes: d03c720e03bd ("kunit: Add APIs for managing devices")
---
lib/kunit/device.c | 11 ++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)
diff --git a/lib/kunit/device.c b/lib/kunit/device.c
index 25c81ed465fb..bc2e2032e505 100644
--- a/lib/kunit/device.c
+++ b/lib/kunit/device.c
@@ -119,10 +119,8 @@ static struct kunit_device *kunit_device_register_internal(struct kunit *test,
kunit_dev->owner = test;
err = dev_set_name(&kunit_dev->dev, "%s.%s", test->name, name);
- if (err) {
- kfree(kunit_dev);
- return ERR_PTR(err);
- }
+ if (err)
+ goto error;
kunit_dev->dev.release = kunit_device_release;
kunit_dev->dev.bus = &kunit_bus_type;
@@ -131,7 +129,7 @@ static struct kunit_device *kunit_device_register_internal(struct kunit *test,
err = device_register(&kunit_dev->dev);
if (err) {
put_device(&kunit_dev->dev);
- return ERR_PTR(err);
+ goto error;
}
kunit_dev->dev.dma_mask = &kunit_dev->dev.coherent_dma_mask;
@@ -140,6 +138,9 @@ static struct kunit_device *kunit_device_register_internal(struct kunit *test,
kunit_add_action(test, device_unregister_wrapper, &kunit_dev->dev);
return kunit_dev;
+error:
+ kfree(kunit_dev);
+ return ERR_PTR(err);
}
/*
--
2.44.0
next prev parent reply other threads:[~2024-04-18 21:02 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-04-18 21:02 [PATCH v3 0/2] kunit: fix minor error path mistakes Wander Lairson Costa
2024-04-18 21:02 ` [PATCH v3 1/2] kunit: unregister the device on error Wander Lairson Costa
2024-04-19 4:58 ` David Gow
2024-04-19 5:40 ` Markus Elfring
2024-04-19 6:07 ` Greg Kroah-Hartman
2024-04-18 21:02 ` Wander Lairson Costa [this message]
2024-04-19 4:58 ` [PATCH v3 2/2] kunit: avoid memory leak on device register error David Gow
2024-04-19 12:30 ` Wander Lairson Costa
2024-04-19 13:59 ` Greg Kroah-Hartman
2024-04-19 6:15 ` Markus Elfring
2024-04-19 6:33 ` Greg Kroah-Hartman
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240418210236.194190-3-wander@redhat.com \
--to=wander@redhat.com \
--cc=brendan.higgins@linux.dev \
--cc=davidgow@google.com \
--cc=gregkh@linuxfoundation.org \
--cc=kunit-dev@googlegroups.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=mazziesaccount@gmail.com \
--cc=mripard@kernel.org \
--cc=rmoar@google.com \
--cc=skhan@linuxfoundation.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.