On Wed, Nov 03, 2010 at 01:12:20PM +0100, Otto Moerbeek wrote:

> On Wed, Nov 03, 2010 at 01:18:51PM +0200, Alexey Suslikov wrote:
> 
> > Hello t...@.
> > 
> > On OpenBSD/amd64, doing something like
> >     char *buf = mmap(NULL, len + 1, PROT_READ | PROT_WRITE,
> > MAP_PRIVATE, some.fd, 0);
> >     buf[len] = '\0';
> > causes segfault on buf[len] = '\0' assignment if len = 16384.
> > 
> > However doing
> >     char *buf = mmap(NULL, len + 1, PROT_READ | PROT_WRITE,
> > MAP_PRIVATE, some.fd, 0);
> >     char *nbuf = malloc(len + 1);
> >     memcpy(nbuf, buf, len);
> >     nbuf[len] = '\0';
> > does not lead to a crash.
> > 
> > Is it expected behavior of mmap (alignment?) or usage of mmap is wrong?
> > 
> > Thanks.
> > 
> > Alexey
> 
> This (complete!) program does not show the behahaviour. Please post a
> complete testcase. Did you include sys/mman.h?

BTW, accesses beyond the file do cause a segfault, and that is correct.

Note that your firts case accesses buf[16384], while your memcpy does
not access that address.

        -Otto

> 
> #include <sys/types.h>
> #include <sys/mman.h>
> 
> #include <err.h>
> #include <fcntl.h>
> #include <stdio.h>
> #include <stdlib.h>
> 
> 
> int
> main()
> {
>       int fd;
>       char *buf;
>       size_t len;
> 
>       fd = open("file", O_RDWR, 0);
>       if (fd == -1)
>               err(1, NULL);
> 
>       len = 16384;
>       buf = mmap(NULL, len + 1, PROT_READ | PROT_WRITE, MAP_PRIVATE,
>           fd, (off_t)0);
>       if (buf == MAP_FAILED)
>               err(1, NULL);
>       buf[len] = '\0';
> }

Reply via email to