From 022ba32c1ab58ff6935c8f147f5cfe247cb5402d Mon Sep 17 00:00:00 2001 From: "Markus F.X.J. Oberhumer" Date: Thu, 6 Oct 2016 12:11:32 +0200 Subject: [PATCH] Improve robustness of seek() by adding some sanity checks. --- src/file.cpp | 12 ++++++++---- src/file.h | 6 +++--- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/file.cpp b/src/file.cpp index c2752624..b6042017 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -168,8 +168,10 @@ void FileBase::write(const void *buf, int len) } -off_t FileBase::seek(off_t off, int whence) +off_t FileBase::seek(upx_int64_t off64, int whence) { + (void) mem_size(1, off64 >= 0 ? off64 : -off64); // sanity check + off_t off = ACC_ICONV(off_t, off64); if (!isOpen()) throwIOException("bad seek 1"); if (whence == SEEK_SET) { @@ -285,9 +287,9 @@ int InputFile::readx(MemBuffer &buf, int len) } -off_t InputFile::seek(off_t off, int whence) +off_t InputFile::seek(upx_int64_t off64, int whence) { - off_t pos = super::seek(off,whence); + off_t pos = super::seek(off64, whence); if (_length < pos) throwIOException("bad seek 4"); return pos; @@ -402,8 +404,10 @@ void OutputFile::rewrite(const void *buf, int len) bytes_written -= len; // restore } -off_t OutputFile::seek(off_t off, int whence) +off_t OutputFile::seek(upx_int64_t off64, int whence) { + (void) mem_size(1, off64 >= 0 ? off64 : -off64); // sanity check + off_t off = ACC_ICONV(off_t, off64); assert(!opt->to_stdout); switch (whence) { case SEEK_SET: { diff --git a/src/file.h b/src/file.h index d895efd6..0ddd84a6 100644 --- a/src/file.h +++ b/src/file.h @@ -67,7 +67,7 @@ protected: virtual int read(void *buf, int len); virtual int readx(void *buf, int len); virtual void write(const void *buf, int len); - virtual off_t seek(off_t off, int whence); + virtual off_t seek(upx_int64_t off, int whence); virtual off_t tell() const; int _fd; @@ -106,7 +106,7 @@ public: virtual int read(MemBuffer &buf, int len); virtual int readx(MemBuffer &buf, int len); - virtual off_t seek(off_t off, int whence); + virtual off_t seek(upx_int64_t off, int whence); virtual off_t tell() const; virtual off_t st_size_orig() const; protected: @@ -142,7 +142,7 @@ public: virtual off_t st_size() const; // { return _length; } // FIXME - these won't work when using the '--stdout' option - virtual off_t seek(off_t off, int whence); + virtual off_t seek(upx_int64_t off, int whence); virtual void rewrite(const void *buf, int len); // util