PNGTranslator: Fix PVS 2237-2241

* Use new(std::nothrow) so that NULL check for allocated memory makes sense.
* Other new() without NULL check is replaced with new(std::nothrow).

Change-Id: I23df98e58ea1960463e86a75ff69d67855f59074
Reviewed-on: https://review.haiku-os.org/690
Reviewed-by: Adrien Destugues <[email protected]>
This commit is contained in:
Murai Takashi
2018-11-13 19:58:08 +00:00
committed by waddlesplash
parent 99b0a7e0ed
commit 6c8b0b3724
+12 -11
View File
@@ -133,7 +133,7 @@ BTranslator *
make_nth_translator(int32 n, image_id you, uint32 flags, ...) make_nth_translator(int32 n, image_id you, uint32 flags, ...)
{ {
if (!n) if (!n)
return new PNGTranslator(); return new(std::nothrow) PNGTranslator();
else else
return NULL; return NULL;
} }
@@ -193,7 +193,7 @@ pngcb_flush_data(png_structp ppng)
// Returns: // Returns:
// --------------------------------------------------------------- // ---------------------------------------------------------------
PNGTranslator::PNGTranslator() PNGTranslator::PNGTranslator()
: BaseTranslator(B_TRANSLATE("PNG images"), : BaseTranslator(B_TRANSLATE("PNG images"),
B_TRANSLATE("PNG image translator"), B_TRANSLATE("PNG image translator"),
PNG_TRANSLATOR_VERSION, PNG_TRANSLATOR_VERSION,
sInputFormats, kNumInputFormats, sInputFormats, kNumInputFormats,
@@ -433,7 +433,7 @@ PNGTranslator::translate_from_png_to_bits(BPositionIO *inSource,
if (interlace_type == PNG_INTERLACE_NONE) { if (interlace_type == PNG_INTERLACE_NONE) {
// allocate buffer for storing PNG row // allocate buffer for storing PNG row
prow = new uint8[rowbytes]; prow = new(std::nothrow) uint8[rowbytes];
if (!prow) { if (!prow) {
result = B_NO_MEMORY; result = B_NO_MEMORY;
break; break;
@@ -452,14 +452,14 @@ PNGTranslator::translate_from_png_to_bits(BPositionIO *inSource,
} else { } else {
// interlaced PNG image // interlaced PNG image
prows = new uint8 *[height]; prows = new(std::nothrow) uint8 *[height];
if (!prows) { if (!prows) {
result = B_NO_MEMORY; result = B_NO_MEMORY;
break; break;
} }
// allocate enough memory to store the whole image // allocate enough memory to store the whole image
for (nalloc = 0; nalloc < height; nalloc++) { for (nalloc = 0; nalloc < height; nalloc++) {
prows[nalloc] = new uint8[rowbytes]; prows[nalloc] = new(std::nothrow) uint8[rowbytes];
if (!prows[nalloc]) if (!prows[nalloc])
break; break;
} }
@@ -853,26 +853,27 @@ PNGTranslator::translate_from_bits_to_png(BPositionIO *inSource,
pngcb_write_data, pngcb_flush_data); pngcb_write_data, pngcb_flush_data);
// Allocate memory needed to buffer image data // Allocate memory needed to buffer image data
pbitsrow = new uint8[bitsHeader.rowBytes]; pbitsrow = new(std::nothrow) uint8[bitsHeader.rowBytes];
if (!pbitsrow) { if (!pbitsrow) {
result = B_NO_MEMORY; result = B_NO_MEMORY;
break; break;
} }
if (interlace_type == PNG_INTERLACE_NONE) { if (interlace_type == PNG_INTERLACE_NONE) {
prow = new uint8[width * pngBytesPerPixel]; prow = new(std::nothrow) uint8[width * pngBytesPerPixel];
if (!prow) { if (!prow) {
result = B_NO_MEMORY; result = B_NO_MEMORY;
break; break;
} }
} else { } else {
prows = new uint8 *[height]; prows = new(std::nothrow) uint8 *[height];
if (!prows) { if (!prows) {
result = B_NO_MEMORY; result = B_NO_MEMORY;
break; break;
} }
// allocate enough memory to store the whole image // allocate enough memory to store the whole image
for (nalloc = 0; nalloc < height; nalloc++) { for (nalloc = 0; nalloc < height; nalloc++) {
prows[nalloc] = new uint8[width * pngBytesPerPixel]; prows[nalloc] =
new(std::nothrow) uint8[width * pngBytesPerPixel];
if (!prows[nalloc]) if (!prows[nalloc])
break; break;
} }
@@ -1001,8 +1002,8 @@ PNGTranslator::DerivedTranslate(BPositionIO *inSource,
BView * BView *
PNGTranslator::NewConfigView(TranslatorSettings *settings) PNGTranslator::NewConfigView(TranslatorSettings *settings)
{ {
return new PNGView(BRect(0, 0, PNG_VIEW_WIDTH, PNG_VIEW_HEIGHT), return new(std::nothrow) PNGView(BRect(0, 0, PNG_VIEW_WIDTH, PNG_VIEW_HEIGHT),
B_TRANSLATE("PNGTranslator Settings"), B_FOLLOW_ALL, B_TRANSLATE("PNGTranslator Settings"), B_FOLLOW_ALL,
B_WILL_DRAW, settings); B_WILL_DRAW, settings);
} }