Skip to content

Commit

Permalink
Merge pull request #1611 from ila-embsys/fix/ar_hdr_attr_access
Browse files Browse the repository at this point in the history
Fix `dataoff` attribute access on `ext_fnhdr`
  • Loading branch information
dmach authored Aug 27, 2024
2 parents 735bab7 + 2920529 commit 2afae5d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
10 changes: 8 additions & 2 deletions osc/util/ar.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,10 @@ def _fixupFilenames(self):
"""

# read extended header with long file names and then only seek into the right offsets
self.__file.seek(self.ext_fnhdr.dataoff, os.SEEK_SET)
ext_fnhdr_data = self.__file.read(self.ext_fnhdr.size)
ext_fnhdr_data = None
if self.ext_fnhdr:
self.__file.seek(self.ext_fnhdr.dataoff, os.SEEK_SET)
ext_fnhdr_data = self.__file.read(self.ext_fnhdr.size)

for h in self.hdrs:
if h.file == b'/':
Expand All @@ -159,8 +161,12 @@ def _fixupFilenames(self):
h.file = h.file[:-1]
continue

if not h.file.startswith(b'/'):
continue

# long file name
assert h.file[0:1] == b"/"
assert ext_fnhdr_data is not None
start = int(h.file[1:])
end = ext_fnhdr_data.find(b'/', start)

Expand Down
6 changes: 6 additions & 0 deletions tests/fixtures/README
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,9 @@ Create archive.cpio

printf "/tmp/foo\0/123\0very-long-long-long-long-name\0very-long-long-long-long-name2\0very-long-name
-with-newline\0a\nb\0dir/file\0" | cpio -ocv0 --owner=root:root > archive.cpio


Create archive-no-ext_fnhdr.ar
------------------------------

ar qP archive-no-ext_fnhdr.ar dir/file
3 changes: 3 additions & 0 deletions tests/fixtures/archive-no-ext_fnhdr.ar
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
!<arch>
dir/file/ 1724142481 1000 1000 100644 14 `
file-in-a-dir
6 changes: 6 additions & 0 deletions tests/test_util_ar.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ def test_saveTo_abspath(self):
# this is supposed to throw an error, extracting files with absolute paths might overwrite system files
self.assertRaises(ArError, f.saveTo, self.tmpdir)

def test_no_exthdr(self):
self.archive = os.path.join(FIXTURES_DIR, "archive-no-ext_fnhdr.ar")
self.ar = Ar(self.archive)
self.ar.read()
self.test_saveTo_subdir()


if __name__ == "__main__":
unittest.main()

0 comments on commit 2afae5d

Please sign in to comment.