From 183060d729645587be8adb916bd79db945e95d1c Mon Sep 17 00:00:00 2001 From: Scott Wheeler Date: Wed, 18 Jul 2007 16:41:10 +0000 Subject: [PATCH] Make sure that we only write the number of bytes that we read. BUG:122698 git-svn-id: svn://anonsvn.kde.org/home/kde/trunk/kdesupport/taglib@689596 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- taglib/toolkit/tfile.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/taglib/toolkit/tfile.cpp b/taglib/toolkit/tfile.cpp index 58533fe0..2fa76077 100644 --- a/taglib/toolkit/tfile.cpp +++ b/taglib/toolkit/tfile.cpp @@ -325,6 +325,7 @@ void File::insert(const ByteVector &data, ulong start, ulong replace) // that aren't yet in memory, so this is necessary. ulong bufferLength = bufferSize(); + while(data.size() - replace > bufferLength) bufferLength += bufferSize(); @@ -352,10 +353,14 @@ void File::insert(const ByteVector &data, ulong start, ulong replace) buffer = aboutToOverwrite; + // In case we've already reached the end of file... + + buffer.resize(bytesRead); + // Ok, here's the main loop. We want to loop until the read fails, which // means that we hit the end of the file. - while(bytesRead != 0) { + while(!buffer.isEmpty()) { // Seek to the current read position and read the data that we're about // to overwrite. Appropriately increment the readPosition. @@ -375,8 +380,8 @@ void File::insert(const ByteVector &data, ulong start, ulong replace) // writePosition. seek(writePosition); - fwrite(buffer.data(), sizeof(char), bufferLength, d->file); - writePosition += bufferLength; + fwrite(buffer.data(), sizeof(char), buffer.size(), d->file); + writePosition += buffer.size(); // Make the current buffer the data that we read in the beginning.