SkJpegCodec: Gracefully handle malloc failure

Bug: b/150353577

If malloc fails, don't let SkJpegCodec attempt to decode into it.
Return kInternalError, which is what we return for OOM.

Change-Id: I6750e907c5e649d9e12ba75e84c8d9e91a66dd61
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/273809
Auto-Submit: Leon Scroggins <scroggo@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
diff --git a/src/codec/SkJpegCodec.cpp b/src/codec/SkJpegCodec.cpp
index 45bd35d..f79c8cc 100644
--- a/src/codec/SkJpegCodec.cpp
+++ b/src/codec/SkJpegCodec.cpp
@@ -553,7 +553,9 @@
         this->initializeSwizzler(dstInfo, options, true);
     }
 
-    this->allocateStorage(dstInfo);
+    if (!this->allocateStorage(dstInfo)) {
+        return kInternalError;
+    }
 
     int rows = this->readRows(dstInfo, dst, dstRowBytes, dstInfo.height(), options);
     if (rows < dstInfo.height()) {
@@ -564,7 +566,7 @@
     return kSuccess;
 }
 
-void SkJpegCodec::allocateStorage(const SkImageInfo& dstInfo) {
+bool SkJpegCodec::allocateStorage(const SkImageInfo& dstInfo) {
     int dstWidth = dstInfo.width();
 
     size_t swizzleBytes = 0;
@@ -582,11 +584,14 @@
 
     size_t totalBytes = swizzleBytes + xformBytes;
     if (totalBytes > 0) {
-        fStorage.reset(totalBytes);
+        if (!fStorage.reset(totalBytes)) {
+            return false;
+        }
         fSwizzleSrcRow = (swizzleBytes > 0) ? fStorage.get() : nullptr;
         fColorXformSrcRow = (xformBytes > 0) ?
                 SkTAddOffset<uint32_t>(fStorage.get(), swizzleBytes) : nullptr;
     }
+    return true;
 }
 
 void SkJpegCodec::initializeSwizzler(const SkImageInfo& dstInfo, const Options& options,
@@ -646,7 +651,9 @@
             fDecoderMgr->dinfo()->out_color_space, this->getEncodedInfo().profile(),
             this->colorXform());
     this->initializeSwizzler(this->dstInfo(), this->options(), needsCMYKToRGB);
-    this->allocateStorage(this->dstInfo());
+    if (!this->allocateStorage(this->dstInfo())) {
+        return nullptr;
+    }
     return fSwizzler.get();
 }
 
@@ -709,7 +716,9 @@
         this->initializeSwizzler(dstInfo, options, true);
     }
 
-    this->allocateStorage(dstInfo);
+    if (!this->allocateStorage(dstInfo)) {
+        return kInternalError;
+    }
 
     return kSuccess;
 }