Commit ce41ddcd23

jacob gw <jacoblevgw@gmail.com>
2021-04-21 21:30:06
std: fix potential bug in parseInt
1 parent 93d1c2d
Changed files (1)
lib
lib/std/fmt.zig
@@ -1374,6 +1374,10 @@ test "parseInt" {
     std.testing.expectError(error.Overflow, parseInt(u32, "-10", 10));
     std.testing.expectError(error.InvalidCharacter, parseInt(u32, " 10", 10));
     std.testing.expectError(error.InvalidCharacter, parseInt(u32, "10 ", 10));
+    std.testing.expectError(error.InvalidCharacter, parseInt(u32, "_10_", 10));
+    std.testing.expectError(error.InvalidCharacter, parseInt(u32, "0x_10_", 10));
+    std.testing.expectError(error.InvalidCharacter, parseInt(u32, "0x10_", 10));
+    std.testing.expectError(error.InvalidCharacter, parseInt(u32, "0x_10", 10));
     std.testing.expect((try parseInt(u8, "255", 10)) == 255);
     std.testing.expectError(error.Overflow, parseInt(u8, "256", 10));
 
@@ -1454,6 +1458,8 @@ fn parseWithSign(
 
     var x: T = 0;
 
+    if (buf_start[0] == '_' or buf_start[buf_start.len - 1] == '_') return error.InvalidCharacter;
+
     for (buf_start) |c| {
         if (c == '_') continue;
         const digit = try charToDigit(c, buf_radix);