Fixed a case of using new[]/delete[] when malloc/free should have been used.

--HG--
branch : minor
This commit is contained in:
Alex Szpakowski
2014-08-27 21:53:33 -03:00
parent b59e088d6c
commit 52b62e8620
3 changed files with 27 additions and 29 deletions
+2
View File
@@ -54,6 +54,8 @@ bool JPEGHandler::canDecode(love::filesystem::FileData *data)
if (!decompressor) if (!decompressor)
return false; return false;
love::thread::Lock lock(mutex);
int w, h, subsamp; int w, h, subsamp;
int status = tjDecompressHeader2(decompressor, int status = tjDecompressHeader2(decompressor,
(unsigned char *) data->getData(), (unsigned char *) data->getData(),
+23 -27
View File
@@ -33,6 +33,9 @@
// C++ // C++
#include <algorithm> #include <algorithm>
// C
#include <cstdlib>
namespace love namespace love
{ {
namespace image namespace image
@@ -46,39 +49,36 @@ static unsigned zlibDecompress(unsigned char **out, size_t *outsize, const unsig
{ {
int status = Z_OK; int status = Z_OK;
uLongf outdataSize = insize; uLongf outdatasize = insize;
size_t sizeMultiplier = 0; size_t sizemultiplier = 0;
unsigned char *outdata = nullptr; unsigned char *outdata = nullptr;
while (true) while (true)
{ {
// Enough size to hold the decompressed data, hopefully. // Enough size to hold the decompressed data, hopefully.
outdataSize = insize << (++sizeMultiplier); outdatasize = insize << (++sizemultiplier);
try // LodePNG uses malloc, realloc, and free.
{ outdata = (unsigned char *) malloc(outdatasize);
outdata = new unsigned char[outdataSize];
} if (!outdata)
catch (std::bad_alloc &)
{
return 83; // "Memory allocation failed" error code for LodePNG. return 83; // "Memory allocation failed" error code for LodePNG.
}
// Use zlib to decompress the PNG data. // Use zlib to decompress the PNG data.
status = uncompress(outdata, &outdataSize, in, insize); status = uncompress(outdata, &outdatasize, in, insize);
// If the out buffer was big enough, break out of the loop. // If the out buffer was big enough, break out of the loop.
if (status != Z_BUF_ERROR) if (status != Z_BUF_ERROR)
break; break;
// Otherwise delete the out buffer and try again with a larger size... // Otherwise delete the out buffer and try again with a larger size...
delete[] outdata; free(outdata);
outdata = nullptr; outdata = nullptr;
} }
if (status != Z_OK) if (status != Z_OK)
{ {
delete[] outdata; free(outdata);
return 10000; // "Unknown error code" for LodePNG. return 10000; // "Unknown error code" for LodePNG.
} }
@@ -86,34 +86,30 @@ static unsigned zlibDecompress(unsigned char **out, size_t *outsize, const unsig
*out = outdata; *out = outdata;
if (outsize != nullptr) if (outsize != nullptr)
*outsize = outdataSize; *outsize = outdatasize;
return 0; // Success. return 0; // Success.
} }
// Custom PNG compression function for LodePNG, using zlib. // Custom PNG compression function for LodePNG, using zlib.
static unsigned zlibCompress(unsigned char **out, size_t *outsize, const unsigned char *in, static unsigned zlibCompress(unsigned char **out, size_t *outsize, const unsigned char *in,
size_t insize, const LodePNGCompressSettings* /*settings*/) size_t insize, const LodePNGCompressSettings* /*settings*/)
{ {
// Get the maximum compressed size of the data. // Get the maximum compressed size of the data.
uLongf outdataSize = compressBound(insize); uLongf outdatasize = compressBound(insize);
unsigned char *outdata = nullptr;
try // LodePNG uses malloc, realloc, and free.
{ unsigned char *outdata = (unsigned char *) malloc(outdatasize);
outdata = new unsigned char[outdataSize];
} if (!outdata)
catch (std::bad_alloc &)
{
return 83; // "Memory allocation failed" error code for LodePNG. return 83; // "Memory allocation failed" error code for LodePNG.
}
// Use zlib to compress the PNG data. // Use zlib to compress the PNG data.
int status = compress(outdata, &outdataSize, in, insize); int status = compress(outdata, &outdatasize, in, insize);
if (status != Z_OK) if (status != Z_OK)
{ {
delete[] outdata; free(outdata);
return 10000; // "Unknown error code" for LodePNG. return 10000; // "Unknown error code" for LodePNG.
} }
@@ -121,7 +117,7 @@ static unsigned zlibCompress(unsigned char **out, size_t *outsize, const unsigne
*out = outdata; *out = outdata;
if (outsize != nullptr) if (outsize != nullptr)
*outsize = (size_t) outdataSize; *outsize = (size_t) outdatasize;
return 0; // Success. return 0; // Success.
} }
+2 -2
View File
@@ -65,8 +65,8 @@ FormatHandler::DecodedImage STBHandler::decode(love::filesystem::FileData *data)
int comp = 0; int comp = 0;
img.data = stbi_load_from_memory((const stbi_uc *) data->getData(), img.data = stbi_load_from_memory((const stbi_uc *) data->getData(),
(int) data->getSize(), (int) data->getSize(),
&img.width, &img.height, &img.width, &img.height,
&comp, 4); &comp, 4);
if (img.data == nullptr || img.width <= 0 || img.height <= 0) if (img.data == nullptr || img.width <= 0 || img.height <= 0)
throw love::Exception("Could not decode TGA or BMP image."); throw love::Exception("Could not decode TGA or BMP image.");