From 435e8e3b9e649695ac2125e6627e31b3679e426a Mon Sep 17 00:00:00 2001 From: Denes Tarjan Date: Mon, 26 Feb 2024 13:35:55 +0100 Subject: [PATCH] [test] Add check against zero image width and height --- .../intrinsiccv/morphology/workspace.h | 2 ++ intrinsiccv/include/intrinsiccv/utils.h | 24 +++++++++++-------- test/api/test_morphology.cpp | 2 ++ 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/intrinsiccv/include/intrinsiccv/morphology/workspace.h b/intrinsiccv/include/intrinsiccv/morphology/workspace.h index 3132c9054..9eff46566 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 c6d4652a9..0a9922bb2 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 21e0a5a41..4dcb87971 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, -- GitLab