From 8f615d44f17860ba40ed7ae35bd3cca7a8e89ece Mon Sep 17 00:00:00 2001 From: Stefan Weil Date: Mon, 22 Oct 2018 20:41:48 +0200 Subject: [PATCH] osdetect: Fix CID 1164539 (Division or modulo by float zero) Avoid also a conversion from int16_t to double to float. Signed-off-by: Stefan Weil --- src/ccmain/osdetect.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/ccmain/osdetect.cpp b/src/ccmain/osdetect.cpp index 62de43a83b..3e0e793f76 100644 --- a/src/ccmain/osdetect.cpp +++ b/src/ccmain/osdetect.cpp @@ -17,6 +17,10 @@ // /////////////////////////////////////////////////////////////////////// +#include +#include // for std::fabs +#include + #include "osdetect.h" #include "blobbox.h" @@ -33,9 +37,6 @@ #include "tesseractclass.h" #include "textord.h" -#include -#include - const float kSizeRatioToReject = 2.0; const int kMinAcceptableBlobHeight = 10; @@ -252,7 +253,10 @@ int os_detect(TO_BLOCK_LIST* port_blocks, OSResults* osr, TBOX box = blob->bounding_box(); ++blobs_total; - float y_x = fabs((box.height() * 1.0) / box.width()); + // Catch illegal value of box width and avoid division by zero. + if (box.width() == 0) continue; + // TODO: Can height and width be negative? If not, remove fabs. + float y_x = std::fabs((box.height() * 1.0f) / box.width()); float x_y = 1.0f / y_x; // Select a >= 1.0 ratio float ratio = x_y > y_x ? x_y : y_x;