Skip to contentSkip to conversation

KVM: selftests: Assert that vasprintf() is successful

PATCH Applied Sean Christopherson 3 participants 3 messages Sep 14, 2023 – Sep 20, 2023

Subsystems (2)
lore
Share & tools
lore

Sean Christopherson

author reviewer
#0 lore raw

PATCH 1/1 KVM: selftests: Assert that vasprintf() is successful

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 <[email protected]>
Cc Haibo Xu <[email protected]>
Cc Anup Patel <[email protected]>

---

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(-)
tools/testing/selftests/kvm/lib/test_util.c +1 -1
index 3e36019eeb4a..5d7f28b02d73 100644
@@ -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

Signature · 1 line
2.42.0.283.g2d96d420d3-goog
Reviewed-by Tested-by on v1 1/1
#1 lore raw
Quoted 11 lines · 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);
      |         ^~~~~~~~~~~~~~~~~~~~~~~~

Oh, darn. My compilers didn't report that or I would have fixed it.

Quoted 8 lines

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 <[email protected]>
Cc Haibo Xu <[email protected]>
Cc Anup Patel <[email protected]>
Quoted 4 lines

---

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.

Thanks,
drew

Quoted 22 lines
 tools/testing/selftests/kvm/lib/test_util.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/kvm/lib/test_util.c +1 -1
index 3e36019eeb4a..5d7f28b02d73 100644
@@ -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

Signature · 1 line
2.42.0.283.g2d96d420d3-goog