authorgravatar for me@gasinfinity.devGasInfinity <me@gasinfinity.dev> 2025-03-22 00:40:33+01:00
committergravatar for mail@linusgroh.deLinus Groh <mail@linusgroh.de> 2025-03-24 11:40:19+00:00
logdfe0fb675de43f3a4697246a5ec4ec6300b57682
treeb15390c56273a6c297ef8e0af6ed28770fde55e2
parent149eace5d563a3326604baf808dabb02edbe7fba

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 {...@@ -1380,13 +1380,8 @@ test fmtDuration {
1380}1380}
13811381
1382fn formatDurationSigned(ns: i64, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void {1382fn formatDurationSigned(ns: i64, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void {
1383 if (ns < 0) {1383 const data = FormatDurationData{ .ns = @abs(ns), .negative = ns < 0 };
1384 const data = FormatDurationData{ .ns = @as(u64, @intCast(-ns)), .negative = true };1384 try formatDuration(data, fmt, options, writer);
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 }
1390}1385}
13911386
1392/// Return a Formatter for number of nanoseconds according to its signed magnitude:1387/// Return a Formatter for number of nanoseconds according to its signed magnitude:
...@@ -1457,6 +1452,7 @@ test fmtDurationSigned {...@@ -1457,6 +1452,7 @@ test fmtDurationSigned {
1457 .{ .s = "-1y1m999ns", .d = -(365 * std.time.ns_per_day + std.time.ns_per_min + 999) },1452 .{ .s = "-1y1m999ns", .d = -(365 * std.time.ns_per_day + std.time.ns_per_min + 999) },
1458 .{ .s = "292y24w3d23h47m16.854s", .d = math.maxInt(i64) },1453 .{ .s = "292y24w3d23h47m16.854s", .d = math.maxInt(i64) },
1459 .{ .s = "-292y24w3d23h47m16.854s", .d = math.minInt(i64) + 1 },1454 .{ .s = "-292y24w3d23h47m16.854s", .d = math.minInt(i64) + 1 },
1455 .{ .s = "-292y24w3d23h47m16.854s", .d = math.minInt(i64) },
1460 }) |tc| {1456 }) |tc| {
1461 const slice = try bufPrint(&buf, "{}", .{fmtDurationSigned(tc.d)});1457 const slice = try bufPrint(&buf, "{}", .{fmtDurationSigned(tc.d)});
1462 try std.testing.expectEqualStrings(tc.s, slice);1458 try std.testing.expectEqualStrings(tc.s, slice);