Skip to content

Commit ad5b072

Browse files
ouptonbonzini
authored andcommitted
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>
1 parent 31f6e38 commit ad5b072

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

tools/testing/selftests/kvm/kvm_binary_stats_test.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ static void stats_test(int stats_fd)
7373
for (i = 0; i < header.num_desc; ++i) {
7474
pdesc = get_stats_descriptor(stats_desc, i, &header);
7575

76+
/* Check name string */
77+
TEST_ASSERT(strlen(pdesc->name) < header.name_size,
78+
"KVM stats name (index: %d) too long", i);
79+
7680
/* Check type,unit,base boundaries */
7781
TEST_ASSERT((pdesc->flags & KVM_STATS_TYPE_MASK) <= KVM_STATS_TYPE_MAX,
7882
"Unknown KVM stats type");
@@ -99,9 +103,7 @@ static void stats_test(int stats_fd)
99103
TEST_ASSERT(pdesc->exponent <= 0, "Unsupported KVM stats unit");
100104
break;
101105
}
102-
/* Check name string */
103-
TEST_ASSERT(strlen(pdesc->name) < header.name_size,
104-
"KVM stats name(%s) too long", pdesc->name);
106+
105107
/* Check size field, which should not be zero */
106108
TEST_ASSERT(pdesc->size,
107109
"KVM descriptor(%s) with size of 0", pdesc->name);

0 commit comments

Comments
 (0)