Commit b3af5d076c

Evin Yulo <yujiri@disroot.org>
2023-03-15 17:53:53
Fix #14901: parseFloat parsing `0x` successfully
1 parent da05097
Changed files (2)
lib
std
fmt
lib/std/fmt/parse_float/parse.zig
@@ -107,6 +107,8 @@ fn parsePartialNumberBase(comptime T: type, stream: *FloatStream, negative: bool
     tryParseDigits(MantissaT, stream, &mantissa, info.base);
     var int_end = stream.offsetTrue();
     var n_digits = @intCast(isize, stream.offsetTrue());
+    // the base being 16 implies a 0x prefix, which shouldn't be included in the digit count
+    if (info.base == 16) n_digits -= 2;
 
     // handle dot with the following digits
     var exponent: i64 = 0;
lib/std/fmt/parse_float.zig
@@ -119,6 +119,7 @@ test "fmt.parseFloat hex.f16" {
 }
 
 test "fmt.parseFloat hex.f32" {
+    try testing.expectError(error.InvalidCharacter, parseFloat(f32, "0x"));
     try testing.expectEqual(try parseFloat(f32, "0x1p0"), 1.0);
     try testing.expectEqual(try parseFloat(f32, "-0x1p-1"), -0.5);
     try testing.expectEqual(try parseFloat(f32, "0x10p+10"), 16384.0);