Skip to contentSkip to conversation

mmap01: fix check_file() test for file corruption

PATCH Superseded Sven Schnelle 2 participants 2 messages Jan 22, 2025 – Jan 23, 2025

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 mmap01: fix check_file() test for file corruption

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

---
Changes in v2:

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

 testcases/kernel/syscalls/mmap/mmap01.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
testcases/kernel/syscalls/mmap/mmap01.c +6 -3
index c93c37ceda52..b5798c8d2367 100644
@@ -35,15 +35,18 @@ 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));
+	len = SAFE_READ(0, fildes, buf, sizeof(buf));
+	if (len != strlen(STRING))
+		tst_res(TFAIL, "Error reading from file\n");
 
-	for (i = 0; i < buf_len; i++)
+	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");
Signature · 5 lines
2.47.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

Hi!

Quoted 22 lines

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

---
Changes in v2:

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

 testcases/kernel/syscalls/mmap/mmap01.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
testcases/kernel/syscalls/mmap/mmap01.c +0 -0
index c93c37ceda52..b5798c8d2367 100644

testcases/kernel/syscalls/mmap/mmap01.c static void check_file(void) · line 43

Quoted 10 lines
testcases/kernel/syscalls/mmap/mmap01.c +4 -1
@@ -35,15 +35,18 @@ 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));
+	len = SAFE_READ(0, fildes, buf, sizeof(buf));
+	if (len != strlen(STRING))
+		tst_res(TFAIL, "Error reading from file\n");

^
no newline here please

And possibly it would make sense to write how many characters were read and how many we expected so something as:

tst_res(TFAIL, "Read %zi expected %zu", len, strlen(STRING));

and we also want to do return; if we fail to read strlen(STRING) bytes because checking the buffer content does not make sense at that point, since it's possibly only partialy intialized

Quoted 17 lines
testcases/kernel/syscalls/mmap/mmap01.c +2 -2
-	for (i = 0; i < buf_len; i++)
+	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");
Signature · 5 lines
2.47.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 5 lines
Cyril Hrubis
[email protected]

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