diff options
| author | cryptocode <cryptocode@zolo.io> | 2021-03-04 18:42:56 +0100 |
|---|---|---|
| committer | Isaac Freund <ifreund@ifreund.xyz> | 2021-03-05 13:11:20 +0100 |
| commit | 02737d535ac5bda4e9a00ec0ae11ae313065dfbb (patch) | |
| tree | e9af1999fd94f99a78f500e26ae9dac284466eb2 /lib/std/fmt/parse_float.zig | |
| parent | 434fce2146a9f1dc096fdae6827e0855799351ca (diff) | |
| download | zig-02737d535ac5bda4e9a00ec0ae11ae313065dfbb.tar.gz zig-02737d535ac5bda4e9a00ec0ae11ae313065dfbb.zip | |
Reject bare +/- input when parsing floats
Diffstat (limited to 'lib/std/fmt/parse_float.zig')
| -rw-r--r-- | lib/std/fmt/parse_float.zig | 4 |
1 files changed, 3 insertions, 1 deletions
diff --git a/lib/std/fmt/parse_float.zig b/lib/std/fmt/parse_float.zig index 324b06898e..19e17ef5a8 100644 --- a/lib/std/fmt/parse_float.zig +++ b/lib/std/fmt/parse_float.zig @@ -339,7 +339,7 @@ fn caseInEql(a: []const u8, b: []const u8) bool { } pub fn parseFloat(comptime T: type, s: []const u8) !T { - if (s.len == 0) { + if (s.len == 0 or (s.len == 1 and (s[0] == '+' or s[0] == '-'))) { return error.InvalidCharacter; } @@ -379,6 +379,8 @@ test "fmt.parseFloat" { testing.expectError(error.InvalidCharacter, parseFloat(T, "")); testing.expectError(error.InvalidCharacter, parseFloat(T, " 1")); testing.expectError(error.InvalidCharacter, parseFloat(T, "1abc")); + testing.expectError(error.InvalidCharacter, parseFloat(T, "+")); + testing.expectError(error.InvalidCharacter, parseFloat(T, "-")); expectEqual(try parseFloat(T, "0"), 0.0); expectEqual(try parseFloat(T, "0"), 0.0); |
