mnl_nlmsg_fprintf_payload() prints the message in 4-byte words while i < nlmsg_len. mnl_nlmsg_ok() does not require nlmsg_len to be a multiple of 4, so for an unaligned length the last word reads up to 3 bytes past the end of the message, and past the buffer when it ends there. Only print complete words in the loop, then print the 1 to 3 bytes left over on a last row labelled "trailing bytes", so that the dump shows them: | 42 42 42 | | trailing bytes | Assisted-by: LLM Signed-off-by: Florian Stosse --- Notes: v2: print the 1 to 3 bytes left over on a last row labelled "trailing bytes" instead of skipping them, as suggested. Aligned messages print exactly as before. src/nlmsg.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/nlmsg.c b/src/nlmsg.c index f8eed79..0db0fa7 100644 --- a/src/nlmsg.c +++ b/src/nlmsg.c @@ -302,7 +302,7 @@ static void mnl_nlmsg_fprintf_payload(FILE *fd, const struct nlmsghdr *nlh, if (fdnum != -1) colorize = isatty(fdnum); - for (i=sizeof(struct nlmsghdr); inlmsg_len; i+=4) { + for (i=sizeof(struct nlmsghdr); i + 4 <= nlh->nlmsg_len; i+=4) { char *b = (char *) nlh; struct nlattr *attr = (struct nlattr *) (b+i); @@ -346,6 +346,20 @@ static void mnl_nlmsg_fprintf_payload(FILE *fd, const struct nlmsghdr *nlh, isprint(b[i+3]) ? b[i+3] : ' '); } } + /* nlmsg_len is not a multiple of 4: print the bytes left over. */ + if (i < nlh->nlmsg_len) { + const char *b = (const char *) nlh; + unsigned int j; + + fprintf(fd, "|"); + for (j = i; j < i + 4; j++) { + if (j < nlh->nlmsg_len) + fprintf(fd, " %.2x", 0xff & b[j]); + else + fprintf(fd, " "); + } + fprintf(fd, " |\t| trailing bytes |\n"); + } fprintf(fd, "----------------\t------------------\n"); } -- 2.56.0.windows.4