[PATCH] selftests/net: use MAP_FAILED instead of (void *)-1 in tcp_mmap

longlong yan posted 1 patch 2 days, 21 hours ago
tools/testing/selftests/net/tcp_mmap.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
[PATCH] selftests/net: use MAP_FAILED instead of (void *)-1 in tcp_mmap
Posted by longlong yan 2 days, 21 hours ago
mmap() is documented to return MAP_FAILED on error, but tcp_mmap.c
compares the return value against (void *)-1 and (unsigned char *)-1.

Replace these with the standard MAP_FAILED macro for better readability
and type safety.

Signed-off-by: longlong yan <yanlonglong@kylinos.cn>
---
 tools/testing/selftests/net/tcp_mmap.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/tools/testing/selftests/net/tcp_mmap.c b/tools/testing/selftests/net/tcp_mmap.c
index 2544ae35d07a..487ae659a1f1 100644
--- a/tools/testing/selftests/net/tcp_mmap.c
+++ b/tools/testing/selftests/net/tcp_mmap.c
@@ -141,12 +141,12 @@ static void *mmap_large_buffer(size_t need, size_t *allocated)
 	buffer = mmap(NULL, sz, PROT_READ | PROT_WRITE,
 		      MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB, -1, 0);
 
-	if (buffer == (void *)-1) {
+	if (buffer == MAP_FAILED) {
 		sz = need;
 		buffer = mmap(NULL, sz, PROT_READ | PROT_WRITE,
 			      MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE,
 			      -1, 0);
-		if (buffer != (void *)-1)
+		if (buffer != MAP_FAILED)
 			fprintf(stderr, "MAP_HUGETLB attempt failed, look at /sys/kernel/mm/hugepages for optimal performance\n");
 	}
 	*allocated = sz;
@@ -189,13 +189,13 @@ void *child_thread(void *arg)
 
 	fcntl(fd, F_SETFL, O_NDELAY);
 	buffer = mmap_large_buffer(chunk_size, &buffer_sz);
-	if (buffer == (void *)-1) {
+	if (buffer == MAP_FAILED) {
 		perror("mmap");
 		goto error;
 	}
 	if (zflg) {
 		raddr = mmap(NULL, chunk_size + map_align, PROT_READ, flags, fd, 0);
-		if (raddr == (void *)-1) {
+		if (raddr == MAP_FAILED) {
 			perror("mmap");
 			zflg = 0;
 		} else {
@@ -547,7 +547,7 @@ int main(int argc, char *argv[])
 	}
 
 	buffer = mmap_large_buffer(chunk_size, &buffer_sz);
-	if (buffer == (unsigned char *)-1) {
+	if (buffer == MAP_FAILED) {
 		perror("mmap");
 		exit(1);
 	}
-- 
2.43.0
Re: [PATCH] selftests/net: use MAP_FAILED instead of (void *)-1 in tcp_mmap
Posted by Joe Damato 2 days, 10 hours ago
On Wed, Jul 22, 2026 at 09:51:29AM +0800, longlong yan wrote:
> mmap() is documented to return MAP_FAILED on error, but tcp_mmap.c
> compares the return value against (void *)-1 and (unsigned char *)-1.
> 
> Replace these with the standard MAP_FAILED macro for better readability
> and type safety.
> 
> Signed-off-by: longlong yan <yanlonglong@kylinos.cn>
> ---
>  tools/testing/selftests/net/tcp_mmap.c | 10 +++++-----
>  1 file changed, 5 insertions(+), 5 deletions(-)
>

Looks right to me. Not sure if it needs to be set with "net-next" in the
subject line to make which tree this targets more explicit?

Either way:

Reviewed-by: Joe Damato <joe@dama.to>
Re: [PATCH] selftests/net: use MAP_FAILED instead of (void *)-1 in tcp_mmap
Posted by Eric Dumazet 2 days, 9 hours ago
On Wed, Jul 22, 2026 at 3:15 PM Joe Damato <joe@dama.to> wrote:
>
> On Wed, Jul 22, 2026 at 09:51:29AM +0800, longlong yan wrote:
> > mmap() is documented to return MAP_FAILED on error, but tcp_mmap.c
> > compares the return value against (void *)-1 and (unsigned char *)-1.
> >
> > Replace these with the standard MAP_FAILED macro for better readability
> > and type safety.
> >
> > Signed-off-by: longlong yan <yanlonglong@kylinos.cn>
> > ---
> >  tools/testing/selftests/net/tcp_mmap.c | 10 +++++-----
> >  1 file changed, 5 insertions(+), 5 deletions(-)
> >
>
> Looks right to me. Not sure if it needs to be set with "net-next" in the
> subject line to make which tree this targets more explicit?
>
> Either way:
>
> Reviewed-by: Joe Damato <joe@dama.to>

Yeah, I doubt linux will ever use a different value than (void *)-1
for MAP_FAILED.

Reviewed-by: Eric Dumazet <edumazet@google.com>