diff --git a/dulwich/_objects.c b/dulwich/_objects.c
index c16e5cc..941fcc1 100644
--- a/dulwich/_objects.c
+++ b/dulwich/_objects.c
@@ -71,7 +71,9 @@ static PyObject *py_parse_tree(PyObject *self, PyObject *args)
 
 		text++;
 
-		namelen = strnlen(text, len - (text - start));
+		size_t maxlen = len - (text - start);
+		const char *last = memchr(text, '\0', maxlen);
+		namelen = last ? (size_t) (last - text) : maxlen;
 
 		name = PyString_FromStringAndSize(text, namelen);
 		if (name == NULL) {
