parse_tree: Fix tests for pure-python implementation.
[jelmer/dulwich-libgit2.git] / dulwich / objects.py
index e1854eb2a998b27c0f7f940b6168358d9a7a8841..fab297d6825eae7e5f6392298bd5d99e9dafe535 100644 (file)
@@ -683,12 +683,20 @@ def parse_tree(text):
     l = len(text)
     while count < l:
         mode_end = text.index(' ', count)
-        mode = int(text[count:mode_end], 8)
+        mode_text = text[count:mode_end]
+        assert mode_text[0] != '0'
+        try:
+            mode = int(mode_text, 8)
+        except ValueError:
+            raise ObjectFormatException("Invalid mode '%s'" % mode_text)
         name_end = text.index('\0', mode_end)
         name = text[mode_end+1:name_end]
         count = name_end+21
         sha = text[name_end+1:count]
-        yield (name, mode, sha_to_hex(sha))
+        if len(sha) != 20:
+            raise ObjectFormatException("Sha has invalid length")
+        hexsha = sha_to_hex(sha)
+        yield (name, mode, hexsha)
 
 
 def serialize_tree(items):