Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfae13e5

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 parent5445db3 commitfae13e5

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

‎api/Stream.cpp‎

Lines changed: 8 additions & 6 deletions
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
#definePARSE_TIMEOUT1000// 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+
unsignedint 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
elseif(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-
returnvalue* 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)

[8]ページ先頭

©2009-2025 Movatter.jp