From 111b0dc6eb5f3804f4f814cd3d9a5476b52d8367 Mon Sep 17 00:00:00 2001 From: Scott Wheeler Date: Sat, 23 Jul 2005 23:49:29 +0000 Subject: [PATCH] Make sure that buffer allocations for file reads aren't completely bogus. Specifically make sure that we don't actually allocate a buffer for a read that extends beyond the end of the file. BUG:101401 git-svn-id: svn://anonsvn.kde.org/home/kde/trunk/kdesupport/taglib@438035 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- toolkit/tfile.cpp | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/toolkit/tfile.cpp b/toolkit/tfile.cpp index 7994efdd..464ba28a 100644 --- a/toolkit/tfile.cpp +++ b/toolkit/tfile.cpp @@ -36,7 +36,8 @@ public: file(0), name(fileName), readOnly(true), - valid(true) + valid(true), + size(0) {} ~FilePrivate() @@ -48,6 +49,7 @@ public: const char *name; bool readOnly; bool valid; + ulong size; static const uint bufferSize = 1024; }; @@ -85,6 +87,12 @@ ByteVector File::readBlock(ulong length) return ByteVector::null; } + if(length > FilePrivate::bufferSize && + length > ulong(File::length())) + { + length = File::length(); + } + ByteVector v(static_cast(length)); const int count = fread(v.data(), sizeof(char), length, d->file); v.resize(count); @@ -446,8 +454,13 @@ long File::tell() const long File::length() { + // Do some caching in case we do multiple calls. + + if(d->size > 0) + return d->size; + if(!d->file) - return 0; + return 0; long curpos = tell(); @@ -455,7 +468,8 @@ long File::length() long endpos = tell(); seek(curpos, Beginning); - + + d->size = endpos; return endpos; } -- 2.40.0