You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2018/11/05 22:05:30 UTC

[GitHub] anirudh2290 closed pull request #12999: [Issue #11912] throw mxnet exceptions when decoding invalid images.

anirudh2290 closed pull request #12999: [Issue #11912] throw mxnet exceptions when decoding invalid images.
URL: https://github.com/apache/incubator-mxnet/pull/12999
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/python/mxnet/image/image.py b/python/mxnet/image/image.py
index b846700fb50..5c4f01e2fd2 100644
--- a/python/mxnet/image/image.py
+++ b/python/mxnet/image/image.py
@@ -140,10 +140,6 @@ def imdecode(buf, *args, **kwargs):
                              'if you would like to input type str, please convert to bytes')
         buf = nd.array(np.frombuffer(buf, dtype=np.uint8), dtype=np.uint8)
 
-    if len(buf) == 0:
-        # empty buf causes OpenCV crash.
-        raise ValueError("input buf cannot be empty.")
-
     return _internal._cvimdecode(buf, *args, **kwargs)
 
 
diff --git a/src/io/image_io.cc b/src/io/image_io.cc
index a996a2208d7..b3f7c40b2b1 100644
--- a/src/io/image_io.cc
+++ b/src/io/image_io.cc
@@ -143,11 +143,8 @@ void ImdecodeImpl(int flag, bool to_rgb, void* data, size_t size,
   cv::Mat dst;
   if (out->is_none()) {
     cv::Mat res = cv::imdecode(buf, flag);
-    if (res.empty()) {
-      LOG(INFO) << "Decoding failed. Invalid image file.";
-      *out = NDArray();
-      return;
-    }
+    CHECK(!res.empty()) << "Decoding failed. Invalid image file.";
+
     *out = NDArray(mshadow::Shape3(res.rows, res.cols, flag == 0 ? 1 : 3),
                    Context::CPU(), false, mshadow::kUint8);
     dst = cv::Mat(out->shape()[0], out->shape()[1], flag == 0 ? CV_8U : CV_8UC3,
@@ -189,6 +186,8 @@ void Imdecode(const nnvm::NodeAttrs& attrs,
 
   uint8_t* str_img = inputs[0].data().dptr<uint8_t>();
   size_t len = inputs[0].shape().Size();
+  CHECK(len > 0) << "Input cannot be an empty buffer";
+
   TShape oshape(3);
   oshape[2] = param.flag == 0 ? 1 : 3;
   if (get_jpeg_size(str_img, len, &oshape[1], &oshape[0])) {
diff --git a/tests/python/unittest/test_image.py b/tests/python/unittest/test_image.py
index c8022b67bee..3e35d6de2bc 100644
--- a/tests/python/unittest/test_image.py
+++ b/tests/python/unittest/test_image.py
@@ -104,10 +104,15 @@ def test_imdecode_bytearray(self):
             cv_image = cv2.imread(img)
             assert_almost_equal(image.asnumpy(), cv_image)
 
-    @raises(ValueError)
+    @raises(mx.base.MXNetError)
     def test_imdecode_empty_buffer(self):
         mx.image.imdecode(b'', to_rgb=0)
 
+    @raises(mx.base.MXNetError)
+    def test_imdecode_invalid_image(self):
+        image = mx.image.imdecode(b'clearly not image content')
+        assert_equal(image, None)
+
     def test_scale_down(self):
         assert mx.image.scale_down((640, 480), (720, 120)) == (640, 106)
         assert mx.image.scale_down((360, 1000), (480, 500)) == (360, 375)


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services