Skip to contentSkip to conversation

KVM: selftests: Drop the unsigned >= 0 assertions in test_write/test_read

PATCH In review Gokul K 2 participants 2 messages Sep 17

Subsystems (2)
lore
Share & tools
lore

Gokul K

author
#0 lore raw

PATCH 1/1 KVM: selftests: Drop the unsigned >= 0 assertions in test_write/test_read

test_write() and test_read() both open with

	TEST_ASSERT(count >= 0, "Unexpected count, count: %li", count);

but @count is a size_t, so the condition is always true and the assertion can never fire. Building the selftests with -Wextra says so:

lib/io.c:51:27: warning: comparison of unsigned expression in '>= 0'
is always true [-Wtype-limits]
lib/io.c:128:27: warning: comparison of unsigned expression in '>= 0'
is always true [-Wtype-limits]

@count has been a size_t since these helpers were added in commit 6089ae0bd5e1 ("kvm: selftests: add sync_regs_test"), so this has never guarded anything; nothing regressed and there is no behavioural change.

Note also that the message the assertion would have printed is wrong: %li takes a long, not a size_t. That has gone unnoticed precisely because the assertion is unreachable, which is a fair summary of the value it adds.

Delete both. The comment above each one is about a count of zero being legitimate, which remains true and is worth keeping.

---
 tools/testing/selftests/kvm/lib/io.c | 2 --
 1 file changed, 2 deletions(-)
tools/testing/selftests/kvm/lib/io.c +0 -2
index fedb2a741f0b..356586250595 100644
@@ -48,7 +48,6 @@ ssize_t test_write(int fd, const void *buf, size_t count)
 	/* Note: Count of zero is allowed (see "RETURN VALUE" portion of
 	 * write(2) manpage for details.
 	 */
-	TEST_ASSERT(count >= 0, "Unexpected count, count: %li", count);
 
 	do {
 		rc = write(fd, ptr, num_left);
@@ -125,7 +124,6 @@ ssize_t test_read(int fd, void *buf, size_t count)
 	/* Note: Count of zero is allowed (see "If count is zero" portion of
 	 * read(2) manpage for details.
 	 */
-	TEST_ASSERT(count >= 0, "Unexpected count, count: %li", count);
 
 	do {
 		rc = read(fd, ptr, num_left);
Signature · 1 line
2.54.0
#1 lore raw
Quoted 16 lines · Gokul K

test_write() and test_read() both open with

	TEST_ASSERT(count >= 0, "Unexpected count, count: %li", count);

but @count is a size_t, so the condition is always true and the assertion can never fire. Building the selftests with -Wextra says so:

lib/io.c:51:27: warning: comparison of unsigned expression in '>= 0'
is always true [-Wtype-limits]
lib/io.c:128:27: warning: comparison of unsigned expression in '>= 0'
is always true [-Wtype-limits]

@count has been a size_t since these helpers were added in commit 6089ae0bd5e1 ("kvm: selftests: add sync_regs_test"), so this has never guarded anything; nothing regressed and there is no behavioural change.

Quoted 6 lines

Note also that the message the assertion would have printed is wrong: %li takes a long, not a size_t. That has gone unnoticed precisely because the assertion is unreachable, which is a fair summary of the value it adds.

Delete both. The comment above each one is about a count of zero being legitimate, which remains true and is worth keeping.

I disagree. This code is all rather non-sensical. Keeping the comment with code that doesn't handle it correct can't work. Because per the manpage:

a read() with a count of 0 returns zero and has no other effects.

which means passing in a size of 0 will hit the "case 0" and fail:

		rc = read(fd, ptr, num_left);

		switch (rc) {
		case -1:
			TEST_ASSERT(errno == EAGAIN || errno == EINTR,
				    "Unexpected read failure,\n"
				    "  rc: %zi errno: %i", rc, errno);
			break;

		case 0:
			TEST_FAIL("Unexpected EOF,\n"  <==========================
				  "   rc: %zi num_read: %zi num_left: %zu",
				  rc, num_read, num_left);
			break;

		default:
			TEST_ASSERT(rc > 0, "Unexpected ret from read,\n"
				    "  rc: %zi errno: %i", rc, errno);
			num_read += rc;
			num_left -= rc;
			ptr += rc;
			break;

The only user of test_read() is tools/testing/selftests/kvm/lib/elf.c, and all users guarantee a non-zero count. test_write() is dead code, and test_seq_read() has a defunct declaration.

The names are also confusing, because it's easy to read it as "test the read() sycall", not "do read() for this test".

So, I think we should move test_read() into elf.c, drop the absurdly verbose and unhelpful commentry, then kill off io.c, test_write() and the stale test_seq_read().

testing/selftests/kvm/Makefile.kvm +0 -1
index 6a1482e3a286..610b4d959673 100644
@@ -6,7 +6,6 @@ all:
 LIBKVM += lib/assert.c
 LIBKVM += lib/elf.c
 LIBKVM += lib/guest_modes.c
-LIBKVM += lib/io.c
 LIBKVM += lib/kvm_util.c
 LIBKVM += lib/lru_gen_util.c
 LIBKVM += lib/memstress.c
testing/selftests/kvm/include/test_util.h +0 -4
index a6a3e1657895..e558346c3b69 100644
@@ -49,10 +49,6 @@ do {								\
 
 #define TEST_REQUIRE(f) __TEST_REQUIRE(f, "Requirement not met: %s", #f)
 
-ssize_t test_write(int fd, const void *buf, size_t count);
-ssize_t test_read(int fd, void *buf, size_t count);
-int test_seq_read(const char *path, char **bufp, size_t *sizep);
-
 void __printf(5, 6) test_assert(bool exp, const char *exp_str,
 				const char *file, unsigned int line,
 				const char *fmt, ...);
testing/selftests/kvm/lib/elf.c +42 -4

76 lines not on this page · raw message

testing/selftests/kvm/lib/io.c deleted +0 -157

157 lines not on this page · raw message