From 8abdc2eb40c03b97153f2a01bdb53927f3a336c1 Mon Sep 17 00:00:00 2001 From: Alexander Langer Date: Thu, 16 Jan 1997 21:58:40 +0000 Subject: [PATCH] Sweep through the tree fixing mmap() usage: - Use MAP_FAILED instead of the constant -1 to indicate failure (required by POSIX). - Removed flag arguments of '0' (required by POSIX). - Fixed code which expected an error return of 0. - Fixed code which thought any address with the high bit set was an error. - Check for failure where no checks were present. Discussed with: bde --- bin/cp/utils.c | 2 +- lib/libc/db/recno/rec_open.c | 2 +- lib/libc/gen/nlist.c | 4 ++-- libexec/ftpd/ftpd.c | 2 +- libexec/lfs_cleanerd/library.c | 8 ++++---- release/sysinstall/uc_main.c | 2 +- sbin/newfs/mkfs.c | 2 +- share/examples/meteor/rgb16.c | 4 ++-- share/examples/meteor/test-n.c | 4 ++-- share/examples/meteor/yuvpk.c | 4 ++-- share/examples/meteor/yuvpl.c | 4 ++-- share/man/man4/man4.i386/meteor.4 | 7 ++++--- usr.bin/cmp/regular.c | 4 ++-- usr.bin/locate/locate/locate.c | 2 +- usr.bin/look/look.c | 2 +- usr.bin/strip/strip.c | 2 +- usr.bin/tail/forward.c | 2 +- usr.bin/tail/reverse.c | 2 +- usr.bin/xinstall/xinstall.c | 10 +++++----- usr.bin/xlint/lint1/mem1.c | 2 +- usr.bin/xlint/lint2/mem2.c | 2 +- usr.sbin/config/main.c | 8 ++++---- usr.sbin/ctm/mkCTM/mkctm.c | 6 +++--- usr.sbin/kvm_mkdb/nlist.c | 3 +++ usr.sbin/rpc.statd/file.c | 2 +- 25 files changed, 48 insertions(+), 44 deletions(-) diff --git a/bin/cp/utils.c b/bin/cp/utils.c index 762cedfb9cf9..cda81f8eef41 100644 --- a/bin/cp/utils.c +++ b/bin/cp/utils.c @@ -121,7 +121,7 @@ copy_file(entp, dne) #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED if (fs->st_size <= 8 * 1048576) { if ((p = mmap(NULL, (size_t)fs->st_size, PROT_READ, - 0, from_fd, (off_t)0)) == (char *)-1) { + MAP_SHARED, from_fd, (off_t)0)) == MAP_FAILED) { warn("%s", entp->fts_path); rval = 1; } else { diff --git a/lib/libc/db/recno/rec_open.c b/lib/libc/db/recno/rec_open.c index 51d8a3c260fd..8f8eff24642a 100644 --- a/lib/libc/db/recno/rec_open.c +++ b/lib/libc/db/recno/rec_open.c @@ -169,7 +169,7 @@ slow: if ((t->bt_rfp = fdopen(rfd, "r")) == NULL) t->bt_msize = sb.st_size; if ((t->bt_smap = mmap(NULL, t->bt_msize, PROT_READ, MAP_PRIVATE, rfd, - (off_t)0)) == (caddr_t)-1) + (off_t)0)) == MAP_FAILED) goto slow; t->bt_cmap = t->bt_smap; t->bt_emap = t->bt_smap + sb.st_size; diff --git a/lib/libc/gen/nlist.c b/lib/libc/gen/nlist.c index 72924520d0bc..7415cbea6d6b 100644 --- a/lib/libc/gen/nlist.c +++ b/lib/libc/gen/nlist.c @@ -99,8 +99,8 @@ __fdnlist(fd, list) * without making the memory allocation permanent as with * malloc/free (i.e., munmap will return it to the system). */ - a_out_mmap = mmap(NULL, (size_t)st.st_size, PROT_READ, 0, fd, (off_t)0); - if (a_out_mmap == (char *)-1) + a_out_mmap = mmap(NULL, (size_t)st.st_size, PROT_READ, MAP_PRIVATE, fd, (off_t)0); + if (a_out_mmap == MAP_FAILED) return (-1); exec = (struct exec *)a_out_mmap; diff --git a/libexec/ftpd/ftpd.c b/libexec/ftpd/ftpd.c index ebe7dd38b5ec..71565cf1c4d3 100644 --- a/libexec/ftpd/ftpd.c +++ b/libexec/ftpd/ftpd.c @@ -1189,7 +1189,7 @@ send_data(instr, outstr, blksize, filesize, isreg) if (isreg && filesize < (off_t)16 * 1024 * 1024) { buf = mmap(0, filesize, PROT_READ, MAP_SHARED, filefd, (off_t)0); - if (!buf) { + if (buf == MAP_FAILED) { syslog(LOG_WARNING, "mmap(%lu): %m", (unsigned long)filesize); goto oldway; diff --git a/libexec/lfs_cleanerd/library.c b/libexec/lfs_cleanerd/library.c index 9397f6245206..93951c6aad1f 100644 --- a/libexec/lfs_cleanerd/library.c +++ b/libexec/lfs_cleanerd/library.c @@ -204,8 +204,8 @@ get_ifile (fsp, use_mmap) if (fsp->fi_cip) munmap((caddr_t)fsp->fi_cip, fsp->fi_ifile_length); ifp = mmap ((caddr_t)0, file_stat.st_size, - PROT_READ|PROT_WRITE, 0, fid, (off_t)0); - if (ifp == (caddr_t)(-1)) + PROT_READ|PROT_WRITE, MAP_SHARED, fid, (off_t)0); + if (ifp == MAP_FAILED) err(1, "get_ifile: mmap failed"); } else { if (fsp->fi_cip) @@ -541,8 +541,8 @@ mmap_segment (fsp, segment, segbuf, use_mmap) if (use_mmap) { *segbuf = mmap ((caddr_t)0, seg_size(lfsp), PROT_READ, - 0, fid, seg_byte); - if (*(long *)segbuf < 0) { + MAP_SHARED, fid, seg_byte); + if (*segbuf == MAP_FAILED) { err(0, "mmap_segment: mmap failed"); return (NULL); } diff --git a/release/sysinstall/uc_main.c b/release/sysinstall/uc_main.c index b6b9906fe1bd..e66ceb9b08fd 100644 --- a/release/sysinstall/uc_main.c +++ b/release/sysinstall/uc_main.c @@ -190,7 +190,7 @@ uc_open(char *name){ kern->core = mmap((caddr_t)0, sb.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, kd, 0); kern->incore = 0; - if (kern->core == (caddr_t)0) { + if (kern->core == MAP_FAILED) { free(kern); msgDebug("uc_open: Unable to mmap from %s.\n", kname); return NULL; diff --git a/sbin/newfs/mkfs.c b/sbin/newfs/mkfs.c index 20d68327e329..e7504b0c79b1 100644 --- a/sbin/newfs/mkfs.c +++ b/sbin/newfs/mkfs.c @@ -183,7 +183,7 @@ mkfs(pp, fsys, fi, fo) MAP_SHARED, fd, 0); - if((int)membase == -1) { + if(membase == MAP_FAILED) { perror("mmap"); exit(12); } diff --git a/share/examples/meteor/rgb16.c b/share/examples/meteor/rgb16.c index 17b15922a7e4..4c7ee0703c69 100644 --- a/share/examples/meteor/rgb16.c +++ b/share/examples/meteor/rgb16.c @@ -77,9 +77,9 @@ main() exit(1); } - rgb16 = (short *)mmap((caddr_t)0,SIZE,PROT_READ,0, i, (off_t)0); + rgb16 = (short *)mmap((caddr_t)0,SIZE,PROT_READ,MAP_SHARED, i, (off_t)0); - if (rgb16 == (short *) -1) return (0); + if (rgb16 == (short *) MAP_FAILED) return (0); c = METEOR_CAP_SINGLE ; ioctl(i, METEORCAPTUR, &c); diff --git a/share/examples/meteor/test-n.c b/share/examples/meteor/test-n.c index f899033b978b..4651654c96d3 100644 --- a/share/examples/meteor/test-n.c +++ b/share/examples/meteor/test-n.c @@ -127,9 +127,9 @@ main() printf("mmap %d %d\n", errno, i); size = ((width*height*depth*frames+4095)/4096)*4096; - y=(uint8 *) mmap((caddr_t)0, size + 4096, PROT_READ |PROT_WRITE,0, i, (off_t)0); + y=(uint8 *) mmap((caddr_t)0, size + 4096, PROT_READ |PROT_WRITE,MAP_SHARED, i, (off_t)0); - if (y == (uint8 *) -1) return (0); + if (y == (uint8 *) MAP_FAILED) return (0); common_mem = (struct meteor_mem *) (y + size); diff --git a/share/examples/meteor/yuvpk.c b/share/examples/meteor/yuvpk.c index b2941e049638..6ebd2e46206e 100644 --- a/share/examples/meteor/yuvpk.c +++ b/share/examples/meteor/yuvpk.c @@ -81,9 +81,9 @@ main() exit(1); } - yuv_data = (uint8 *)mmap((caddr_t)0,SIZE,PROT_READ,0, i, (off_t)0); + yuv_data = (uint8 *)mmap((caddr_t)0,SIZE,PROT_READ,MAP_SHARED, i, (off_t)0); - if (yuv_data == (int8 *) -1) return (0); + if (yuv_data == (int8 *) MAP_FAILED) return (0); c = METEOR_CAP_SINGLE ; ioctl(i, METEORCAPTUR, &c); diff --git a/share/examples/meteor/yuvpl.c b/share/examples/meteor/yuvpl.c index 243644da0526..704dfce331d3 100644 --- a/share/examples/meteor/yuvpl.c +++ b/share/examples/meteor/yuvpl.c @@ -84,9 +84,9 @@ main() exit(1); } - yuv_data = (uint8 *)mmap((caddr_t)0,SIZE,PROT_READ,0, i, (off_t)0); + yuv_data = (uint8 *)mmap((caddr_t)0,SIZE,PROT_READ,MAP_SHARED, i, (off_t)0); - if (yuv_data == (uint8 *) -1) return (0); + if (yuv_data == (uint8 *) MAP_FAILED) return (0); temp = ROWS * COLS; ue = yuv_data + temp; diff --git a/share/man/man4/man4.i386/meteor.4 b/share/man/man4/man4.i386/meteor.4 index add769cd5664..879e573dd5be 100644 --- a/share/man/man4/man4.i386/meteor.4 +++ b/share/man/man4/man4.i386/meteor.4 @@ -233,7 +233,8 @@ main() exit(1); } - mmbuf=(char *)mmap((caddr_t)0, SIZE, PROT_READ, 0, i, (off_t)0); + mmbuf=(char *)mmap((caddr_t)0, SIZE, PROT_READ, + MAP_SHARED, i, (off_t)0); #ifdef SINGLE_MODE /* single frame capture */ @@ -405,9 +406,9 @@ main() size = ((width*height*depth*frames+4095)/4096)*4096; /* add one page after data for meteor_mem */ data_frames = mmap((caddr_t)0, size + 4096, PROT_READ | PROT_WRITE, - 0, i, (off_t)0); + MAP_SHARED, i, (off_t)0); - if (data_frames == (caddr_t) -1) return (0); + if (data_frames == (caddr_t) MAP_FAILED) return (0); /* common_mem is located at page following data */ common_mem = (struct meteor_mem *) (y + size); diff --git a/usr.bin/cmp/regular.c b/usr.bin/cmp/regular.c index 5d301fad6bb9..f540ab4aebe8 100644 --- a/usr.bin/cmp/regular.c +++ b/usr.bin/cmp/regular.c @@ -80,12 +80,12 @@ c_regular(fd1, file1, skip1, len1, fd2, file2, skip2, len2) return (c_special(fd1, file1, skip1, fd2, file2, skip2)); if ((p1 = (u_char *)mmap(NULL, - (size_t)length, PROT_READ, 0, fd1, off1)) == (u_char *)-1) + (size_t)length, PROT_READ, MAP_SHARED, fd1, off1)) == (u_char *)MAP_FAILED) err(ERR_EXIT, "%s", file1); madvise(p1, length, MADV_SEQUENTIAL); if ((p2 = (u_char *)mmap(NULL, - (size_t)length, PROT_READ, 0, fd2, off2)) == (u_char *)-1) + (size_t)length, PROT_READ, MAP_SHARED, fd2, off2)) == (u_char *)MAP_FAILED) err(ERR_EXIT, "%s", file2); madvise(p2, length, MADV_SEQUENTIAL); diff --git a/usr.bin/locate/locate/locate.c b/usr.bin/locate/locate/locate.c index 427d49d2ed57..791c3eba6b70 100644 --- a/usr.bin/locate/locate/locate.c +++ b/usr.bin/locate/locate/locate.c @@ -301,7 +301,7 @@ search_mmap(db, s) if ((p = mmap((caddr_t)0, (size_t)len, PROT_READ, MAP_SHARED, - fd, (off_t)0)) == (caddr_t)-1) + fd, (off_t)0)) == MAP_FAILED) err(1, "mmap ``%s''", path_fcodes); /* foreach search string ... */ diff --git a/usr.bin/look/look.c b/usr.bin/look/look.c index e2bd95285e4e..daef12fd4361 100644 --- a/usr.bin/look/look.c +++ b/usr.bin/look/look.c @@ -143,7 +143,7 @@ main(argc, argv) if (sb.st_size > SIZE_T_MAX) err("%s: %s", file, strerror(EFBIG)); if ((front = mmap(NULL, - (size_t)sb.st_size, PROT_READ, 0, fd, (off_t)0)) == NULL) + (size_t)sb.st_size, PROT_READ, MAP_SHARED, fd, (off_t)0)) == MAP_FAILED) err("%s: %s", file, strerror(errno)); back = front + sb.st_size; exit(look(string, front, back)); diff --git a/usr.bin/strip/strip.c b/usr.bin/strip/strip.c index 8fe4c49a2f4d..36f14c26d6c4 100644 --- a/usr.bin/strip/strip.c +++ b/usr.bin/strip/strip.c @@ -173,7 +173,7 @@ s_stab(fn, fd, ep) /* Map the file. */ if ((ep = (EXEC *)mmap(NULL, (size_t)sb.st_size, - PROT_READ | PROT_WRITE, MAP_SHARED, fd, (off_t)0)) == (EXEC *)-1) { + PROT_READ | PROT_WRITE, MAP_SHARED, fd, (off_t)0)) == (EXEC *)MAP_FAILED) { err(0, "%s: %s", fn, strerror(errno)); return; } diff --git a/usr.bin/tail/forward.c b/usr.bin/tail/forward.c index 4b22eefddf08..b9457afbb0cf 100644 --- a/usr.bin/tail/forward.c +++ b/usr.bin/tail/forward.c @@ -210,7 +210,7 @@ rlines(fp, off, sbp) } if ((start = mmap(NULL, (size_t)size, - PROT_READ, 0, fileno(fp), (off_t)0)) == (caddr_t)-1) { + PROT_READ, MAP_SHARED, fileno(fp), (off_t)0)) == MAP_FAILED) { ierr(); return; } diff --git a/usr.bin/tail/reverse.c b/usr.bin/tail/reverse.c index 2653add72cd3..0a07f4f1bdcb 100644 --- a/usr.bin/tail/reverse.c +++ b/usr.bin/tail/reverse.c @@ -125,7 +125,7 @@ r_reg(fp, style, off, sbp) } if ((start = mmap(NULL, (size_t)size, - PROT_READ, 0, fileno(fp), (off_t)0)) == (caddr_t)-1) { + PROT_READ, MAP_SHARED, fileno(fp), (off_t)0)) == MAP_FAILED) { ierr(); return; } diff --git a/usr.bin/xinstall/xinstall.c b/usr.bin/xinstall/xinstall.c index 9a2a9918b66e..0148958f7e7e 100644 --- a/usr.bin/xinstall/xinstall.c +++ b/usr.bin/xinstall/xinstall.c @@ -513,11 +513,11 @@ compare(int from_fd, const char *from_name, int to_fd, const char *to_name, if (tsize <= 8 * 1024 * 1024) { done_compare = 0; if (trymmap(from_fd) && trymmap(to_fd)) { - p = mmap(NULL, tsize, PROT_READ, 0, from_fd, (off_t)0); - if ((long)p == -1) + p = mmap(NULL, tsize, PROT_READ, MAP_SHARED, from_fd, (off_t)0); + if (p == (char *)MAP_FAILED) goto out; - q = mmap(NULL, tsize, PROT_READ, 0, to_fd, (off_t)0); - if ((long)q == -1) { + q = mmap(NULL, tsize, PROT_READ, MAP_SHARED, to_fd, (off_t)0); + if (q == (char *)MAP_FAILED) { munmap(p, tsize); goto out; } @@ -581,7 +581,7 @@ copy(from_fd, from_name, to_fd, to_name, size) done_copy = 0; if (size <= 8 * 1048576 && trymmap(from_fd)) { if ((p = mmap(NULL, (size_t)size, PROT_READ, - 0, from_fd, (off_t)0)) == (char *)-1) + MAP_SHARED, from_fd, (off_t)0)) == (char *)MAP_FAILED) goto out; if ((nw = write(to_fd, p, size)) != size) { serrno = errno; diff --git a/usr.bin/xlint/lint1/mem1.c b/usr.bin/xlint/lint1/mem1.c index df880b133738..f3bf3c6a9a6b 100644 --- a/usr.bin/xlint/lint1/mem1.c +++ b/usr.bin/xlint/lint1/mem1.c @@ -180,7 +180,7 @@ xnewblk() prot = PROT_READ | PROT_WRITE; flags = MAP_ANON | MAP_PRIVATE; mb->blk = mmap(NULL, mblklen, prot, flags, -1, (off_t)0); - if (mb->blk == (void *)-1) + if (mb->blk == (void *)MAP_FAILED) err(1, "can't map memory"); if (ALIGN((u_long)mb->blk) != (u_long)mb->blk) errx(1, "mapped address is not aligned"); diff --git a/usr.bin/xlint/lint2/mem2.c b/usr.bin/xlint/lint2/mem2.c index 06d749153ed8..ed97c6fc20f4 100644 --- a/usr.bin/xlint/lint2/mem2.c +++ b/usr.bin/xlint/lint2/mem2.c @@ -82,7 +82,7 @@ xalloc(sz) prot = PROT_READ | PROT_WRITE; flags = MAP_ANON | MAP_PRIVATE; mbuf = mmap(NULL, mblklen, prot, flags, -1, (off_t)0); - if (mbuf == (void *)-1) + if (mbuf == (void *)MAP_FAILED) err(1, "can't map memory"); if (ALIGN((u_long)mbuf) != (u_long)mbuf) errx(1, "mapped address is not aligned"); diff --git a/usr.sbin/config/main.c b/usr.sbin/config/main.c index 4d3cccb78f9e..3e6778f9dd12 100644 --- a/usr.sbin/config/main.c +++ b/usr.sbin/config/main.c @@ -405,11 +405,11 @@ moveifchanged(const char *from_name, const char *to_name) tsize = (size_t)from_sb.st_size; if (!changed) { - p = mmap(NULL, tsize, PROT_READ, 0, from_fd, (off_t)0); - if ((long)p == -1) + p = mmap(NULL, tsize, PROT_READ, MAP_SHARED, from_fd, (off_t)0); + if (p == MAP_FAILED) err(EX_OSERR, "mmap %s", from_name); - q = mmap(NULL, tsize, PROT_READ, 0, to_fd, (off_t)0); - if ((long)q == -1) + q = mmap(NULL, tsize, PROT_READ, MAP_SHARED, to_fd, (off_t)0); + if (q == MAP_FAILED) err(EX_OSERR, "mmap %s", to_name); changed = memcmp(p, q, tsize); diff --git a/usr.sbin/ctm/mkCTM/mkctm.c b/usr.sbin/ctm/mkCTM/mkctm.c index 24c3aaacb204..698d1143b883 100644 --- a/usr.sbin/ctm/mkCTM/mkctm.c +++ b/usr.sbin/ctm/mkCTM/mkctm.c @@ -179,11 +179,11 @@ Equ(const char *dir1, const char *dir2, const char *name, struct dirent *de) } #endif p1=mmap(0, s1.st_size, PROT_READ, MAP_PRIVATE, fd1, 0); - if ((int)p1 == -1) { perror(buf1); exit(3); } + if (p1 == (u_char *)MAP_FAILED) { perror(buf1); exit(3); } close(fd1); p2=mmap(0, s2.st_size, PROT_READ, MAP_PRIVATE, fd2, 0); - if ((int)p2 == -1) { perror(buf2); exit(3); } + if (p2 == (u_char *)MAP_FAILED) { perror(buf2); exit(3); } close(fd2); /* If identical, we're done. */ @@ -322,7 +322,7 @@ Add(const char *dir1, const char *dir2, const char *name, struct dirent *de) if (fd1 < 0) {perror(buf2); exit (3); } fstat(fd1, &st); p1=mmap(0, st.st_size, PROT_READ, MAP_PRIVATE, fd1, 0); - if ((int)p1 == -1) { perror(buf2); exit(3); } + if (p1 == (u_char *)MAP_FAILED) { perror(buf2); exit(3); } close(fd1); m2 = MD5Data(p1, st.st_size, md5_2); name_stat("CTMFM", dir2, name, de); diff --git a/usr.sbin/kvm_mkdb/nlist.c b/usr.sbin/kvm_mkdb/nlist.c index adf4a421fba4..58d66fa7fb80 100644 --- a/usr.sbin/kvm_mkdb/nlist.c +++ b/usr.sbin/kvm_mkdb/nlist.c @@ -88,6 +88,9 @@ create_knlist(name, db) filep = (u_char*)mmap(0, sst.st_size, PROT_READ, MAP_PRIVATE, fd, 0); + if (filep == (u_char*)MAP_FAILED) + err(1, "mmap failed"); + /* Read in exec structure. */ ebuf = (struct exec *) filep; diff --git a/usr.sbin/rpc.statd/file.c b/usr.sbin/rpc.statd/file.c index aeb1fc09651d..5dbc1cac6e80 100644 --- a/usr.sbin/rpc.statd/file.c +++ b/usr.sbin/rpc.statd/file.c @@ -158,7 +158,7 @@ void init_file(char *filename) status_info = (FileLayout *) mmap(NULL, 0x10000000, PROT_READ | PROT_WRITE, MAP_SHARED, status_fd, 0); - if (status_info == (FileLayout *) -1) + if (status_info == (FileLayout *) MAP_FAILED) { perror("rpc.statd"); fprintf(stderr, "Unable to mmap() status file\n");