authorgravatar for igor.anic@gmail.comIgor Anić <igor.anic@gmail.com> 2023-11-29 20:30:08+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-13 19:37:33-07:00
log48b160c1bf75f602acabc3b43eca56b8aa4abf4f
treefb23b9fa99d1ccc0d5548aca74c60500b9138513
parent16c40fc4713c195c7a6b8544c9dffbfc6201dc9d

tar: handle pax null attrs and pax attr ending


1 files changed, 57 insertions(+), 22 deletions(-)

lib/std/tar.zig+57-22
......@@ -164,7 +164,7 @@ pub const Header = struct {
164164 const ltrimmed = std.mem.trimLeft(u8, raw, "0 ");
165165 const rtrimmed = std.mem.trimRight(u8, ltrimmed, " \x00");
166166 if (rtrimmed.len == 0) return 0;
167 return std.fmt.parseInt(u64, rtrimmed, 8);
167 return std.fmt.parseInt(u64, rtrimmed, 8) catch return error.TarHeader;
168168 }
169169
170170 // Sum of all bytes in the header block. The chksum field is treated as if
......@@ -289,6 +289,10 @@ fn BufferedReader(comptime ReaderType: type) type {
289289 try self.reader.skip(len);
290290 }
291291
292 fn byte(self: *@This()) u8 {
293 return self.reader.buffer[self.reader.start];
294 }
295
292296 fn copy(self: *@This(), dst: []u8) ![]const u8 {
293297 _ = try self.reader.copy(dst);
294298 self.offset += dst.len;
......@@ -416,21 +420,25 @@ fn Iterator(comptime ReaderType: type) type {
416420 },
417421 .extended_header => {
418422 if (file_size == 0) continue;
423 // TODO: ovo resetiranje je nezgodno
424 self.attrs.free();
425 file = File{ .reader = &self.reader };
419426
420427 var rdr = self.reader.sliceReader(file_size, false);
421428 while (try rdr.next()) |slice| {
422429 const attr = try parsePaxAttribute(slice, rdr.remainingSize());
423430 try rdr.advance(attr.value_off);
424431 if (attr.is("path")) {
425 file.name = try rdr.copy(try self.attrs.alloc(attr.value_len));
432 file.name = try noNull(try rdr.copy(try self.attrs.alloc(attr.value_len)));
426433 } else if (attr.is("linkpath")) {
427 file.link_name = try rdr.copy(try self.attrs.alloc(attr.value_len));
434 file.link_name = try noNull(try rdr.copy(try self.attrs.alloc(attr.value_len)));
428435 } else if (attr.is("size")) {
429436 var buf = [_]u8{'0'} ** 32;
430437 file.size = try std.fmt.parseInt(usize, try rdr.copy(buf[0..attr.value_len]), 10);
431438 } else {
432439 try rdr.advance(attr.value_len);
433440 }
441 if (rdr.byte() != '\n') return error.InvalidPaxAttribute;
434442 try rdr.advance(1);
435443 }
436444 try self.reader.skipPadding(file_size);
......@@ -582,15 +590,21 @@ fn parsePaxAttribute(data: []const u8, max_size: usize) !PaxAttributeInfo {
582590 if (kv_size > max_size) {
583591 return error.InvalidPaxAttribute;
584592 }
593 const key = data[pos_space + 1 .. pos_equals];
585594 return .{
586595 .size = kv_size,
587 .key = data[pos_space + 1 .. pos_equals],
596 .key = try noNull(key),
588597 .value_off = pos_equals + 1,
589598 .value_len = kv_size - pos_equals - 2,
590599 };
591600}
592601
593test parsePaxAttribute {
602fn noNull(str: []const u8) ![]const u8 {
603 if (std.mem.indexOfScalar(u8, str, 0)) |_| return error.InvalidPaxAttribute;
604 return str;
605}
606
607test "parsePaxAttribute" {
594608 const expectEqual = std.testing.expectEqual;
595609 const expectEqualStrings = std.testing.expectEqualStrings;
596610 const expectError = std.testing.expectError;
......@@ -605,6 +619,7 @@ test parsePaxAttribute {
605619 try expectEqual(attr_info, try parsePaxAttribute(header, 1012));
606620 try expectError(error.InvalidPaxAttribute, parsePaxAttribute(header, 1010));
607621 try expectError(error.InvalidPaxAttribute, parsePaxAttribute("", 0));
622 try expectError(error.InvalidPaxAttribute, parsePaxAttribute("13 pa\x00th=abc\n", 1024)); // null in key
608623}
609624
610625const TestCase = struct {
......@@ -633,12 +648,10 @@ test "Go test cases" {
633648 .{
634649 .name = "small.txt",
635650 .size = 5,
636 .file_type = .normal,
637651 },
638652 .{
639653 .name = "small2.txt",
640654 .size = 11,
641 .file_type = .normal,
642655 },
643656 },
644657 .chksums = &[_][]const u8{
......@@ -656,12 +669,10 @@ test "Go test cases" {
656669 .{
657670 .name = "small.txt",
658671 .size = 5,
659 .file_type = .normal,
660672 },
661673 .{
662674 .name = "small2.txt",
663675 .size = 11,
664 .file_type = .normal,
665676 },
666677 },
667678 .chksums = &[_][]const u8{
......@@ -675,12 +686,10 @@ test "Go test cases" {
675686 .{
676687 .name = "small.txt",
677688 .size = 5,
678 .file_type = .normal,
679689 },
680690 .{
681691 .name = "small2.txt",
682692 .size = 11,
683 .file_type = .normal,
684693 },
685694 },
686695 .chksums = &[_][]const u8{
......@@ -694,7 +703,6 @@ test "Go test cases" {
694703 .{
695704 .name = "a/123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100",
696705 .size = 7,
697 .file_type = .normal,
698706 },
699707 .{
700708 .name = "a/b",
......@@ -707,18 +715,25 @@ test "Go test cases" {
707715 "3c382e8f5b6631aa2db52643912ffd4a",
708716 },
709717 },
710 // TODO: this should fail
711 // .{
712 // .path = "pax-bad-hdr-file.tar",
713 // .err = error.TarBadHeader,
714 // },
718 .{
719 // pax attribute don't end with \n
720 .path = "pax-bad-hdr-file.tar",
721 // .files = &[_]TestCase.File{
722 // .{
723 // .name = "PAX1/PAX1/long-path-name",
724 // .size = 684,
725 // },
726 // },
727 .err = error.InvalidPaxAttribute,
728 },
729 //
715730 // .{
716731 // .path = "pax-bad-mtime-file.tar",
717732 // .err = error.TarBadHeader,
718733 // },
719734 //
720 // TODO: giving wrong result because we are not reading pax size header
721735 .{
736 // size is in pax attribute
722737 .path = "pax-pos-size-file.tar",
723738 .files = &[_]TestCase.File{
724739 .{
......@@ -799,9 +814,17 @@ test "Go test cases" {
799814 .path = "gnu-incremental.tar",
800815 .err = error.TarUnsupportedFileType,
801816 },
802 // .{
803 // .path = "pax-multi-hdrs.tar",
804 // },
817 .{
818 // should use values only from last pax header
819 .path = "pax-multi-hdrs.tar",
820 .files = &[_]TestCase.File{
821 .{
822 .name = "bar",
823 .link_name = "PAX4/PAX4/long-linkpath-name",
824 .file_type = .symbolic_link,
825 },
826 },
827 },
805828 // .{
806829 // .path = "gnu-long-nul.tar",
807830 // .files = &[_]TestCase.File{
......@@ -827,8 +850,20 @@ test "Go test cases" {
827850 },
828851 },
829852 },
853 .{
854 .path = "neg-size.tar",
855 .err = error.TarHeader,
856 },
857 .{
858 .path = "pax-nul-path.tar",
859 .err = error.InvalidPaxAttribute,
860 },
861 .{
862 .path = "pax-nul-xattrs.tar",
863 .err = error.InvalidPaxAttribute,
864 },
830865 // TODO some files with errors:
831 // pax-nul-xattrs.tar, pax-nul-path.tar, neg-size.tar, issue10968.tar, issue11169.tar, issue12435.tar
866 // issue10968.tar, issue11169.tar, issue12435.tar
832867 .{
833868 .path = "trailing-slash.tar",
834869 .files = &[_]TestCase.File{