Skip to content

Commit fae13e5

Browse files
committed
Exchanging the type of 'value' from 'long' to 'double' prevents overflow when parsing float values.
However, we still need to ensure against too large values contained in streams. This should be possible because the maximum length of a float value pre-comma is known to be 38 digits (FLT_MAX_10_EXP).
1 parent 5445db3 commit fae13e5

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

Diff for: api/Stream.cpp

+8-6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include "Common.h"
2626
#include "Stream.h"
27+
#include <math.h>
2728

2829
#define PARSE_TIMEOUT 1000 // default number of milli-seconds to wait
2930

@@ -162,9 +163,9 @@ float Stream::parseFloat(LookaheadMode lookahead, char ignore)
162163
{
163164
bool isNegative = false;
164165
bool isFraction = false;
165-
long value = 0;
166+
double value = 0.0;
166167
int c;
167-
float fraction = 1.0;
168+
unsigned int digits_post_comma = 0;
168169

169170
c = peekNextDigit(lookahead, true);
170171
// ignore non numeric leading characters
@@ -181,7 +182,7 @@ float Stream::parseFloat(LookaheadMode lookahead, char ignore)
181182
else if(c >= '0' && c <= '9') { // is c a digit?
182183
value = value * 10 + c - '0';
183184
if(isFraction)
184-
fraction *= 0.1;
185+
digits_post_comma++;
185186
}
186187
read(); // consume the character we got with peek
187188
c = timedPeek();
@@ -190,10 +191,11 @@ float Stream::parseFloat(LookaheadMode lookahead, char ignore)
190191

191192
if(isNegative)
192193
value = -value;
194+
193195
if(isFraction)
194-
return value * fraction;
195-
else
196-
return value;
196+
value /= pow(10, digits_post_comma);
197+
198+
return value;
197199
}
198200

199201
// read characters from stream into buffer

0 commit comments

Comments
 (0)