authorgravatar for me@gasinfinity.devGasInfinity <me@gasinfinity.dev> 2025-03-22 00:40:33+01:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-03-24 15:31:53+01:00
loga7cfc23e5a44f980c12bb686450fdd9e74ca958d
tree3451cb4c0ab88e2c3a169e82b2fa42e83ebbe5b3
parente62a3ea74eb4d019a3360f666700579ca6b30cb9
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

fix(std/fmt.zig): fix overflow in fmtDurationSigned

fixes #23315

1 files changed, 3 insertions(+), 7 deletions(-)

lib/std/fmt.zig+3-7
......@@ -1380,13 +1380,8 @@ test fmtDuration {
13801380}
13811381
13821382fn formatDurationSigned(ns: i64, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void {
1383 if (ns < 0) {
1384 const data = FormatDurationData{ .ns = @as(u64, @intCast(-ns)), .negative = true };
1385 try formatDuration(data, fmt, options, writer);
1386 } else {
1387 const data = FormatDurationData{ .ns = @as(u64, @intCast(ns)) };
1388 try formatDuration(data, fmt, options, writer);
1389 }
1383 const data = FormatDurationData{ .ns = @abs(ns), .negative = ns < 0 };
1384 try formatDuration(data, fmt, options, writer);
13901385}
13911386
13921387/// Return a Formatter for number of nanoseconds according to its signed magnitude:
......@@ -1457,6 +1452,7 @@ test fmtDurationSigned {
14571452 .{ .s = "-1y1m999ns", .d = -(365 * std.time.ns_per_day + std.time.ns_per_min + 999) },
14581453 .{ .s = "292y24w3d23h47m16.854s", .d = math.maxInt(i64) },
14591454 .{ .s = "-292y24w3d23h47m16.854s", .d = math.minInt(i64) + 1 },
1455 .{ .s = "-292y24w3d23h47m16.854s", .d = math.minInt(i64) },
14601456 }) |tc| {
14611457 const slice = try bufPrint(&buf, "{}", .{fmtDurationSigned(tc.d)});
14621458 try std.testing.expectEqualStrings(tc.s, slice);