Get time stamp from cmsg can be NULL?

Issue

I looking at this code
https://elixir.bootlin.com/linux/v4.6.7/source/Documentation/networking/timestamping/timestamping.c#L181

This code try to print the timestamp of packet using struct timespec *stamp = (struct timespec *)CMSG_DATA(cmsg);

If this is UDP packet.
Is there any way that CMSG_DATA(cmsg) will return NULL?

Solution

If you look at the definition of CMSG_DATA you see

#define CMSG_DATA(cmsg) ((void *)((char *)(cmsg) + CMSG_ALIGN(sizeof(struct cmsghdr))))

This adds an offset to cmsg.
Unless you manage to feed a cmsg into that macro that evaluates to NULL after adding that offset, the result can never be come NULL.

Answered By – Gerhardh

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published