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

fixes #23315
This commit is contained in:
GasInfinity 2025-03-22 00:40:33 +01:00 committed by Alex Rønne Petersen
parent e62a3ea74e
commit a7cfc23e5a
No known key found for this signature in database

View file

@ -1380,13 +1380,8 @@ test fmtDuration {
}
fn formatDurationSigned(ns: i64, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void {
if (ns < 0) {
const data = FormatDurationData{ .ns = @as(u64, @intCast(-ns)), .negative = true };
try formatDuration(data, fmt, options, writer);
} else {
const data = FormatDurationData{ .ns = @as(u64, @intCast(ns)) };
try formatDuration(data, fmt, options, writer);
}
const data = FormatDurationData{ .ns = @abs(ns), .negative = ns < 0 };
try formatDuration(data, fmt, options, writer);
}
/// Return a Formatter for number of nanoseconds according to its signed magnitude:
@ -1457,6 +1452,7 @@ test fmtDurationSigned {
.{ .s = "-1y1m999ns", .d = -(365 * std.time.ns_per_day + std.time.ns_per_min + 999) },
.{ .s = "292y24w3d23h47m16.854s", .d = math.maxInt(i64) },
.{ .s = "-292y24w3d23h47m16.854s", .d = math.minInt(i64) + 1 },
.{ .s = "-292y24w3d23h47m16.854s", .d = math.minInt(i64) },
}) |tc| {
const slice = try bufPrint(&buf, "{}", .{fmtDurationSigned(tc.d)});
try std.testing.expectEqualStrings(tc.s, slice);