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
This commit is contained in:
Alexander Langer 1997-01-16 21:58:40 +00:00
parent 839cc09e53
commit 8abdc2eb40
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=21786
25 changed files with 48 additions and 44 deletions

View File

@ -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 {

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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);
}

View File

@ -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;

View File

@ -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);
}

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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;

View File

@ -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);

View File

@ -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);

View File

@ -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 ... */

View File

@ -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));

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;

View File

@ -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");

View File

@ -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");

View File

@ -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);

View File

@ -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);

View File

@ -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;

View File

@ -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");