From 52b62e862011da7a79a6ed0a57a184030c117411 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Wed, 27 Aug 2014 21:53:33 -0300 Subject: [PATCH] Fixed a case of using new[]/delete[] when malloc/free should have been used. --HG-- branch : minor --- src/modules/image/magpie/JPEGHandler.cpp | 2 + src/modules/image/magpie/PNGHandler.cpp | 50 +++++++++++------------- src/modules/image/magpie/STBHandler.cpp | 4 +- 3 files changed, 27 insertions(+), 29 deletions(-) diff --git a/src/modules/image/magpie/JPEGHandler.cpp b/src/modules/image/magpie/JPEGHandler.cpp index 549505257..bde83965b 100644 --- a/src/modules/image/magpie/JPEGHandler.cpp +++ b/src/modules/image/magpie/JPEGHandler.cpp @@ -54,6 +54,8 @@ bool JPEGHandler::canDecode(love::filesystem::FileData *data) if (!decompressor) return false; + love::thread::Lock lock(mutex); + int w, h, subsamp; int status = tjDecompressHeader2(decompressor, (unsigned char *) data->getData(), diff --git a/src/modules/image/magpie/PNGHandler.cpp b/src/modules/image/magpie/PNGHandler.cpp index 1d45365cc..5f57c34e3 100644 --- a/src/modules/image/magpie/PNGHandler.cpp +++ b/src/modules/image/magpie/PNGHandler.cpp @@ -33,6 +33,9 @@ // C++ #include +// C +#include + namespace love { namespace image @@ -46,39 +49,36 @@ static unsigned zlibDecompress(unsigned char **out, size_t *outsize, const unsig { int status = Z_OK; - uLongf outdataSize = insize; - size_t sizeMultiplier = 0; + uLongf outdatasize = insize; + size_t sizemultiplier = 0; unsigned char *outdata = nullptr; while (true) { // Enough size to hold the decompressed data, hopefully. - outdataSize = insize << (++sizeMultiplier); + outdatasize = insize << (++sizemultiplier); - try - { - outdata = new unsigned char[outdataSize]; - } - catch (std::bad_alloc &) - { + // LodePNG uses malloc, realloc, and free. + outdata = (unsigned char *) malloc(outdatasize); + + if (!outdata) return 83; // "Memory allocation failed" error code for LodePNG. - } // 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 (status != Z_BUF_ERROR) break; // Otherwise delete the out buffer and try again with a larger size... - delete[] outdata; + free(outdata); outdata = nullptr; } if (status != Z_OK) { - delete[] outdata; + free(outdata); return 10000; // "Unknown error code" for LodePNG. } @@ -86,34 +86,30 @@ static unsigned zlibDecompress(unsigned char **out, size_t *outsize, const unsig *out = outdata; if (outsize != nullptr) - *outsize = outdataSize; + *outsize = outdatasize; return 0; // Success. } // Custom PNG compression function for LodePNG, using zlib. 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. - uLongf outdataSize = compressBound(insize); - unsigned char *outdata = nullptr; + uLongf outdatasize = compressBound(insize); - try - { - outdata = new unsigned char[outdataSize]; - } - catch (std::bad_alloc &) - { + // LodePNG uses malloc, realloc, and free. + unsigned char *outdata = (unsigned char *) malloc(outdatasize); + + if (!outdata) return 83; // "Memory allocation failed" error code for LodePNG. - } // 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) { - delete[] outdata; + free(outdata); return 10000; // "Unknown error code" for LodePNG. } @@ -121,7 +117,7 @@ static unsigned zlibCompress(unsigned char **out, size_t *outsize, const unsigne *out = outdata; if (outsize != nullptr) - *outsize = (size_t) outdataSize; + *outsize = (size_t) outdatasize; return 0; // Success. } diff --git a/src/modules/image/magpie/STBHandler.cpp b/src/modules/image/magpie/STBHandler.cpp index f7b1b66a2..3b0ba64e1 100644 --- a/src/modules/image/magpie/STBHandler.cpp +++ b/src/modules/image/magpie/STBHandler.cpp @@ -65,8 +65,8 @@ FormatHandler::DecodedImage STBHandler::decode(love::filesystem::FileData *data) int comp = 0; img.data = stbi_load_from_memory((const stbi_uc *) data->getData(), (int) data->getSize(), - &img.width, &img.height, - &comp, 4); + &img.width, &img.height, + &comp, 4); if (img.data == nullptr || img.width <= 0 || img.height <= 0) throw love::Exception("Could not decode TGA or BMP image.");