From e76411f98371a7d7956039c1e00cf7652a7ce6a2 Mon Sep 17 00:00:00 2001 From: Jan Mazur Date: Fri, 3 Apr 2020 16:27:54 +0200 Subject: [PATCH 1/6] Truncating file if opened in append mode. --- Lib/test/test_zipfile.py | 12 ++++++++---- Lib/zipfile.py | 2 ++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py index 643c5b477bab32..454c7a1ebb03fe 100644 --- a/Lib/test/test_zipfile.py +++ b/Lib/test/test_zipfile.py @@ -1808,7 +1808,6 @@ def test_struct_sizes(self): def test_comments(self): """Check that comments on the archive are handled properly.""" - # check default comment is empty with zipfile.ZipFile(TESTFN, mode="w") as zipf: self.assertEqual(zipf.comment, b'') @@ -1853,13 +1852,18 @@ def test_comments(self): self.assertEqual(zipf.comment, b"an updated comment") # check that comments are correctly shortened in append mode + # and the file is indeed truncated + original_comment = b"original comment that's longer" + one_byte_shorter_comment = original_comment[:-1] with zipfile.ZipFile(TESTFN,mode="w") as zipf: - zipf.comment = b"original comment that's longer" + zipf.comment = original_comment zipf.writestr("foo.txt", "O, for a Muse of Fire!") + original_zip_size = os.path.getsize(TESTFN) with zipfile.ZipFile(TESTFN,mode="a") as zipf: - zipf.comment = b"shorter comment" + zipf.comment = one_byte_shorter_comment + self.assertEqual(original_zip_size, os.path.getsize(TESTFN) + 1) with zipfile.ZipFile(TESTFN,mode="r") as zipf: - self.assertEqual(zipf.comment, b"shorter comment") + self.assertEqual(zipf.comment, one_byte_shorter_comment) def test_unicode_comment(self): with zipfile.ZipFile(TESTFN, "w", zipfile.ZIP_STORED) as zipf: diff --git a/Lib/zipfile.py b/Lib/zipfile.py index c3f814cc747e06..a6fff0a48093b2 100644 --- a/Lib/zipfile.py +++ b/Lib/zipfile.py @@ -1919,6 +1919,8 @@ def _write_end_record(self): centDirSize, centDirOffset, len(self._comment)) self.fp.write(endrec) self.fp.write(self._comment) + if self.mode == "a": + self.fp.truncate() self.fp.flush() def _fpclose(self, fp): From c31136241937ba87bfba95dfd010c922b06b442f Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Fri, 3 Apr 2020 16:14:00 +0000 Subject: [PATCH 2/6] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NEWS.d/next/Library/2020-04-03-16-13-59.bpo-40105.hfM2c0.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2020-04-03-16-13-59.bpo-40105.hfM2c0.rst diff --git a/Misc/NEWS.d/next/Library/2020-04-03-16-13-59.bpo-40105.hfM2c0.rst b/Misc/NEWS.d/next/Library/2020-04-03-16-13-59.bpo-40105.hfM2c0.rst new file mode 100644 index 00000000000000..6753bacb27395d --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-04-03-16-13-59.bpo-40105.hfM2c0.rst @@ -0,0 +1 @@ +`ZipFile`s are truncated when shorter commend is provider in `'a'` mode. \ No newline at end of file From 4f8d7cb90f9c664bf5cc12b79bc20b3072cee663 Mon Sep 17 00:00:00 2001 From: Jan Mazur Date: Fri, 3 Apr 2020 18:47:23 +0200 Subject: [PATCH 3/6] fix news dos --- .../next/Library/2020-04-03-16-13-59.bpo-40105.hfM2c0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2020-04-03-16-13-59.bpo-40105.hfM2c0.rst b/Misc/NEWS.d/next/Library/2020-04-03-16-13-59.bpo-40105.hfM2c0.rst index 6753bacb27395d..fc0603c590ca33 100644 --- a/Misc/NEWS.d/next/Library/2020-04-03-16-13-59.bpo-40105.hfM2c0.rst +++ b/Misc/NEWS.d/next/Library/2020-04-03-16-13-59.bpo-40105.hfM2c0.rst @@ -1 +1 @@ -`ZipFile`s are truncated when shorter commend is provider in `'a'` mode. \ No newline at end of file +ZipFiles are truncated when shorter comment is provided in append ("a") mode. From a93382e8d62e9b3ac1a17f2e3723762e42c5c7ab Mon Sep 17 00:00:00 2001 From: Jan Mazur Date: Sat, 26 Sep 2020 15:59:41 +0200 Subject: [PATCH 4/6] check whether file whether zipfile was truncated when appending shorter comment --- Lib/test/test_zipfile.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py index 454c7a1ebb03fe..028ff63b050254 100644 --- a/Lib/test/test_zipfile.py +++ b/Lib/test/test_zipfile.py @@ -1853,17 +1853,15 @@ def test_comments(self): # check that comments are correctly shortened in append mode # and the file is indeed truncated - original_comment = b"original comment that's longer" - one_byte_shorter_comment = original_comment[:-1] with zipfile.ZipFile(TESTFN,mode="w") as zipf: - zipf.comment = original_comment + zipf.comment = b"original comment that's longer" zipf.writestr("foo.txt", "O, for a Muse of Fire!") original_zip_size = os.path.getsize(TESTFN) with zipfile.ZipFile(TESTFN,mode="a") as zipf: - zipf.comment = one_byte_shorter_comment - self.assertEqual(original_zip_size, os.path.getsize(TESTFN) + 1) + zipf.comment = b"shorter comment" + self.assertTrue(original_zip_size > os.path.getsize(TESTFN)) with zipfile.ZipFile(TESTFN,mode="r") as zipf: - self.assertEqual(zipf.comment, one_byte_shorter_comment) + self.assertEqual(zipf.comment, b"shorter comment") def test_unicode_comment(self): with zipfile.ZipFile(TESTFN, "w", zipfile.ZIP_STORED) as zipf: From 55d3b4625030ca4d39f3317f4fe77139ae5c96ab Mon Sep 17 00:00:00 2001 From: Jan Mazur Date: Mon, 28 Sep 2020 18:20:01 +0200 Subject: [PATCH 5/6] delete unnecessary whitespace change --- Lib/test/test_zipfile.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py index 6bbf4c39bfbfca..687e43df780d65 100644 --- a/Lib/test/test_zipfile.py +++ b/Lib/test/test_zipfile.py @@ -1811,6 +1811,7 @@ def test_struct_sizes(self): def test_comments(self): """Check that comments on the archive are handled properly.""" + # check default comment is empty with zipfile.ZipFile(TESTFN, mode="w") as zipf: self.assertEqual(zipf.comment, b'') From 198b93bb45204fd32626784797c335ead3124461 Mon Sep 17 00:00:00 2001 From: Tal Einat Date: Mon, 28 Sep 2020 21:24:43 +0300 Subject: [PATCH 6/6] slightly reword NEWS entry --- .../next/Library/2020-04-03-16-13-59.bpo-40105.hfM2c0.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2020-04-03-16-13-59.bpo-40105.hfM2c0.rst b/Misc/NEWS.d/next/Library/2020-04-03-16-13-59.bpo-40105.hfM2c0.rst index fc0603c590ca33..f71a7a1e697a48 100644 --- a/Misc/NEWS.d/next/Library/2020-04-03-16-13-59.bpo-40105.hfM2c0.rst +++ b/Misc/NEWS.d/next/Library/2020-04-03-16-13-59.bpo-40105.hfM2c0.rst @@ -1 +1,2 @@ -ZipFiles are truncated when shorter comment is provided in append ("a") mode. +ZipFile truncates files to avoid corruption when a shorter comment is provided +in append ("a") mode. Patch by Jan Mazur.