Skip to content

Commit a93426c

Browse files
committed
Fix wrong results from function streamtofloat
The local variable k should be 10 ^ (number of digits after comma), but will overflow when there are more than 9 digits after the comma because an int value cannot store 10000000000. This results in wrong double values read from .tr files for example (or in a runtime exception if Tesseract was compiled with -ftrapv). Using uint64_t does not fix the general problem but allows more digits which should be sufficient for the data read by Tesseract. Signed-off-by: Stefan Weil <sw@weilnetz.de>
1 parent b67ea2c commit a93426c

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

src/ccutil/scanutils.cpp

+6-7
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,11 @@ static uintmax_t streamtoumax(FILE* s, int base) {
143143
}
144144

145145
static double streamtofloat(FILE* s) {
146-
int minus = 0;
147-
int v = 0;
148-
int d, c = 0;
149-
int k = 1;
150-
int w = 0;
146+
bool minus = false;
147+
uint64_t v = 0;
148+
int d, c;
149+
uint64_t k = 1;
150+
uint64_t w = 0;
151151

152152
for (c = fgetc(s); isascii(c) && isspace(c); c = fgetc(s));
153153

@@ -166,8 +166,7 @@ static double streamtofloat(FILE* s) {
166166
k *= 10;
167167
}
168168
}
169-
double f = static_cast<double>(v)
170-
+ static_cast<double>(w) / static_cast<double>(k);
169+
double f = v + static_cast<double>(w) / k;
171170
if (c == 'e' || c == 'E') {
172171
c = fgetc(s);
173172
int expsign = 1;

0 commit comments

Comments
 (0)