Procházet zdrojové kódy

Add extra check for bytes to read in compute_file_sha.

Jelmer Vernooij před 10 roky
rodič
revize
3e8246aeaa
2 změnil soubory, kde provedl 14 přidání a 1 odebrání
  1. 7 1
      dulwich/pack.py
  2. 7 0
      dulwich/tests/test_pack.py

+ 7 - 1
dulwich/pack.py

@@ -916,7 +916,13 @@ def compute_file_sha(f, start_ofs=0, end_ofs=0, buffer_size=1<<16):
     """
     sha = sha1()
     f.seek(0, SEEK_END)
-    todo = f.tell() + end_ofs - start_ofs
+    length = f.tell()
+    if (end_ofs < 0 and length + end_ofs < start_ofs) or end_ofs > length:
+        raise AssertionError(
+            "Attempt to read beyond file length. "
+            "start_ofs: %d, end_ofs: %d, file length: %d" % (
+                start_ofs, end_ofs, length))
+    todo = length + end_ofs - start_ofs
     f.seek(start_ofs)
     while todo:
         data = f.read(min(todo, buffer_size))

+ 7 - 0
dulwich/tests/test_pack.py

@@ -259,6 +259,13 @@ class TestPackData(PackTests):
           sha1('1234').hexdigest(),
           compute_file_sha(f, start_ofs=4, end_ofs=-4).hexdigest())
 
+    def test_compute_file_sha_short_file(self):
+        f = BytesIO('abcd1234wxyz')
+        self.assertRaises(AssertionError, compute_file_sha, f, end_ofs=-20)
+        self.assertRaises(AssertionError, compute_file_sha, f, end_ofs=20)
+        self.assertRaises(AssertionError, compute_file_sha, f, start_ofs=10,
+            end_ofs=-12)
+
 
 class TestPack(PackTests):