diff --git a/intrinsiccv/include/intrinsiccv/morphology/workspace.h b/intrinsiccv/include/intrinsiccv/morphology/workspace.h index 3132c9054c865938b425bd9396bc9e8c880c555b..9eff46566e58539e7e4dac43e3875b4e2604ba8e 100644 --- a/intrinsiccv/include/intrinsiccv/morphology/workspace.h +++ b/intrinsiccv/include/intrinsiccv/morphology/workspace.h @@ -70,6 +70,8 @@ class MorphologyWorkspace final { // To avoid load/store penalties. const size_t kAlignment = 16; + CHECK_IMAGE_SIZE(image.width, image.height); + if (anchor.x >= kernel.width || anchor.y >= kernel.height) { return INTRINSICCV_ERROR_RANGE; } diff --git a/intrinsiccv/include/intrinsiccv/utils.h b/intrinsiccv/include/intrinsiccv/utils.h index c6d4652a9fc80f35eb587ffbdd3a8c639ae162c8..0a9922bb2dd8ece04ef921b86a1ff28ad626dcca 100644 --- a/intrinsiccv/include/intrinsiccv/utils.h +++ b/intrinsiccv/include/intrinsiccv/utils.h @@ -404,16 +404,20 @@ std::enable_if_t check_pointer_and_stride( ElementType *name = reinterpret_cast(from) // Check whether the image size is acceptable by limiting it. -#define CHECK_IMAGE_SIZE(width, height) \ - do { \ - size_t image_size = 0; \ - if (__builtin_mul_overflow(width, height, &image_size)) { \ - return INTRINSICCV_ERROR_RANGE; \ - } \ - \ - if (image_size > INTRINSICCV_MAX_IMAGE_PIXELS) { \ - return INTRINSICCV_ERROR_RANGE; \ - } \ +#define CHECK_IMAGE_SIZE(width, height) \ + do { \ + size_t image_size = 0; \ + if (__builtin_mul_overflow((width), (height), &image_size)) { \ + return INTRINSICCV_ERROR_RANGE; \ + } \ + \ + if ((width) <= 0 || (height) <= 0) { \ + return INTRINSICCV_ERROR_RANGE; \ + } \ + \ + if (image_size > INTRINSICCV_MAX_IMAGE_PIXELS) { \ + return INTRINSICCV_ERROR_RANGE; \ + } \ } while (false) // Check whether the rectangle size is acceptable by limiting it. diff --git a/test/api/test_morphology.cpp b/test/api/test_morphology.cpp index 21e0a5a410622a17b96a92513842f31f74527039..4dcb8797185084d31d268b52a81228532082f7c3 100644 --- a/test/api/test_morphology.cpp +++ b/test/api/test_morphology.cpp @@ -277,6 +277,8 @@ TYPED_TEST(Morphology, UnsupportedSize) { intrinsiccv_rectangle_t{INTRINSICCV_MAX_IMAGE_PIXELS + 1, 1}, intrinsiccv_rectangle_t{INTRINSICCV_MAX_IMAGE_PIXELS, INTRINSICCV_MAX_IMAGE_PIXELS}, + intrinsiccv_rectangle_t{0, 1}, + intrinsiccv_rectangle_t{1, 0}, }) { EXPECT_EQ(INTRINSICCV_ERROR_RANGE, intrinsiccv_morphology_create(&context, bad_rect, anchor, border,