Skip to content

Commit dfe0fb6

Browse files
GasInfinitylinusg
authored andcommitted
fix(std/fmt.zig): fix overflow in fmtDurationSigned
fixes #23315
1 parent 149eace commit dfe0fb6

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

lib/std/fmt.zig

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,13 +1380,8 @@ test fmtDuration {
13801380
}
13811381

13821382
fn 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);

0 commit comments

Comments
 (0)