Skip to content

Commit

Permalink
Merge pull request #2022 from stweil/coverity
Browse files Browse the repository at this point in the history
Fix several issues reported by Coverity Scan
  • Loading branch information
zdenop authored Oct 22, 2018
2 parents 69a2e94 + 7ebbb73 commit 5a68b7f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
12 changes: 8 additions & 4 deletions src/ccmain/osdetect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
//
///////////////////////////////////////////////////////////////////////

#include <algorithm>
#include <cmath> // for std::fabs
#include <memory>

#include "osdetect.h"

#include "blobbox.h"
Expand All @@ -33,9 +37,6 @@
#include "tesseractclass.h"
#include "textord.h"

#include <algorithm>
#include <memory>

const float kSizeRatioToReject = 2.0;
const int kMinAcceptableBlobHeight = 10;

Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/classify/errorcounter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ double ErrorCounter::ComputeErrorRate(ShapeClassifier* classifier,
GenericVector<UnicharRating> 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;
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion src/textord/colpartition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down

0 comments on commit 5a68b7f

Please sign in to comment.