Bug 1277397 - High memory usage triggered by a jpg draft
authorSamathy Barratt <samathy@sbarratt.co.uk>
Fri, 08 Dec 2017 16:39:01 +0000
changeset 709716 77bb7d785863450025ebe05d47962e8398a931e3
parent 709659 45e436176d70f76ab28b72a5b740b1b8ff9bf0b6
child 743494 5e9130afd62d72899bffce80324e76414218e586
push id92728
push userbmo:samathy@sbarratt.co.uk
push dateFri, 08 Dec 2017 16:39:57 +0000
bugs1277397
milestone59.0a1
Bug 1277397 - High memory usage triggered by a jpg If an image's size is larger than the SurfaceCache maximum capacity limit, load it sequentially rather than progressivly. MozReview-Commit-ID: Eskyw6Xk812
image/decoders/nsJPEGDecoder.cpp
image/decoders/nsJPEGDecoder.h
--- a/image/decoders/nsJPEGDecoder.cpp
+++ b/image/decoders/nsJPEGDecoder.cpp
@@ -373,32 +373,29 @@ nsJPEGDecoder::ReadJPEGData(const char* 
         default:
           mState = JPEG_ERROR;
           MOZ_LOG(sJPEGDecoderAccountingLog, LogLevel::Debug,
                  ("} (unknown colorpsace (3))"));
           return Transition::TerminateFailure();
       }
     }
 
+    //If the memory needed by the image is bigger than the SurfaceCache size limit,
+    //Decode sequentially and don't make this a buffered image.
+    if ( ((mInfo.image_width * mInfo.image_height) *24) >
+        SurfaceCache::MaximumCapacity()) 
+    {
+	    mDecodeStyle = SEQUENTIAL;
+    }
+
     // Don't allocate a giant and superfluous memory buffer
     // when not doing a progressive decode.
     mInfo.buffered_image = mDecodeStyle == PROGRESSIVE &&
                            jpeg_has_multiple_scans(&mInfo);
 
-    //If the memory needed by the image is bigger than the SurfaceCache size limit,
-    // & we're a buffered image, don't start decompressing. Bail out.
-    if ( mInfo.buffered_image && ((mInfo.image_width * mInfo.image_height) *24) >
-        SurfaceCache::MaximumCapacity()) {
-     mState= JPEG_ERROR;
-     MOZ_LOG(sJPEGDecoderAccountingLog, LogLevel::Debug,
-            ("} (Image size is larger than SurfaceCache::MaximumCapacity"));
-     return Transition::TerminateFailure();
-    }
-
-
     /* Used to set up image size so arrays can be allocated */
     jpeg_calc_output_dimensions(&mInfo);
 
     MOZ_ASSERT(!mImageData, "Already have a buffer allocated?");
     nsresult rv = AllocateFrame(/* aFrameNum = */ 0, OutputSize(),
                                 FullOutputFrame(), SurfaceFormat::B8G8R8X8);
     if (NS_FAILED(rv)) {
       mState = JPEG_ERROR;
--- a/image/decoders/nsJPEGDecoder.h
+++ b/image/decoders/nsJPEGDecoder.h
@@ -102,17 +102,17 @@ public:
   JOCTET * mProfile;
   uint32_t mProfileLength;
 
   qcms_profile* mInProfile;
   qcms_transform* mTransform;
 
   bool mReading;
 
-  const Decoder::DecodeStyle mDecodeStyle;
+  Decoder::DecodeStyle mDecodeStyle;
 
   uint32_t mCMSMode;
 };
 
 } // namespace image
 } // namespace mozilla
 
 #endif // mozilla_image_decoders_nsJPEGDecoder_h