authorgravatar for marc@tiehu.isMarc Tiehuis <marc@tiehu.is> 2024-02-25 19:15:22+13:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-02-25 12:37:03-08:00
logff3bf983459c5c58ec0374f6f52627902f13e721
tree3bd084aa161643c696dc0441b0b7e04bec836459
parent08e886b8fec4c51366517b1c1d52d588c24abb4f

fix large f128 values being incorrectly parsed as inf

Found while fuzzing. Previously 1.1897314953572317650857593266280070162E4932 was parsed as +inf, which caused issues for round-trip serialization of floats. Only f128 had issues, but have added other tests for all floating point large normals. The max_exponent for f128 was wrong, it is subtly different in the decimal code-path as it is based on where the decimal digit should go. This needs to be 2 greater than the max exponent (e.g. 308 or 4932) to work correctly (greater by 1, then we use a >= comparision). In addition, I've removed the redundant `optimize` constant which was only use for testing the slow path locally.

3 files changed, 22 insertions(+), 19 deletions(-)

lib/std/fmt/parse_float.zig+7
...@@ -78,6 +78,13 @@ test "fmt.parseFloat nan and inf" {...@@ -78,6 +78,13 @@ test "fmt.parseFloat nan and inf" {
78 }78 }
79}79}
8080
81test "fmt.parseFloat largest normals" {
82 try expectEqual(@as(u16, @bitCast(try parseFloat(f16, "65504"))), 0x7bff);
83 try expectEqual(@as(u32, @bitCast(try parseFloat(f32, "3.4028234664E38"))), 0x7f7f_ffff);
84 try expectEqual(@as(u64, @bitCast(try parseFloat(f64, "1.7976931348623157E308"))), 0x7fef_ffff_ffff_ffff);
85 try expectEqual(@as(u128, @bitCast(try parseFloat(f128, "1.1897314953572317650857593266280070162E4932"))), 0x7ffe_ffff_ffff_ffff_ffff_ffff_ffff_ffff);
86}
87
81test "fmt.parseFloat #11169" {88test "fmt.parseFloat #11169" {
82 try expectEqual(try parseFloat(f128, "9007199254740993.0"), 9007199254740993.0);89 try expectEqual(try parseFloat(f128, "9007199254740993.0"), 9007199254740993.0);
83}90}
lib/std/fmt/parse_float/decimal.zig+1-1
...@@ -63,7 +63,7 @@ pub fn Decimal(comptime T: type) type {...@@ -63,7 +63,7 @@ pub fn Decimal(comptime T: type) type {
63 pub const max_digits_without_overflow = if (MantissaT == u64) 19 else 38;63 pub const max_digits_without_overflow = if (MantissaT == u64) 19 else 38;
64 pub const decimal_point_range = if (MantissaT == u64) 2047 else 32767;64 pub const decimal_point_range = if (MantissaT == u64) 2047 else 32767;
65 pub const min_exponent = if (MantissaT == u64) -324 else -4966;65 pub const min_exponent = if (MantissaT == u64) -324 else -4966;
66 pub const max_exponent = if (MantissaT == u64) 310 else 4933;66 pub const max_exponent = if (MantissaT == u64) 310 else 4934;
67 pub const max_decimal_digits = if (MantissaT == u64) 18 else 37;67 pub const max_decimal_digits = if (MantissaT == u64) 18 else 37;
6868
69 /// The number of significant digits in the decimal.69 /// The number of significant digits in the decimal.
lib/std/fmt/parse_float/parse_float.zig+14-18
...@@ -5,8 +5,6 @@ const convertEiselLemire = @import("convert_eisel_lemire.zig").convertEiselLemir...@@ -5,8 +5,6 @@ const convertEiselLemire = @import("convert_eisel_lemire.zig").convertEiselLemir
5const convertSlow = @import("convert_slow.zig").convertSlow;5const convertSlow = @import("convert_slow.zig").convertSlow;
6const convertHex = @import("convert_hex.zig").convertHex;6const convertHex = @import("convert_hex.zig").convertHex;
77
8const optimize = true;
9
10pub const ParseFloatError = error{8pub const ParseFloatError = error{
11 InvalidCharacter,9 InvalidCharacter,
12};10};
...@@ -41,25 +39,23 @@ pub fn parseFloat(comptime T: type, s: []const u8) ParseFloatError!T {...@@ -41,25 +39,23 @@ pub fn parseFloat(comptime T: type, s: []const u8) ParseFloatError!T {
41 return convertHex(T, n);39 return convertHex(T, n);
42 }40 }
4341
44 if (optimize) {42 if (convertFast(T, n)) |f| {
45 if (convertFast(T, n)) |f| {43 return f;
46 return f;44 }
47 }
4845
49 if (T == f16 or T == f32 or T == f64) {46 if (T == f16 or T == f32 or T == f64) {
50 // If significant digits were truncated, then we can have rounding error47 // If significant digits were truncated, then we can have rounding error
51 // only if `mantissa + 1` produces a different result. We also avoid48 // only if `mantissa + 1` produces a different result. We also avoid
52 // redundantly using the Eisel-Lemire algorithm if it was unable to49 // redundantly using the Eisel-Lemire algorithm if it was unable to
53 // correctly round on the first pass.50 // correctly round on the first pass.
54 if (convertEiselLemire(T, n.exponent, n.mantissa)) |bf| {51 if (convertEiselLemire(T, n.exponent, n.mantissa)) |bf| {
55 if (!n.many_digits) {52 if (!n.many_digits) {
53 return bf.toFloat(T, n.negative);
54 }
55 if (convertEiselLemire(T, n.exponent, n.mantissa + 1)) |bf2| {
56 if (bf.eql(bf2)) {
56 return bf.toFloat(T, n.negative);57 return bf.toFloat(T, n.negative);
57 }58 }
58 if (convertEiselLemire(T, n.exponent, n.mantissa + 1)) |bf2| {
59 if (bf.eql(bf2)) {
60 return bf.toFloat(T, n.negative);
61 }
62 }
63 }59 }
64 }60 }
65 }61 }