Skip to content

Commit

Permalink
[Issue apache#11912] throw mxnet exceptions when decoding invalid ima…
Browse files Browse the repository at this point in the history
…ges. (apache#12999)

* Raise an excption when passing an empty buffer to imdecode.

* src/io/image_io.cc: Check the length of the input buffer.
* tests/python/unittest/test_image.py: Update the (already existing) test to expect a mx.base.MXNetError.

* Raise an exception when passing an invalid data buffer to imdecode.

* src/io/image_io.cc: Raise an exception when the image could not be decoded instead of just logging.
* tests/python/unittest/test_image.py: Add a new test test_imdecode_invalid_image.

* Raise an exception when passing an invalid data buffer to imdecode.

* src/io/image_io.cc: Raise an exception when the image could not be decoded instead of just logging.
* tests/python/unittest/test_image.py: Add a new test test_imdecode_invalid_image.

* Rollback a "empty buffer" check in the image python bindings that's now more generally handled
in the core code.

* python/mxnet/image/image.py: remove buffer length check.
  • Loading branch information
lgov authored and Jose Luis Contreras committed Nov 13, 2018
1 parent 0cd47ea commit a220e07
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
4 changes: 0 additions & 4 deletions python/mxnet/image/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
9 changes: 4 additions & 5 deletions src/io/image_io.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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])) {
Expand Down
7 changes: 6 additions & 1 deletion tests/python/unittest/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a220e07

Please sign in to comment.