diff options
author | Andrea Shepard <andrea@torproject.org> | 2014-03-19 11:17:55 -0700 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2014-03-31 11:27:08 -0400 |
commit | 0938c20fa3b44a09d20ba2eb05775b0c46d39129 (patch) | |
tree | 3f9a09906c1670131cf88f814d0e23aff366b56a /src/common/compat.c | |
parent | abdf1878a3f8fe366d8bb7f649127880bdbdf82d (diff) | |
download | tor-0938c20fa3b44a09d20ba2eb05775b0c46d39129.tar.gz tor-0938c20fa3b44a09d20ba2eb05775b0c46d39129.zip |
Eliminate lseek() with unchecked return in tor_mmap_file()
Diffstat (limited to 'src/common/compat.c')
-rw-r--r-- | src/common/compat.c | 28 |
1 files changed, 24 insertions, 4 deletions
diff --git a/src/common/compat.c b/src/common/compat.c index b43af888b0..ead96ca30e 100644 --- a/src/common/compat.c +++ b/src/common/compat.c @@ -35,6 +35,12 @@ #ifdef HAVE_UNAME #include <sys/utsname.h> #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 #include <unistd.h> #endif @@ -178,9 +184,10 @@ tor_mmap_file(const char *filename) { int fd; /* router file */ char *string; - int page_size; + int page_size, result; tor_mmap_t *res; size_t size, filesize; + struct stat st; tor_assert(filename); @@ -194,9 +201,22 @@ tor_mmap_file(const char *filename) return NULL; } - /* XXXX why not just do fstat here? */ - size = filesize = (size_t) lseek(fd, 0, SEEK_END); - lseek(fd, 0, SEEK_SET); + /* Get the size of the file */ + result = fstat(fd, &st); + 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 */ page_size = getpagesize(); size += (size%page_size) ? page_size-(size%page_size) : 0; |