We use strnlen so we can find namelen even if the buffer is truncated
in the name. This is not necessary for Python string objects, which are
guaranteed to be null-terminated, but some buffer objects (e.g. mmap)
may not be.
static PyObject *py_parse_tree(PyObject *self, PyObject *args)
{
- char *text, *end;
+ char *text, *start, *end;
int len, namelen;
PyObject *ret, *item, *name;
return NULL;
}
+ start = text;
end = text + len;
while (text < end) {
text++;
- namelen = strlen(text);
+ namelen = strnlen(text, len - (text - start));
name = PyString_FromStringAndSize(text, namelen);
if (name == NULL) {