From f7a76f7e9618516fc054f18e3e8011fa06356ae9 Mon Sep 17 00:00:00 2001 From: Joris Vink Date: Fri, 22 Apr 2022 17:01:06 +0200 Subject: [PATCH] Improvements for kore.recvmsg() in Python. The cmsghdr contains a length (cmsg_len) which indicates the length of the data in combination with the cmsghdr length itself. Remove the length of the cmsghdr before passing it back up to callers so they don't need to bother with it. This also fixes a mistake where we ended up copying extra data from the ancdata buffer that was unintended. --- src/python.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/python.c b/src/python.c index a36a166..9280963 100644 --- a/src/python.c +++ b/src/python.c @@ -898,6 +898,7 @@ static PyObject * python_cmsg_to_list(struct msghdr *msg) { struct cmsghdr *c; + size_t len; Py_ssize_t idx; PyObject *list, *tuple; @@ -907,8 +908,10 @@ python_cmsg_to_list(struct msghdr *msg) idx = 0; for (c = CMSG_FIRSTHDR(msg); c != NULL; c = CMSG_NXTHDR(msg, c)) { - tuple = Py_BuildValue("(Iiiy#)", c->cmsg_len, - c->cmsg_level, c->cmsg_type, CMSG_DATA(c), c->cmsg_len); + len = c->cmsg_len - sizeof(*c); + + tuple = Py_BuildValue("(Iiiy#)", len, + c->cmsg_level, c->cmsg_type, CMSG_DATA(c), len); if (tuple == NULL) { Py_DECREF(list);