From: Jinseok Kim <always.starving0@gmail.com>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH] read: verify read size and data correctness
Date: Sun, 22 Mar 2026 00:37:21 +0900 [thread overview]
Message-ID: <20260321153734.5590-1-always.starving0@gmail.com> (raw)
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
reply other threads:[~2026-03-21 15:38 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260321153734.5590-1-always.starving0@gmail.com \
--to=always.starving0@gmail.com \
--cc=ltp@lists.linux.it \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox