authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-09-03 10:22:35+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-09-03 18:14:19+03:00
log969547902b49d6b21af762fb24ed591789b9d2a4
tree4f921d3bf0f3041f9809dfd3f9dd1383ddf5f3a1
parentfb3c5b84ede6fa48949c8069bf735ac67ec21091

std: Fix silent overflow in float parsing code

A u64 can only hold 19 decimal digits, adjust the limit.

1 files changed, 4 insertions(+), 1 deletions(-)

lib/std/fmt/parse_float.zig+4-1
...@@ -37,7 +37,9 @@...@@ -37,7 +37,9 @@
37const std = @import("../std.zig");37const std = @import("../std.zig");
38const ascii = std.ascii;38const ascii = std.ascii;
3939
40const max_digits = 25;40// The mantissa field in FloatRepr is 64bit wide and holds only 19 digits
41// without overflowing
42const max_digits = 19;
4143
42const f64_plus_zero: u64 = 0x0000000000000000;44const f64_plus_zero: u64 = 0x0000000000000000;
43const f64_minus_zero: u64 = 0x8000000000000000;45const f64_minus_zero: u64 = 0x8000000000000000;
...@@ -409,6 +411,7 @@ test "fmt.parseFloat" {...@@ -409,6 +411,7 @@ test "fmt.parseFloat" {
409 expect(approxEq(T, try parseFloat(T, "123142.1"), 123142.1, epsilon));411 expect(approxEq(T, try parseFloat(T, "123142.1"), 123142.1, epsilon));
410 expect(approxEq(T, try parseFloat(T, "-123142.1124"), @as(T, -123142.1124), epsilon));412 expect(approxEq(T, try parseFloat(T, "-123142.1124"), @as(T, -123142.1124), epsilon));
411 expect(approxEq(T, try parseFloat(T, "0.7062146892655368"), @as(T, 0.7062146892655368), epsilon));413 expect(approxEq(T, try parseFloat(T, "0.7062146892655368"), @as(T, 0.7062146892655368), epsilon));
414 expect(approxEq(T, try parseFloat(T, "2.71828182845904523536"), @as(T, 2.718281828459045), epsilon));
412 }415 }
413 }416 }
414}417}