Skip to content

Commit

Permalink
Issue #148: Check for overflow when reading floating point exponent.
Browse files Browse the repository at this point in the history
GCC with -O2 optimizes away the if(exp<-maxExponent) branch completely,
so we don't end up with the expected '512' value for overflowing
exponents. Limit the exponent parsing to MAX_INT instead to prevent
signed overflow from tripping up over-eager optimizing compilers.
  • Loading branch information
ccxvii committed Apr 21, 2021
1 parent 292415b commit 833b6f1
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion jsdtoa.c
Original file line number Diff line number Diff line change
Expand Up @@ -691,10 +691,12 @@ js_strtod(const char *string, char **endPtr)
}
expSign = FALSE;
}
while ((*p >= '0') && (*p <= '9')) {
while ((*p >= '0') && (*p <= '9') && exp < INT_MAX/10) {
exp = exp * 10 + (*p - '0');

This comment has been minimized.

Copy link
@avih

avih Apr 21, 2021

Contributor

Wouldn't this still overflow for any digit other than 0?

p += 1;
}
while ((*p >= '0') && (*p <= '9'))
p += 1;
}
if (expSign) {
exp = fracExp - exp;
Expand Down

0 comments on commit 833b6f1

Please sign in to comment.