]> granicus.if.org Git - taglib/commitdiff
Make sure that buffer allocations for file reads aren't completely bogus.
authorScott Wheeler <wheeler@kde.org>
Sat, 23 Jul 2005 23:49:29 +0000 (23:49 +0000)
committerScott Wheeler <wheeler@kde.org>
Sat, 23 Jul 2005 23:49:29 +0000 (23:49 +0000)
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

index 7994efdd5857db3d777a09f70cbf4bcaff1887fb..464ba28a4ea5a47b5dd4980ffc276f04fa144e7b 100644 (file)
@@ -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<uint>(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;
 }