aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOliver Upton <oupton@google.com>2022-07-19 14:31:32 +0000
committerPaolo Bonzini <pbonzini@redhat.com>2022-08-01 08:01:22 -0400
commitad5b072716e9ea7164b578a06c615966e9203c45 (patch)
treeafb41756421e398817938394783212f31d95c8a0
parent31f6e3832a0f1c366e54033335aed2375f6e447a (diff)
downloadlinux-ad5b072716e9ea7164b578a06c615966e9203c45.tar.gz
selftests: KVM: Check stat name before other fields
In order to provide more useful test assertions that describe the broken stats descriptor, perform sanity check on the stat name before any other descriptor field. While at it, avoid dereferencing the name field if the sanity check fails as it is more likely to contain garbage. Signed-off-by: Oliver Upton <oupton@google.com> Reviewed-by: Andrew Jones <andrew.jones@linux.dev> Message-Id: <20220719143134.3246798-2-oliver.upton@linux.dev> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
-rw-r--r--tools/testing/selftests/kvm/kvm_binary_stats_test.c8
1 files changed, 5 insertions, 3 deletions
diff --git a/tools/testing/selftests/kvm/kvm_binary_stats_test.c b/tools/testing/selftests/kvm/kvm_binary_stats_test.c
index b01e8b0851e755..40227ad9ba0c7b 100644
--- a/tools/testing/selftests/kvm/kvm_binary_stats_test.c
+++ b/tools/testing/selftests/kvm/kvm_binary_stats_test.c
@@ -73,6 +73,10 @@ static void stats_test(int stats_fd)
for (i = 0; i < header.num_desc; ++i) {
pdesc = get_stats_descriptor(stats_desc, i, &header);
+ /* Check name string */
+ TEST_ASSERT(strlen(pdesc->name) < header.name_size,
+ "KVM stats name (index: %d) too long", i);
+
/* Check type,unit,base boundaries */
TEST_ASSERT((pdesc->flags & KVM_STATS_TYPE_MASK) <= KVM_STATS_TYPE_MAX,
"Unknown KVM stats type");
@@ -99,9 +103,7 @@ static void stats_test(int stats_fd)
TEST_ASSERT(pdesc->exponent <= 0, "Unsupported KVM stats unit");
break;
}
- /* Check name string */
- TEST_ASSERT(strlen(pdesc->name) < header.name_size,
- "KVM stats name(%s) too long", pdesc->name);
+
/* Check size field, which should not be zero */
TEST_ASSERT(pdesc->size,
"KVM descriptor(%s) with size of 0", pdesc->name);