[LTP] [PATCH v3] mmap01: fix check_file() test for file corruption

Petr Vorel pvorel@suse.cz
Thu Jan 23 19:12:40 CET 2025


Hi all,

Reviewed-by: Petr Vorel <pvorel@suse.cz>

> Hi!
> > 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);

> 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)


More information about the ltp mailing list