authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-08-28 18:30:32-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-08-28 18:30:57-07:00
log7da9e4b35e710d6b94114a21017630c5f15940bd
tree818b1cea62a316b002a51e6f1588cd0d1029a3df
parent530cc2c1111699d9d02ad9ebef94efa6b99f5205

std.tz: fix redundant endian handling

I didn't notice the check+swap before.

1 files changed, 19 insertions(+), 20 deletions(-)

lib/std/tz.zig+19-20
...@@ -1,3 +1,6 @@...@@ -1,3 +1,6 @@
1//! The Time Zone Information Format (TZif)
2//! https://datatracker.ietf.org/doc/html/rfc8536
3
1const builtin = @import("builtin");4const builtin = @import("builtin");
25
3const std = @import("std.zig");6const std = @import("std.zig");
...@@ -58,30 +61,26 @@ pub const Tz = struct {...@@ -58,30 +61,26 @@ pub const Tz = struct {
58 };61 };
5962
60 pub fn parse(allocator: Allocator, reader: *Reader) !Tz {63 pub fn parse(allocator: Allocator, reader: *Reader) !Tz {
61 var legacy_header = try reader.takeStruct(Header, .little);64 const legacy_header = try reader.takeStruct(Header, .big);
62 if (!std.mem.eql(u8, &legacy_header.magic, "TZif")) return error.BadHeader;65 if (!std.mem.eql(u8, &legacy_header.magic, "TZif")) return error.BadHeader;
63 if (legacy_header.version != 0 and legacy_header.version != '2' and legacy_header.version != '3') return error.BadVersion;66 if (legacy_header.version != 0 and legacy_header.version != '2' and legacy_header.version != '3')
6467 return error.BadVersion;
65 if (builtin.target.cpu.arch.endian() != std.builtin.Endian.big) {
66 std.mem.byteSwapAllFields(@TypeOf(legacy_header.counts), &legacy_header.counts);
67 }
6868
69 if (legacy_header.version == 0) {69 if (legacy_header.version == 0)
70 return parseBlock(allocator, reader, legacy_header, true);70 return parseBlock(allocator, reader, legacy_header, true);
71 } else {
72 // If the format is modern, just skip over the legacy data
73 const skipv = legacy_header.counts.timecnt * 5 + legacy_header.counts.typecnt * 6 + legacy_header.counts.charcnt + legacy_header.counts.leapcnt * 8 + legacy_header.counts.isstdcnt + legacy_header.counts.isutcnt;
74 try reader.discardAll(skipv);
75
76 var header = try reader.takeStruct(Header, .little);
77 if (!std.mem.eql(u8, &header.magic, "TZif")) return error.BadHeader;
78 if (header.version != '2' and header.version != '3') return error.BadVersion;
79 if (builtin.target.cpu.arch.endian() != std.builtin.Endian.big) {
80 std.mem.byteSwapAllFields(@TypeOf(header.counts), &header.counts);
81 }
8271
83 return parseBlock(allocator, reader, header, false);72 // If the format is modern, just skip over the legacy data
84 }73 const skip_n = legacy_header.counts.timecnt * 5 +
74 legacy_header.counts.typecnt * 6 +
75 legacy_header.counts.charcnt + legacy_header.counts.leapcnt * 8 +
76 legacy_header.counts.isstdcnt + legacy_header.counts.isutcnt;
77 try reader.discardAll(skip_n);
78
79 var header = try reader.takeStruct(Header, .big);
80 if (!std.mem.eql(u8, &header.magic, "TZif")) return error.BadHeader;
81 if (header.version != '2' and header.version != '3') return error.BadVersion;
82
83 return parseBlock(allocator, reader, header, false);
85 }84 }
8685
87 fn parseBlock(allocator: Allocator, reader: *Reader, header: Header, legacy: bool) !Tz {86 fn parseBlock(allocator: Allocator, reader: *Reader, header: Header, legacy: bool) !Tz {