public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
* [LTP] [PATCH v3] mmap01: fix check_file() test for file corruption
@ 2025-01-23 14:36 Sven Schnelle
  2025-01-23 14:42 ` Cyril Hrubis
  0 siblings, 1 reply; 3+ messages in thread
From: Sven Schnelle @ 2025-01-23 14:36 UTC (permalink / raw)
  To: ltp

mmap01 reported random test failures. Investigation showed
that check_file() will compare the whole buffer even if less
bytes were read from the file. Adjust check_file() to:

- fail the test if the not the correct amount of data has been read.
- only compare the bytes actually read

Signed-off-by: Sven Schnelle <svens@linux.ibm.com>
---
Changes in v3:
- remove LF from log message
- also log read size and expected size

Changes in v2:

- check return value from SAFE_READ
- only verify bytes actually read

testcases/kernel/syscalls/mmap/mmap01.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/testcases/kernel/syscalls/mmap/mmap01.c b/testcases/kernel/syscalls/mmap/mmap01.c
index c93c37ceda52..ffbe6485a09c 100644
--- a/testcases/kernel/syscalls/mmap/mmap01.c
+++ b/testcases/kernel/syscalls/mmap/mmap01.c
@@ -35,19 +35,23 @@ static void check_file(void)
 {
 	int i, fildes, buf_len = sizeof(STRING) + 3;
 	char buf[buf_len];
+	ssize_t len;
 
 	fildes = SAFE_OPEN(TEMPFILE, O_RDONLY);
-	SAFE_READ(0, fildes, buf, sizeof(buf));
-
-	for (i = 0; i < buf_len; i++)
+	len = SAFE_READ(0, fildes, buf, sizeof(buf));
+	if (len != strlen(STRING)) {
+		tst_res(TFAIL, "Read %zi expected %zu", len, strlen(STRING));
+		goto out;
+	}
+	for (i = 0; i < len; i++)
 		if (buf[i] == 'X' || buf[i] == 'Y' || buf[i] == 'Z')
 			break;
 
-	if (i == buf_len)
+	if (i == len)
 		tst_res(TPASS, "Specified pattern not found in file");
 	else
 		tst_res(TFAIL, "Specified pattern found in file");
-
+out:
 	SAFE_CLOSE(fildes);
 }
 
-- 
2.47.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-01-23 18:13 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-01-23 14:36 [LTP] [PATCH v3] mmap01: fix check_file() test for file corruption Sven Schnelle
2025-01-23 14:42 ` Cyril Hrubis
2025-01-23 18:12   ` Petr Vorel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox