From 2c5389594bb560b62097de3602755ef97e60135f Mon Sep 17 00:00:00 2001 From: Jiri Horner Date: Wed, 14 Jun 2017 14:56:38 +0200 Subject: [PATCH] features2d: fix crash in AKAZE * out-of-bound reads near the image edge * same as the bug in KAZE descriptors --- modules/features2d/src/kaze/AKAZEFeatures.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/modules/features2d/src/kaze/AKAZEFeatures.cpp b/modules/features2d/src/kaze/AKAZEFeatures.cpp index 9d6070ded8..b1400a7a9b 100644 --- a/modules/features2d/src/kaze/AKAZEFeatures.cpp +++ b/modules/features2d/src/kaze/AKAZEFeatures.cpp @@ -1235,12 +1235,20 @@ void MLDB_Full_Descriptor_Invoker::MLDB_Fill_Values(float* values, int sample_st int y1 = fRound(sample_y); int x1 = fRound(sample_x); - float ri = *(evolution[level].Lt.ptr(y1)+x1); + // fix crash: indexing with out-of-bounds index, this might happen near the edges of image + // clip values so they fit into the image + const MatSize& size = evolution[level].Lt.size; + CV_DbgAssert(size == evolution[level].Lx.size && + size == evolution[level].Ly.size); + y1 = min(max(0, y1), size[0] - 1); + x1 = min(max(0, x1), size[1] - 1); + + float ri = *(evolution[level].Lt.ptr(y1, x1)); di += ri; if(chan > 1) { - float rx = *(evolution[level].Lx.ptr(y1)+x1); - float ry = *(evolution[level].Ly.ptr(y1)+x1); + float rx = *(evolution[level].Lx.ptr(y1, x1)); + float ry = *(evolution[level].Ly.ptr(y1, x1)); if (chan == 2) { dx += sqrtf(rx*rx + ry*ry); }