Correctly avoid parsing ShaFiles with fixed SHAs when calling sha().
[jelmer/dulwich-libgit2.git] / dulwich / _objects.c
index 7ef7ad4c4aa2553aed094fcbc1b607da8dac71ee..d9699ec6cb4a72e52873929f679048e32d176819 100644 (file)
@@ -37,7 +37,7 @@ static PyObject *sha_to_pyhex(const unsigned char *sha)
 
 static PyObject *py_parse_tree(PyObject *self, PyObject *args)
 {
-       char *text, *end;
+       char *text, *start, *end;
        int len, namelen;
        PyObject *ret, *item, *name;
 
@@ -52,6 +52,7 @@ static PyObject *py_parse_tree(PyObject *self, PyObject *args)
                return NULL;
        }
 
+       start = text;
        end = text + len;
 
        while (text < end) {
@@ -59,14 +60,14 @@ static PyObject *py_parse_tree(PyObject *self, PyObject *args)
                mode = strtol(text, &text, 8);
 
                if (*text != ' ') {
-                       PyErr_SetString(PyExc_RuntimeError, "Expected space");
+                       PyErr_SetString(PyExc_ValueError, "Expected space");
                        Py_DECREF(ret);
                        return NULL;
                }
 
                text++;
 
-               namelen = strlen(text);
+               namelen = strnlen(text, len - (text - start));
 
                name = PyString_FromStringAndSize(text, namelen);
                if (name == NULL) {
@@ -75,7 +76,7 @@ static PyObject *py_parse_tree(PyObject *self, PyObject *args)
                }
 
                if (text + namelen + 20 >= end) {
-                       PyErr_SetString(PyExc_RuntimeError, "SHA truncated");
+                       PyErr_SetString(PyExc_ValueError, "SHA truncated");
                        Py_DECREF(ret);
                        Py_DECREF(name);
                        return NULL;