Eliminate lseek() with unchecked return in tor_mmap_file()

This commit is contained in:
Andrea Shepard 2014-03-19 11:17:55 -07:00 committed by Nick Mathewson
parent abdf1878a3
commit 0938c20fa3

View File

@ -35,6 +35,12 @@
#ifdef HAVE_UNAME #ifdef HAVE_UNAME
#include <sys/utsname.h> #include <sys/utsname.h>
#endif #endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_UNISTD_H #ifdef HAVE_UNISTD_H
#include <unistd.h> #include <unistd.h>
#endif #endif
@ -178,9 +184,10 @@ tor_mmap_file(const char *filename)
{ {
int fd; /* router file */ int fd; /* router file */
char *string; char *string;
int page_size; int page_size, result;
tor_mmap_t *res; tor_mmap_t *res;
size_t size, filesize; size_t size, filesize;
struct stat st;
tor_assert(filename); tor_assert(filename);
@ -194,9 +201,22 @@ tor_mmap_file(const char *filename)
return NULL; return NULL;
} }
/* XXXX why not just do fstat here? */ /* Get the size of the file */
size = filesize = (size_t) lseek(fd, 0, SEEK_END); result = fstat(fd, &st);
lseek(fd, 0, SEEK_SET); if (result != 0) {
int save_errno = errno;
log_warn(LD_FS,
"Couldn't fstat opened descriptor for \"%s\" during mmap: %s",
filename, strerror(errno));
close(fd);
errno = save_errno;
return NULL;
}
size = filesize = (size_t)(st.st_size);
/*
* Should we check for weird crap like mmapping a named pipe here,
* or just wait for if (!size) below to fail?
*/
/* ensure page alignment */ /* ensure page alignment */
page_size = getpagesize(); page_size = getpagesize();
size += (size%page_size) ? page_size-(size%page_size) : 0; size += (size%page_size) ? page_size-(size%page_size) : 0;