opencv_contrib icon indicating copy to clipboard operation
opencv_contrib copied to clipboard

Fix array out of range when no edges found

Open fdivitto opened this issue 2 years ago • 2 comments

When input image is almost black edges.numOfEdges could be zero. In this case lines.sId size is zero and "detect lines" loop is never executed, so this array doesn't get chances to increase (see commit https://github.com/opencv/opencv_contrib/commit/7e14b4bba1766641975c88ca8a0bbf51511461f4).

Just before return the pointer to the beginning of lines.sId (pLineSID) is dereferenced at index 0, causing the AV because size of array is zero and pLineSID is invalid.

This fix simply make sure lines.sId contains at least one item.

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • [x] I agree to contribute to the project under Apache 2 License.
  • [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
  • [x] The PR is proposed to the proper branch
  • [x] There is a reference to the original bug report and related work
  • [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name.
  • [x] The feature is well documented and sample code can be built with the project CMake

fdivitto avatar Aug 17 '22 21:08 fdivitto

If I understand the function correctly the best solution is to add early exit if edges.numOfEdges == 0 like this:

if (edges.numOfEdges == 0)
{
  lines.numOfLines = 0;
  return 1; // need to check if it's expected return value if nothing found
}

It's obvious corner case handler for code reader, no questions about vectors size.

asmorkalov avatar Sep 07 '22 08:09 asmorkalov

As you prefer. I like a code with a few jumps or early exits from functions. "edges.numOfEdges" is implicitly checked in the main loop.

fdivitto avatar Sep 19 '22 07:09 fdivitto

@fdivitto friendly reminder.

asmorkalov avatar Oct 03 '22 07:10 asmorkalov

@fdivitto friendly reminder.

asmorkalov avatar Dec 15 '22 07:12 asmorkalov