Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

[Issue #11912] throw mxnet exceptions when decoding invalid images. #12999

Merged
merged 5 commits into from
Nov 5, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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