Addressed memory leaks in the MIME sniffer code. Fixes bug #1660.
git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@24604 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -13,6 +13,8 @@
|
|||||||
#include <stdio.h> // for SEEK_* defines
|
#include <stdio.h> // for SEEK_* defines
|
||||||
#include <new>
|
#include <new>
|
||||||
|
|
||||||
|
#include <AutoDeleter.h>
|
||||||
|
|
||||||
using namespace BPrivate::Storage::Sniffer;
|
using namespace BPrivate::Storage::Sniffer;
|
||||||
|
|
||||||
Pattern::Pattern(const std::string &string, const std::string &mask)
|
Pattern::Pattern(const std::string &string, const std::string &mask)
|
||||||
@@ -115,6 +117,7 @@ Pattern::Sniff(off_t start, off_t size, BPositionIO *data, bool caseInsensitive)
|
|||||||
off_t len = fString.length();
|
off_t len = fString.length();
|
||||||
char *buffer = new(nothrow) char[len+1];
|
char *buffer = new(nothrow) char[len+1];
|
||||||
if (buffer) {
|
if (buffer) {
|
||||||
|
ArrayDeleter<char> _(buffer);
|
||||||
ssize_t bytesRead = data->ReadAt(start, buffer, len);
|
ssize_t bytesRead = data->ReadAt(start, buffer, len);
|
||||||
// \todo If there are fewer bytes left in the data stream
|
// \todo If there are fewer bytes left in the data stream
|
||||||
// from the given position than the length of our data
|
// from the given position than the length of our data
|
||||||
@@ -160,6 +163,7 @@ Pattern::Sniff(off_t start, off_t size, BPositionIO *data, bool caseInsensitive)
|
|||||||
off_t len = fString.length();
|
off_t len = fString.length();
|
||||||
char *buffer = new(std::nothrow) char[len+1];
|
char *buffer = new(std::nothrow) char[len+1];
|
||||||
if (buffer) {
|
if (buffer) {
|
||||||
|
ArrayDeleter<char> _(buffer);
|
||||||
ssize_t bytesRead = data->ReadAt(start, buffer, len);
|
ssize_t bytesRead = data->ReadAt(start, buffer, len);
|
||||||
// \todo If there are fewer bytes left in the data stream
|
// \todo If there are fewer bytes left in the data stream
|
||||||
// from the given position than the length of our data
|
// from the given position than the length of our data
|
||||||
|
|||||||
@@ -163,14 +163,15 @@ SnifferRules::GuessMimeType(const entry_ref *ref, BString *type)
|
|||||||
if (bytes < 0)
|
if (bytes < 0)
|
||||||
err = bytes;
|
err = bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Next read that many bytes (or fewer, if the file isn't
|
// Next read that many bytes (or fewer, if the file isn't
|
||||||
// that long) into a buffer
|
// that long) into a buffer
|
||||||
if (!err) {
|
if (!err) {
|
||||||
buffer = new(std::nothrow) char[bytes];
|
buffer = new(std::nothrow) char[bytes];
|
||||||
if (!buffer)
|
if (!buffer)
|
||||||
err = B_NO_MEMORY;
|
err = B_NO_MEMORY;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!err)
|
if (!err)
|
||||||
err = file.SetTo(ref, B_READ_ONLY);
|
err = file.SetTo(ref, B_READ_ONLY);
|
||||||
if (!err) {
|
if (!err) {
|
||||||
@@ -182,7 +183,9 @@ SnifferRules::GuessMimeType(const entry_ref *ref, BString *type)
|
|||||||
// Now sniff the buffer
|
// Now sniff the buffer
|
||||||
if (!err)
|
if (!err)
|
||||||
err = GuessMimeType(&file, buffer, bytes, type);
|
err = GuessMimeType(&file, buffer, bytes, type);
|
||||||
|
|
||||||
|
delete[] buffer;
|
||||||
|
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user