From 4b142f02b914c56228fcab08095d7f6aa56ca8ad Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Mon, 19 Feb 2007 10:25:54 +0000 Subject: [PATCH] * Check mmap() return value. * Don't pass offsets to mmap() that are not page aligned. --- test/zzcat.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/test/zzcat.c b/test/zzcat.c index 7fd5d24..bd662f8 100644 --- a/test/zzcat.c +++ b/test/zzcat.c @@ -112,18 +112,28 @@ int main(int argc, char *argv[]) #ifdef HAVE_MMAP for(i = 0; i < 128; i++) { - int moff = myrand() % len; - int mlen = myrand() % (len - moff); - char *map = mmap(NULL, mlen, PROT_READ, MAP_PRIVATE, fd, moff); + char *map; + int moff, mlen, pgsz = len + 1; +#ifdef HAVE_GETPAGESIZE + pgsz = getpagesize(); +#endif + moff = len < pgsz ? 0 : (myrand() % (len / pgsz)) * pgsz; + mlen = 1 + (myrand() % (len - moff)); + map = mmap(NULL, mlen, PROT_READ, MAP_PRIVATE, fd, moff); + if(map == MAP_FAILED) + return EXIT_FAILURE; for(j = 0; j < 128; j++) { int x = myrand() % mlen; - data[moff + x] = data[x]; + data[moff + x] = map[x]; } munmap(map, mlen); } #endif close(fd); + break; + default: + return EXIT_FAILURE; } /* Write what we have read */