[PATCH] KVM: selftests: Add 'malloc' failure check in config_name

Kunwu Chan posted 1 patch 1 week, 4 days ago
tools/testing/selftests/kvm/get-reg-list.c | 1 +
1 file changed, 1 insertion(+)
[PATCH] KVM: selftests: Add 'malloc' failure check in config_name
Posted by Kunwu Chan 1 week, 4 days ago
There is a 'malloc' call in config_name function, which can
be unsuccessful. This patch will add the malloc failure checking
to avoid possible null dereference and give more information
about test fail reasons.

Signed-off-by: Kunwu Chan <chentao@kylinos.cn>
---
 tools/testing/selftests/kvm/get-reg-list.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/tools/testing/selftests/kvm/get-reg-list.c b/tools/testing/selftests/kvm/get-reg-list.c
index 91f05f78e824..22398696ffd6 100644
--- a/tools/testing/selftests/kvm/get-reg-list.c
+++ b/tools/testing/selftests/kvm/get-reg-list.c
@@ -66,6 +66,7 @@ static const char *config_name(struct vcpu_reg_list *c)
 		len += strlen(s->name) + 1;
 
 	c->name = malloc(len);
+	TEST_ASSERT(c->name, "-ENOMEM when allocating config name");
 
 	len = 0;
 	for_each_sublist(c, s) {
-- 
2.40.1
Re: [PATCH] KVM: selftests: Add 'malloc' failure check in config_name
Posted by Markus Elfring 1 week, 4 days ago
…
> This patch will add the malloc failure checking
…

* Please use a corresponding imperative wording for the change description.

* Would you like to add the tag “Fixes” accordingly?


…
> +++ b/tools/testing/selftests/kvm/get-reg-list.c
> @@ -66,6 +66,7 @@ static const char *config_name(struct vcpu_reg_list *c)
>  		len += strlen(s->name) + 1;
>
>  	c->name = malloc(len);
> +	TEST_ASSERT(c->name, "-ENOMEM when allocating config name");
…

Can “errno” be relevant for the error message construction?

Regards,
Markus
Re: [PATCH] KVM: selftests: Add 'malloc' failure check in config_name
Posted by Muhammad Usama Anjum 1 week, 4 days ago
On 4/23/24 12:37 PM, Kunwu Chan wrote:
> There is a 'malloc' call in config_name function, which can
> be unsuccessful. This patch will add the malloc failure checking
> to avoid possible null dereference and give more information
> about test fail reasons.
> 
> Signed-off-by: Kunwu Chan <chentao@kylinos.cn>
LGTM

Reviewed-by: Muhammad Usama Anjum <usama.anjum@collabora.com>

> ---
>  tools/testing/selftests/kvm/get-reg-list.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/tools/testing/selftests/kvm/get-reg-list.c b/tools/testing/selftests/kvm/get-reg-list.c
> index 91f05f78e824..22398696ffd6 100644
> --- a/tools/testing/selftests/kvm/get-reg-list.c
> +++ b/tools/testing/selftests/kvm/get-reg-list.c
> @@ -66,6 +66,7 @@ static const char *config_name(struct vcpu_reg_list *c)
>  		len += strlen(s->name) + 1;
>  
>  	c->name = malloc(len);
> +	TEST_ASSERT(c->name, "-ENOMEM when allocating config name");
>  
>  	len = 0;
>  	for_each_sublist(c, s) {

-- 
BR,
Muhammad Usama Anjum