Skip to content

Commit b3af5d0

Browse files
Evin YuloVexu
Evin Yulo
authored andcommitted
Fix #14901: parseFloat parsing 0x successfully
1 parent da05097 commit b3af5d0

File tree

2 files changed

+3
-0
lines changed

2 files changed

+3
-0
lines changed

lib/std/fmt/parse_float.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ test "fmt.parseFloat hex.f16" {
119119
}
120120

121121
test "fmt.parseFloat hex.f32" {
122+
try testing.expectError(error.InvalidCharacter, parseFloat(f32, "0x"));
122123
try testing.expectEqual(try parseFloat(f32, "0x1p0"), 1.0);
123124
try testing.expectEqual(try parseFloat(f32, "-0x1p-1"), -0.5);
124125
try testing.expectEqual(try parseFloat(f32, "0x10p+10"), 16384.0);

lib/std/fmt/parse_float/parse.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ fn parsePartialNumberBase(comptime T: type, stream: *FloatStream, negative: bool
107107
tryParseDigits(MantissaT, stream, &mantissa, info.base);
108108
var int_end = stream.offsetTrue();
109109
var n_digits = @intCast(isize, stream.offsetTrue());
110+
// the base being 16 implies a 0x prefix, which shouldn't be included in the digit count
111+
if (info.base == 16) n_digits -= 2;
110112

111113
// handle dot with the following digits
112114
var exponent: i64 = 0;

0 commit comments

Comments
 (0)