Skip to content

Commit 868d2e5

Browse files
Fix crash when exponent is -2147483648
1 parent 73cc087 commit 868d2e5

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/string.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -2868,7 +2868,11 @@ mrb_float_read(const char *string, char **endPtr)
28682868
mantSize -= 1; /* One of the digits was the point. */
28692869
}
28702870
if (mantSize > 18) {
2871-
fracExp = decPt - 18;
2871+
if (decPt - 18 > 29999) {
2872+
fracExp = 29999;
2873+
} else {
2874+
fracExp = decPt - 18;
2875+
}
28722876
mantSize = 18;
28732877
} else {
28742878
fracExp = decPt - mantSize;
@@ -2922,6 +2926,9 @@ mrb_float_read(const char *string, char **endPtr)
29222926
}
29232927
while (isdigit(*p)) {
29242928
exp = exp * 10 + (*p - '0');
2929+
if (exp > 19999) {
2930+
exp = 19999;
2931+
}
29252932
p += 1;
29262933
}
29272934
}

test/t/string.rb

+4
Original file line numberDiff line numberDiff line change
@@ -596,10 +596,14 @@ def to_str
596596
a = ''.to_f
597597
b = '123456789'.to_f
598598
c = '12345.6789'.to_f
599+
d = '1e-2147483648'.to_f
600+
e = '1e2147483648'.to_f
599601

600602
assert_float(0.0, a)
601603
assert_float(123456789.0, b)
602604
assert_float(12345.6789, c)
605+
assert_float(0, d)
606+
assert_float(Float::INFINITY, e)
603607
end
604608

605609
assert('String#to_i', '15.2.10.5.39') do

0 commit comments

Comments
 (0)