From 8f615d44f17860ba40ed7ae35bd3cca7a8e89ece Mon Sep 17 00:00:00 2001 From: Stefan Weil Date: Mon, 22 Oct 2018 20:41:48 +0200 Subject: [PATCH 1/3] 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 62de43a83..3e0e793f7 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; From eaabe4a3cedc44f2029778a635a3f6a1a1e6b47a Mon Sep 17 00:00:00 2001 From: Stefan Weil Date: Mon, 22 Oct 2018 20:43:43 +0200 Subject: [PATCH 2/3] ErrorCounter: Fix CID 1164538 (Division or modulo by float zero) Signed-off-by: Stefan Weil --- src/classify/errorcounter.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/classify/errorcounter.cpp b/src/classify/errorcounter.cpp index 65b36b7f6..9e0796499 100644 --- a/src/classify/errorcounter.cpp +++ b/src/classify/errorcounter.cpp @@ -46,7 +46,7 @@ double ErrorCounter::ComputeErrorRate(ShapeClassifier* classifier, GenericVector results; clock_t start = clock(); - int total_samples = 0; + unsigned total_samples = 0; double unscaled_error = 0.0; // Set a number of samples on which to run the classify debug mode. int error_samples = report_level > 3 ? report_level * report_level : 0; @@ -89,7 +89,7 @@ double ErrorCounter::ComputeErrorRate(ShapeClassifier* classifier, fontinfo_table, *it, unichar_error, fonts_report); if (scaled_error != nullptr) *scaled_error = counter.scaled_error_; - if (report_level > 1) { + if (report_level > 1 && total_samples > 0) { // It is useful to know the time in microseconds/char. tprintf("Errors computed in %.2fs at %.1f μs/char\n", total_time, 1000000.0 * total_time / total_samples); From 7ebbb7370a4c15c89c21050cb22c6d2fe702ffad Mon Sep 17 00:00:00 2001 From: Stefan Weil Date: Mon, 22 Oct 2018 21:05:44 +0200 Subject: [PATCH 3/3] ColPartition: Fix CID 1164543 (Division or modulo by float zero) Signed-off-by: Stefan Weil --- src/textord/colpartition.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/textord/colpartition.cpp b/src/textord/colpartition.cpp index 629e196a9..0167d59f6 100644 --- a/src/textord/colpartition.cpp +++ b/src/textord/colpartition.cpp @@ -663,10 +663,12 @@ void ColPartition::Absorb(ColPartition* other, WidthCallback* cb) { // Update the special_blobs_densities_. memset(special_blobs_densities_, 0, sizeof(special_blobs_densities_)); for (int type = 0; type < BSTT_COUNT; ++type) { - int w1 = boxes_.length(), w2 = other->boxes_.length(); + unsigned w1 = boxes_.length(); + unsigned w2 = other->boxes_.length(); float new_val = special_blobs_densities_[type] * w1 + other->special_blobs_densities_[type] * w2; if (!w1 || !w2) { + ASSERT_HOST((w1 + w2) > 0); special_blobs_densities_[type] = new_val / (w1 + w2); } }