Rev 2603: Detect truncated indices. in http://people.ubuntu.com/~robertc/baz2.0/repository

Robert Collins robertc at robertcollins.net
Thu Jul 12 16:51:22 BST 2007


At http://people.ubuntu.com/~robertc/baz2.0/repository

------------------------------------------------------------
revno: 2603
revision-id: robertc at robertcollins.net-20070712155119-zmcbukds65jw3tou
parent: robertc at robertcollins.net-20070712154729-ln0fpixap5lkcnsj
committer: Robert Collins <robertc at robertcollins.net>
branch nick: repository
timestamp: Fri 2007-07-13 01:51:19 +1000
message:
  Detect truncated indices.
modified:
  bzrlib/errors.py               errors.py-20050309040759-20512168c4e14fbd
  bzrlib/index.py                index.py-20070712131115-lolkarso50vjr64s-1
  bzrlib/tests/test_errors.py    test_errors.py-20060210110251-41aba2deddf936a8
  bzrlib/tests/test_index.py     test_index.py-20070712131115-lolkarso50vjr64s-2
=== modified file 'bzrlib/errors.py'
--- a/bzrlib/errors.py	2007-07-12 15:47:29 +0000
+++ b/bzrlib/errors.py	2007-07-12 15:51:19 +0000
@@ -353,6 +353,15 @@
         self._type = _type
 
 
+class BadIndexData(BzrError):
+
+    _fmt = "Error in data for index %(value)s."
+
+    def __init__(self, value):
+        BzrError.__init__(self)
+        self.value = value
+
+
 class BadIndexOptions(BzrError):
 
     _fmt = "Could not parse options for index %(value)s."

=== modified file 'bzrlib/index.py'
--- a/bzrlib/index.py	2007-07-12 15:47:29 +0000
+++ b/bzrlib/index.py	2007-07-12 15:51:19 +0000
@@ -100,3 +100,10 @@
             node_ref_lists = int(options_line[len(_OPTION_NODE_REFS):-1])
         except ValueError:
             raise errors.BadIndexOptions(self)
+        line_count = 0
+        for line in stream.readlines():
+            # validate the line
+            line_count += 1
+        if line_count < 1:
+            # there must be one line - the empty trailer line.
+            raise errors.BadIndexData(self)

=== modified file 'bzrlib/tests/test_errors.py'
--- a/bzrlib/tests/test_errors.py	2007-07-12 15:47:29 +0000
+++ b/bzrlib/tests/test_errors.py	2007-07-12 15:51:19 +0000
@@ -188,6 +188,11 @@
         self.assertEqual("foo is not an index of type bar.",
             str(error))
 
+    def test_bad_index_data(self):
+        error = errors.BadIndexData("foo")
+        self.assertEqual("Error in data for index foo.",
+            str(error))
+
     def test_bad_index_options(self):
         error = errors.BadIndexOptions("foo")
         self.assertEqual("Could not parse options for index foo.",

=== modified file 'bzrlib/tests/test_index.py'
--- a/bzrlib/tests/test_index.py	2007-07-12 15:47:29 +0000
+++ b/bzrlib/tests/test_index.py	2007-07-12 15:51:19 +0000
@@ -83,6 +83,14 @@
         trans.put_bytes('index', new_content)
         self.assertRaises(errors.BadIndexOptions, index.validate)
 
+    def test_validate_missing_end_line(self):
+        index = self.make_index(2)
+        trans = self.get_transport()
+        content = trans.get_bytes('index')
+        # truncate the last byte
+        trans.put_bytes('index', content[:-1])
+        self.assertRaises(errors.BadIndexData, index.validate)
+
     def test_validate_empty(self):
         index = self.make_index()
         index.validate()




More information about the bazaar-commits mailing list