Skip to content

Commit 625646a

Browse files
sean-jcbonzini
authored andcommitted
KVM: selftests: Use pread() to read binary stats header
Use pread() with an explicit offset when reading the header and the header name for a binary stats fd so that the common helper and the binary stats test don't subtly rely on the file effectively being untouched, e.g. to allow multiple reads of the header, name, etc. Signed-off-by: Sean Christopherson <seanjc@google.com> Message-Id: <20230711230131.648752-3-seanjc@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent eed3013 commit 625646a

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

tools/testing/selftests/kvm/include/kvm_util_base.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,10 @@ static inline void read_stats_header(int stats_fd, struct kvm_stats_header *head
362362
{
363363
ssize_t ret;
364364

365-
ret = read(stats_fd, header, sizeof(*header));
366-
TEST_ASSERT(ret == sizeof(*header), "Read stats header");
365+
ret = pread(stats_fd, header, sizeof(*header), 0);
366+
TEST_ASSERT(ret == sizeof(*header),
367+
"Failed to read '%lu' header bytes, ret = '%ld'",
368+
sizeof(*header), ret);
367369
}
368370

369371
struct kvm_stats_desc *read_stats_descriptors(int stats_fd,

tools/testing/selftests/kvm/kvm_binary_stats_test.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ static void stats_test(int stats_fd)
4343
id = malloc(header.name_size);
4444
TEST_ASSERT(id, "Allocate memory for id string");
4545

46-
ret = read(stats_fd, id, header.name_size);
47-
TEST_ASSERT(ret == header.name_size, "Read id string");
46+
ret = pread(stats_fd, id, header.name_size, sizeof(header));
47+
TEST_ASSERT(ret == header.name_size,
48+
"Expected header size '%u', read '%lu' bytes",
49+
header.name_size, ret);
4850

4951
/* Check id string, that should start with "kvm" */
5052
TEST_ASSERT(!strncmp(id, "kvm", 3) && strlen(id) < header.name_size,

0 commit comments

Comments
 (0)