Skip to content

Commit 8f615d4

Browse files
committed
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 <sw@weilnetz.de>
1 parent 69a2e94 commit 8f615d4

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/ccmain/osdetect.cpp

+8-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
//
1818
///////////////////////////////////////////////////////////////////////
1919

20+
#include <algorithm>
21+
#include <cmath> // for std::fabs
22+
#include <memory>
23+
2024
#include "osdetect.h"
2125

2226
#include "blobbox.h"
@@ -33,9 +37,6 @@
3337
#include "tesseractclass.h"
3438
#include "textord.h"
3539

36-
#include <algorithm>
37-
#include <memory>
38-
3940
const float kSizeRatioToReject = 2.0;
4041
const int kMinAcceptableBlobHeight = 10;
4142

@@ -252,7 +253,10 @@ int os_detect(TO_BLOCK_LIST* port_blocks, OSResults* osr,
252253
TBOX box = blob->bounding_box();
253254
++blobs_total;
254255

255-
float y_x = fabs((box.height() * 1.0) / box.width());
256+
// Catch illegal value of box width and avoid division by zero.
257+
if (box.width() == 0) continue;
258+
// TODO: Can height and width be negative? If not, remove fabs.
259+
float y_x = std::fabs((box.height() * 1.0f) / box.width());
256260
float x_y = 1.0f / y_x;
257261
// Select a >= 1.0 ratio
258262
float ratio = x_y > y_x ? x_y : y_x;

0 commit comments

Comments
 (0)