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
This commit is contained in:
Scott Wheeler
2005-07-23 23:49:29 +00:00
parent 59fd610999
commit 111b0dc6eb

View 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;
}