authorgravatar for ben@benburkert.comBen Burkert <ben@benburkert.com> 2026-08-08 20:43:16-04:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2026-08-15 06:19:09+02:00
log434c9fe4d79d3513bd36aeaf2f8a90dd28f7cb37
tree2261a7cb531a5975ac3b3b49147f060df214fa23
parentff24d73096cde9792b6156825ad368981e948e2b

asn1: replace use of deprecated lang.Type.Enum API

The API for std.lang.Type changed in PR #35234, but the ASN.1 tag encoder is using the old API. Update the check for an exhaustive enum for the new API, and add a test that exercises this prong of the encoder.

2 files changed, 21 insertions(+), 1 deletions(-)

lib/std/crypto/codecs/asn1.zig+1-1
...@@ -162,7 +162,7 @@ pub const Tag = struct {...@@ -162,7 +162,7 @@ pub const Tag = struct {
162 .int => return universal(.integer, false),162 .int => return universal(.integer, false),
163 .@"enum" => |e| {163 .@"enum" => |e| {
164 if (@hasDecl(T, "oids")) return Oid.asn1_tag;164 if (@hasDecl(T, "oids")) return Oid.asn1_tag;
165 return universal(if (e.is_exhaustive) .enumerated else .integer, false);165 return universal(if (e.mode == .exhaustive) .enumerated else .integer, false);
166 },166 },
167 .optional => |o| return fromZig(o.child),167 .optional => |o| return fromZig(o.child),
168 .null => return universal(.null, false),168 .null => return universal(.null, false),
lib/std/crypto/codecs/asn1/der/Encoder.zig+20
...@@ -21,6 +21,26 @@ pub fn any(self: *Encoder, val: anytype) !void {...@@ -21,6 +21,26 @@ pub fn any(self: *Encoder, val: anytype) !void {
21 try self.anyTag(Tag.fromZig(T), val);21 try self.anyTag(Tag.fromZig(T), val);
22}22}
2323
24test any {
25 const Enum = enum(u8) {
26 x = 0,
27 };
28
29 try checkAnyEncoding(u16, 0, &.{ 0x02, 0x01, 0x00 });
30 try checkAnyEncoding(Enum, .x, &.{ 0x0a, 0x01, 0x00 });
31}
32
33fn checkAnyEncoding(T: type, value: T, expected: []const u8) !void {
34 var encoder: @This() = .init(std.testing.allocator);
35 defer encoder.deinit();
36
37 try encoder.any(value);
38 const encoded = try encoder.buffer.toOwnedSlice();
39 defer std.testing.allocator.free(encoded);
40
41 try std.testing.expectEqualSlices(u8, expected, encoded);
42}
43
24fn anyTag(self: *Encoder, tag_: Tag, val: anytype) !void {44fn anyTag(self: *Encoder, tag_: Tag, val: anytype) !void {
25 const T = @TypeOf(val);45 const T = @TypeOf(val);
26 if (std.meta.hasFn(T, "encodeDer")) return try val.encodeDer(self);46 if (std.meta.hasFn(T, "encodeDer")) return try val.encodeDer(self);