I have reviewed all occurrences of mmap() in git and fixed three types
of errors/defects:

1) The result is not checked.
2) The file descriptor is closed if mmap() succeeds, but not when it
fails.
3) Various casts applied to -1 are used instead of MAP_FAILED, which is
specifically defined to check mmap() return value.

[jc: This is a second round of Pavel's patch.  He fixed up the problem
that close() potentially clobbering the errno from mmap, which
the first round had.]

Signed-off-by: Pavel Roskin <proski@gnu.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
This commit is contained in:
Pavel Roskin 2005-07-29 10:49:14 -04:00 коммит произвёл Junio C Hamano
Родитель 1df092d211
Коммит e35f982415
8 изменённых файлов: 15 добавлений и 12 удалений

4
diff.c
Просмотреть файл

@ -377,8 +377,10 @@ int diff_populate_filespec(struct diff_filespec *s, int size_only)
if (fd < 0) if (fd < 0)
goto err_empty; goto err_empty;
s->data = mmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0); s->data = mmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
s->should_munmap = 1;
close(fd); close(fd);
if (s->data == MAP_FAILED)
goto err_empty;
s->should_munmap = 1;
} }
else { else {
char type[20]; char type[20];

Просмотреть файл

@ -28,7 +28,7 @@ static void prepare_order(const char *orderfile)
} }
map = mmap(NULL, st.st_size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); map = mmap(NULL, st.st_size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
close(fd); close(fd);
if (-1 == (int)(long)map) if (map == MAP_FAILED)
return; return;
endp = map + st.st_size; endp = map + st.st_size;
for (pass = 0; pass < 2; pass++) { for (pass = 0; pass < 2; pass++) {

Просмотреть файл

@ -54,7 +54,7 @@ int fetch(unsigned char *sha1)
} }
map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, ifd, 0); map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, ifd, 0);
close(ifd); close(ifd);
if (-1 == (int)(long)map) { if (map == MAP_FAILED) {
fprintf(stderr, "cannot mmap %s\n", filename); fprintf(stderr, "cannot mmap %s\n", filename);
return -1; return -1;
} }

Просмотреть файл

@ -392,7 +392,7 @@ int read_cache(void)
return (errno == ENOENT) ? 0 : error("open failed"); return (errno == ENOENT) ? 0 : error("open failed");
size = 0; // avoid gcc warning size = 0; // avoid gcc warning
map = (void *)-1; map = MAP_FAILED;
if (!fstat(fd, &st)) { if (!fstat(fd, &st)) {
size = st.st_size; size = st.st_size;
errno = EINVAL; errno = EINVAL;
@ -400,7 +400,7 @@ int read_cache(void)
map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
} }
close(fd); close(fd);
if (-1 == (int)(long)map) if (map == MAP_FAILED)
return error("mmap failed"); return error("mmap failed");
hdr = map; hdr = map;

Просмотреть файл

@ -212,11 +212,9 @@ int read_rev_cache(const char *path, FILE *dumpfile, int dry_run)
return -1; return -1;
} }
map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (map == MAP_FAILED) {
close(fd); close(fd);
if (map == MAP_FAILED)
return -1; return -1;
}
close(fd);
memset(last_sha1, 0, 20); memset(last_sha1, 0, 20);
ofs = 0; ofs = 0;

Просмотреть файл

@ -518,7 +518,7 @@ static void *map_sha1_file_internal(const unsigned char *sha1,
} }
map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
close(fd); close(fd);
if (-1 == (int)(long)map) if (map == MAP_FAILED)
return NULL; return NULL;
*size = st.st_size; *size = st.st_size;
return map; return map;
@ -1363,7 +1363,7 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, con
if (size) if (size)
buf = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); buf = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
close(fd); close(fd);
if ((int)(long)buf == -1) if (buf == MAP_FAILED)
return -1; return -1;
if (!type) if (!type)

Просмотреть файл

@ -41,6 +41,7 @@ int main(int argc, char *argv[])
from_buf = mmap(NULL, from_size, PROT_READ, MAP_PRIVATE, fd, 0); from_buf = mmap(NULL, from_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (from_buf == MAP_FAILED) { if (from_buf == MAP_FAILED) {
perror(argv[2]); perror(argv[2]);
close(fd);
return 1; return 1;
} }
close(fd); close(fd);
@ -54,6 +55,7 @@ int main(int argc, char *argv[])
data_buf = mmap(NULL, data_size, PROT_READ, MAP_PRIVATE, fd, 0); data_buf = mmap(NULL, data_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (data_buf == MAP_FAILED) { if (data_buf == MAP_FAILED) {
perror(argv[3]); perror(argv[3]);
close(fd);
return 1; return 1;
} }
close(fd); close(fd);

Просмотреть файл

@ -116,8 +116,9 @@ int main(int argc, char **argv)
} }
size = st.st_size; size = st.st_size;
map = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); map = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
if (-1 == (int)(long)map) { if (map == MAP_FAILED) {
perror("mmap"); perror("mmap");
close(fd);
exit(1); exit(1);
} }
close(fd); close(fd);