Bug 514033 - Error recovery for imagelib - part 16 - Eliminate checks for infallible malloc, use infallible malloc when we need it.r=joe,a=blocker
authorBobby Holley <bobbyholley@gmail.com>
Sun, 12 Sep 2010 08:22:31 -0700
changeset 53676 389e836517bc1f49b3ba57beb132ca8986ce84e3
parent 53675 06346509f9fc78d3396d1b902c616150a3f3485c
child 53677 fc4dc7ff4e9ad6b0d8bbf76e326df5730dedd8ea
push idunknown
push userunknown
push dateunknown
reviewersjoe, blocker
bugs514033
milestone2.0b6pre
Bug 514033 - Error recovery for imagelib - part 16 - Eliminate checks for infallible malloc, use infallible malloc when we need it.r=joe,a=blocker
modules/libpr0n/decoders/GIF2.h
modules/libpr0n/decoders/nsBMPDecoder.cpp
modules/libpr0n/decoders/nsGIFDecoder2.cpp
modules/libpr0n/decoders/nsICODecoder.cpp
modules/libpr0n/decoders/nsPNGDecoder.cpp
--- a/modules/libpr0n/decoders/GIF2.h
+++ b/modules/libpr0n/decoders/GIF2.h
@@ -65,17 +65,16 @@ typedef enum {
     gif_lzw_start,
     gif_lzw,
     gif_sub_block,
     gif_extension,
     gif_control_extension,
     gif_consume_block,
     gif_skip_block,
     gif_done,
-    gif_oom,
     gif_error,
     gif_comment_extension,
     gif_application_extension,
     gif_netscape_extension_block,
     gif_consume_netscape_extension,
     gif_consume_comment
 } gstate;
 
--- a/modules/libpr0n/decoders/nsBMPDecoder.cpp
+++ b/modules/libpr0n/decoders/nsBMPDecoder.cpp
@@ -207,38 +207,33 @@ nsBMPDecoder::WriteInternal(const char* 
 
         if (mBIH.bpp <= 8) {
             mNumColors = 1 << mBIH.bpp;
             if (mBIH.colors && mBIH.colors < mNumColors)
                 mNumColors = mBIH.colors;
 
             // Always allocate 256 even though mNumColors might be smaller
             mColors = new colorTable[256];
-            if (!mColors) {
-                PostDecoderError(NS_ERROR_OUT_OF_MEMORY);
-                return;
-            }
-
             memset(mColors, 0, 256 * sizeof(colorTable));
         }
         else if (mBIH.compression != BI_BITFIELDS && mBIH.bpp == 16) {
             // Use default 5-5-5 format
             mBitFields.red   = 0x7C00;
             mBitFields.green = 0x03E0;
             mBitFields.blue  = 0x001F;
             CalcBitShift();
         }
 
         PRUint32 imageLength;
         if ((mBIH.compression == BI_RLE8) || (mBIH.compression == BI_RLE4)) {
             rv = mImage->AppendFrame(0, 0, mBIH.width, real_height, gfxASurface::ImageFormatARGB32,
                                      (PRUint8**)&mImageData, &imageLength);
         } else {
             // mRow is not used for RLE encoded images
-            mRow = (PRUint8*)malloc((mBIH.width * mBIH.bpp)/8 + 4);
+            mRow = (PRUint8*)moz_malloc((mBIH.width * mBIH.bpp)/8 + 4);
             // +4 because the line is padded to a 4 bit boundary, but I don't want
             // to make exact calculations here, that's unnecessary.
             // Also, it compensates rounding error.
             if (!mRow) {
                 PostDecoderError(NS_ERROR_OUT_OF_MEMORY);
                 return;
             }
             rv = mImage->AppendFrame(0, 0, mBIH.width, real_height, gfxASurface::ImageFormatRGB24,
--- a/modules/libpr0n/decoders/nsGIFDecoder2.cpp
+++ b/modules/libpr0n/decoders/nsGIFDecoder2.cpp
@@ -972,21 +972,17 @@ nsGIFDecoder2::WriteInternal(const char 
       if (q[8] & 0x80) /* has a local colormap? */
       {
         mGIFStruct.local_colormap_size = 1 << depth;
         if (!mGIFStruct.images_decoded) {
           // First frame has local colormap, allocate space for it
           // as the image frame doesn't have its own palette
           mColormapSize = sizeof(PRUint32) << realDepth;
           if (!mGIFStruct.local_colormap) {
-            mGIFStruct.local_colormap = (PRUint32*)PR_MALLOC(mColormapSize);
-            if (!mGIFStruct.local_colormap) {
-              mGIFStruct.state = gif_oom;
-              break;
-            }
+            mGIFStruct.local_colormap = (PRUint32*)moz_xmalloc(mColormapSize);
           }
           mColormap = mGIFStruct.local_colormap;
         }
         const PRUint32 size = 3 << depth;
         if (mColormapSize > size) {
           // Clear the notfilled part of the colormap
           memset(((PRUint8*)mColormap) + size, 0, mColormapSize - size);
         }
@@ -1053,21 +1049,16 @@ nsGIFDecoder2::WriteInternal(const char 
       PostDecodeDone();
       mGIFOpen = PR_FALSE;
       goto done;
 
     case gif_error:
       PostDataError();
       return;
 
-    // Handle out of memory errors
-    case gif_oom:
-      PostDecoderError(NS_ERROR_OUT_OF_MEMORY);
-      return;
-
     // We shouldn't ever get here.
     default:
       break;
     }
   }
 
   // if an error state is set but no data remains, code flow reaches here
   if (mGIFStruct.state == gif_error) {
--- a/modules/libpr0n/decoders/nsICODecoder.cpp
+++ b/modules/libpr0n/decoders/nsICODecoder.cpp
@@ -230,37 +230,33 @@ nsICODecoder::WriteInternal(const char* 
           mNumColors = 256;
           break;
         default:
           PostDataError();
           return;
       }
 
       mColors = new colorTable[mNumColors];
-      if (!mColors) {
-        PostDecoderError(NS_ERROR_OUT_OF_MEMORY);
-        return;
-      }
     }
 
     if (mIsCursor) {
       nsCOMPtr<nsISupportsPRUint32> intwrapx = do_CreateInstance("@mozilla.org/supports-PRUint32;1");
       nsCOMPtr<nsISupportsPRUint32> intwrapy = do_CreateInstance("@mozilla.org/supports-PRUint32;1");
 
       if (intwrapx && intwrapy) {
         intwrapx->SetData(mDirEntry.mXHotspot);
         intwrapy->SetData(mDirEntry.mYHotspot);
 
         mImage->Set("hotspotX", intwrapx);
         mImage->Set("hotspotY", intwrapy);
       }
     }
 
     mCurLine = mDirEntry.mHeight;
-    mRow = (PRUint8*)malloc((mDirEntry.mWidth * mBIH.bpp)/8 + 4);
+    mRow = (PRUint8*)moz_malloc((mDirEntry.mWidth * mBIH.bpp)/8 + 4);
     // +4 because the line is padded to a 4 bit boundary, but I don't want
     // to make exact calculations here, that's unnecessary.
     // Also, it compensates rounding error.
     if (!mRow) {
       PostDecoderError(NS_ERROR_OUT_OF_MEMORY);
       return;
     }
 
--- a/modules/libpr0n/decoders/nsPNGDecoder.cpp
+++ b/modules/libpr0n/decoders/nsPNGDecoder.cpp
@@ -233,21 +233,17 @@ nsPNGDecoder::InitInternal()
         115,  80,  76,  84, '\0',   /* sPLT */
         116,  69,  88, 116, '\0',   /* tEXt */
         116,  73,  77,  69, '\0',   /* tIME */
         122,  84,  88, 116, '\0'};  /* zTXt */
 #endif
 
   // For size decodes, we only need a small buffer
   if (IsSizeDecode()) {
-    mHeaderBuf = (PRUint8 *)nsMemory::Alloc(BYTES_NEEDED_FOR_DIMENSIONS);
-    if (!mHeaderBuf) {
-      PostDecoderError(NS_ERROR_OUT_OF_MEMORY);
-      return;
-    }
+    mHeaderBuf = (PRUint8 *)moz_xmalloc(BYTES_NEEDED_FOR_DIMENSIONS);
     return;
   }
 
   /* For full decodes, do png init stuff */
 
   /* Initialize the container's source image header. */
   /* Always decode to 24 bit pixdepth */
 
@@ -619,26 +615,25 @@ nsPNGDecoder::info_callback(png_structp 
 #ifdef PNG_APNG_SUPPORTED
   }
 #endif
 
   if (decoder->mTransform &&
       (channels <= 2 || interlace_type == PNG_INTERLACE_ADAM7)) {
     PRUint32 bpp[] = { 0, 3, 4, 3, 4 };
     decoder->mCMSLine =
-      (PRUint8 *)nsMemory::Alloc(bpp[channels] * width);
+      (PRUint8 *)moz_malloc(bpp[channels] * width);
     if (!decoder->mCMSLine) {
       longjmp(png_jmpbuf(decoder->mPNG), 5); // NS_ERROR_OUT_OF_MEMORY
     }
   }
 
   if (interlace_type == PNG_INTERLACE_ADAM7) {
     if (height < PR_INT32_MAX / (width * channels))
-      decoder->interlacebuf = (PRUint8 *)nsMemory::Alloc(channels *
-                                                         width * height);
+      decoder->interlacebuf = (PRUint8 *)moz_malloc(channels * width * height);
     if (!decoder->interlacebuf) {
       longjmp(png_jmpbuf(decoder->mPNG), 5); // NS_ERROR_OUT_OF_MEMORY
     }
   }
 
   /* Reject any ancillary chunk after IDAT with a bad CRC (bug #397593).
    * It would be better to show the default frame (if one has already been
    * successfully decoded) before bailing, but it's simpler to just bail