clinuxudptimespec

Get time stamp from cmsg can be NULL?


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.