Skip to contentSkip to conversation

mmap01: fix check_file() test for file corruption

PATCH In review Sven Schnelle 3 participants 3 messages Jan 23, 2025

lore
Share & tools
lore
#0 lore raw

PATCH v3 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 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(-)
testcases/kernel/syscalls/mmap/mmap01.c +9 -5
index c93c37ceda52..ffbe6485a09c 100644
@@ -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);
 }
 
Signature · 5 lines
2.47.1


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

Hi!

Quoted 21 lines
testcases/kernel/syscalls/mmap/mmap01.c +7 -3
index c93c37ceda52..ffbe6485a09c 100644
@@ -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')

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

Quoted 10 lines
testcases/kernel/syscalls/mmap/mmap01.c +2 -2
 			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);

We could close the file right after the read, that would have avoided the goto, but I guess that the patch is good enough now.

Signature · 5 lines
Cyril Hrubis
[email protected]

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

Hi all,

Quoted 24 lines

Hi!

Quoted 22 lines
testcases/kernel/syscalls/mmap/mmap01.c +7 -3
index c93c37ceda52..ffbe6485a09c 100644
@@ -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;
Quoted 11 lines

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

Quoted 8 lines
testcases/kernel/syscalls/mmap/mmap01.c +2 -2
-	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);

We could close the file right after the read, that would have avoided the goto, but I guess that the patch is good enough now.

Good point. If you don't mind, I would merge it as following.

Kind regards,
Petr

+++ testcases/kernel/syscalls/mmap/mmap01.c
@@ -39,10 +39,13 @@ static void check_file(void)

fildes = SAFE_OPEN(TEMPFILE, O_RDONLY);
len = SAFE_READ(0, fildes, buf, sizeof(buf));
+ SAFE_CLOSE(fildes);
+
if (len != strlen(STRING)) {
tst_res(TFAIL, "Read %zi expected %zu", len, strlen(STRING));
- goto out;
+ return;
}
+
for (i = 0; i < len; i++)
if (buf[i] == 'X' || buf[i] == 'Y' || buf[i] == 'Z')
break;
@@ -51,8 +54,6 @@ static void check_file(void)
tst_res(TPASS, "Specified pattern not found in file");
else
tst_res(TFAIL, "Specified pattern found in file");
-out:
- SAFE_CLOSE(fildes);
}

static void set_file(void)

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp