* [PATCH] KVM: selftests: Assert that vasprintf() is successful
@ 2023-09-14 1:06 Sean Christopherson
2023-09-14 8:02 ` Andrew Jones
2023-09-20 16:29 ` Paolo Bonzini
0 siblings, 2 replies; 3+ messages in thread
From: Sean Christopherson @ 2023-09-14 1:06 UTC (permalink / raw)
To: Paolo Bonzini
Cc: kvm, linux-kernel, Andrew Jones, Haibo Xu, Anup Patel,
Sean Christopherson
Assert that vasprintf() succeeds as the "returned" string is undefined
on failure. Checking the result also eliminates the only warning with
default options in KVM selftests, i.e. is the only thing getting in the
way of compile with -Werror.
lib/test_util.c: In function ‘strdup_printf’:
lib/test_util.c:390:9: error: ignoring return value of ‘vasprintf’
declared with attribute ‘warn_unused_result’ [-Werror=unused-result]
390 | vasprintf(&str, fmt, ap);
| ^~~~~~~~~~~~~~~~~~~~~~~~
Don't bother capturing the return value, allegedly vasprintf() can only
fail due to a memory allocation failure.
Fixes: dfaf20af7649 ("KVM: arm64: selftests: Replace str_with_index with strdup_printf")
Cc: Andrew Jones <ajones@ventanamicro.com>
Cc: Haibo Xu <haibo1.xu@intel.com>
Cc: Anup Patel <anup@brainfault.org>
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
I haven't actually run the relevant tests, someone should probably do so on
ARM and/or RISC-V to make sure I didn't do something stupid.
tools/testing/selftests/kvm/lib/test_util.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tools/testing/selftests/kvm/lib/test_util.c b/tools/testing/selftests/kvm/lib/test_util.c
index 3e36019eeb4a..5d7f28b02d73 100644
--- a/tools/testing/selftests/kvm/lib/test_util.c
+++ b/tools/testing/selftests/kvm/lib/test_util.c
@@ -387,7 +387,7 @@ char *strdup_printf(const char *fmt, ...)
char *str;
va_start(ap, fmt);
- vasprintf(&str, fmt, ap);
+ TEST_ASSERT(vasprintf(&str, fmt, ap) >= 0, "vasprintf() failed");
va_end(ap);
return str;
base-commit: 0bb80ecc33a8fb5a682236443c1e740d5c917d1d
--
2.42.0.283.g2d96d420d3-goog
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] KVM: selftests: Assert that vasprintf() is successful
2023-09-14 1:06 [PATCH] KVM: selftests: Assert that vasprintf() is successful Sean Christopherson
@ 2023-09-14 8:02 ` Andrew Jones
2023-09-20 16:29 ` Paolo Bonzini
1 sibling, 0 replies; 3+ messages in thread
From: Andrew Jones @ 2023-09-14 8:02 UTC (permalink / raw)
To: Sean Christopherson
Cc: Paolo Bonzini, kvm, linux-kernel, Haibo Xu, Anup Patel
On Wed, Sep 13, 2023 at 06:06:36PM -0700, Sean Christopherson wrote:
> Assert that vasprintf() succeeds as the "returned" string is undefined
> on failure. Checking the result also eliminates the only warning with
> default options in KVM selftests, i.e. is the only thing getting in the
> way of compile with -Werror.
>
> lib/test_util.c: In function ‘strdup_printf’:
> lib/test_util.c:390:9: error: ignoring return value of ‘vasprintf’
> declared with attribute ‘warn_unused_result’ [-Werror=unused-result]
> 390 | vasprintf(&str, fmt, ap);
> | ^~~~~~~~~~~~~~~~~~~~~~~~
>
Oh, darn. My compilers didn't report that or I would have fixed it.
> Don't bother capturing the return value, allegedly vasprintf() can only
> fail due to a memory allocation failure.
>
> Fixes: dfaf20af7649 ("KVM: arm64: selftests: Replace str_with_index with strdup_printf")
> Cc: Andrew Jones <ajones@ventanamicro.com>
> Cc: Haibo Xu <haibo1.xu@intel.com>
> Cc: Anup Patel <anup@brainfault.org>
> Signed-off-by: Sean Christopherson <seanjc@google.com>
> ---
>
> I haven't actually run the relevant tests, someone should probably do so on
> ARM and/or RISC-V to make sure I didn't do something stupid.
Done for both.
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
Tested-by: Andrew Jones <ajones@ventanamicro.com>
Thanks,
drew
>
> tools/testing/selftests/kvm/lib/test_util.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/tools/testing/selftests/kvm/lib/test_util.c b/tools/testing/selftests/kvm/lib/test_util.c
> index 3e36019eeb4a..5d7f28b02d73 100644
> --- a/tools/testing/selftests/kvm/lib/test_util.c
> +++ b/tools/testing/selftests/kvm/lib/test_util.c
> @@ -387,7 +387,7 @@ char *strdup_printf(const char *fmt, ...)
> char *str;
>
> va_start(ap, fmt);
> - vasprintf(&str, fmt, ap);
> + TEST_ASSERT(vasprintf(&str, fmt, ap) >= 0, "vasprintf() failed");
> va_end(ap);
>
> return str;
>
> base-commit: 0bb80ecc33a8fb5a682236443c1e740d5c917d1d
> --
> 2.42.0.283.g2d96d420d3-goog
>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] KVM: selftests: Assert that vasprintf() is successful
2023-09-14 1:06 [PATCH] KVM: selftests: Assert that vasprintf() is successful Sean Christopherson
2023-09-14 8:02 ` Andrew Jones
@ 2023-09-20 16:29 ` Paolo Bonzini
1 sibling, 0 replies; 3+ messages in thread
From: Paolo Bonzini @ 2023-09-20 16:29 UTC (permalink / raw)
To: Sean Christopherson; +Cc: kvm, linux-kernel, Andrew Jones, Haibo Xu, Anup Patel
Queued, thanks.
Paolo
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2023-09-20 16:30 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-09-14 1:06 [PATCH] KVM: selftests: Assert that vasprintf() is successful Sean Christopherson
2023-09-14 8:02 ` Andrew Jones
2023-09-20 16:29 ` Paolo Bonzini
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox