[LTP] [PATCH] read: verify read size and data correctness
Jinseok Kim
always.starving0@gmail.com
Sat Mar 21 16:37:21 CET 2026
The test only checked that read(2) did not fail, which allows
partial reads or unexpected data to pass unnoticed.
Verify that read(2) returns the expected number of bytes and
that the read data matches the written buffer.
Signed-off-by: Jinseok Kim <always.starving0@gmail.com>
---
testcases/kernel/syscalls/read/read01.c | 27 ++++++++++++++++++-------
1 file changed, 20 insertions(+), 7 deletions(-)
diff --git a/testcases/kernel/syscalls/read/read01.c b/testcases/kernel/syscalls/read/read01.c
index 68d6346c5..f205979b9 100644
--- a/testcases/kernel/syscalls/read/read01.c
+++ b/testcases/kernel/syscalls/read/read01.c
@@ -10,25 +10,38 @@
#define SIZE 512
static int fd;
-static char buf[SIZE];
+static char write_buf[SIZE];
+static char read_buf[SIZE];
static void verify_read(void)
{
SAFE_LSEEK(fd, 0, SEEK_SET);
- TEST(read(fd, buf, SIZE));
+ TEST(read(fd, read_buf, SIZE));
- if (TST_RET == -1)
+ if (TST_RET == -1) {
tst_res(TFAIL | TTERRNO, "read(2) failed");
- else
- tst_res(TPASS, "read(2) returned %ld", TST_RET);
+ return;
+ }
+
+ if (TST_RET != SIZE) {
+ tst_res(TFAIL, "read(2) returned %ld, expected %d", TST_RET, SIZE);
+ return;
+ }
+
+ if (memcmp(write_buf, read_buf, SIZE)) {
+ tst_res(TFAIL, "read(2) returned unexpected data");
+ return;
+ }
+
+ tst_res(TPASS, "read(2) returned expected data %ld", TST_RET);
}
static void setup(void)
{
- memset(buf, '*', SIZE);
+ memset(write_buf, '*', SIZE);
fd = SAFE_OPEN("testfile", O_RDWR | O_CREAT, 0700);
- SAFE_WRITE(SAFE_WRITE_ALL, fd, buf, SIZE);
+ SAFE_WRITE(SAFE_WRITE_ALL, fd, write_buf, SIZE);
}
static void cleanup(void)
--
2.43.0
More information about the ltp
mailing list