Skip to contentSkip to conversation

mmap() return value when length == 0

DISCUSSION jacob berkman 2 participants 2 messages Aug 23, 2001

lore
Share & tools
lore
#0 lore raw

hello,

on linux (2.4.9 and 2.2.18), the mmap() syscall will return NULL if the length argument is 0 rather than returning MAP_FAILED (-1). this is different than both solaris and hp-ux, and the linux man page doesn't indicate that it should do this.

so, is this indeed the desired behaviour or a longstanding bug?

thanks,
jacob

#1 lore raw
Quoted 6 lines · jacob berkman

on linux (2.4.9 and 2.2.18), the mmap() syscall will return NULL if the length argument is 0 rather than returning MAP_FAILED (-1). this is different than both solaris and hp-ux, and the linux man page doesn't indicate that it should do this.

so, is this indeed the desired behaviour or a longstanding bug?

Surely the posix standard would say.

The sol8 manpage says specifically that EINVAL is returned if len <= 0;

And an openbsd and freebsd box i tried it on happily do the mmaps as well.

All three (linux, sol8, fbsd) segfault with the following code for various values of x and y.

fd = open();
marea = mmap(0, x, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); munmap(marea, y);
fclose(fd);

mmap:
linux
succeeds
sol8
succeeds if x>0, fails otherwise
fbsd4.3
succeeds

munmap:
linux
succeeds unless x>0 and y>x, in which case it segfaults
sol8
succeeds
fbsd
succeeds

close:
linux
succeeds
sol8
segfaults if y>x
fbsd
segfaults if y>x

justin