* [LTP] [PATCH] read: verify read size and data correctness
@ 2026-03-21 15:37 Jinseok Kim
0 siblings, 0 replies; only message in thread
From: Jinseok Kim @ 2026-03-21 15:37 UTC (permalink / raw)
To: ltp
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
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2026-03-21 15:38 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-21 15:37 [LTP] [PATCH] read: verify read size and data correctness Jinseok Kim
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox