Search code examples
cposixstandards

Is the iov_len field of an iovec well-defined?


According to Pointer arithmetic for void pointer in C, arithmetic with void pointers is not allowed. Yet, the iovec structure looks like so:

struct iovec {
    void *iov_base;
    size_t iov_len;
};

This is defined in the POSIX standard, and the iov_len field is described as the somewhat vague "The size of the memory pointed to by iov_base". Since iovec is just a data structure, I wondered if the functions that accept it as input parameters define how they use the iov_len field, but both readv and writev desribe it, again vaguely, as "the length of an area of memory."

For some functions, the meaning of the iov_len field does seem to be explicitly defined. For example, the recvmsg specification says the "iov_len field gives [the storage area's] size in bytes". Additionally, recv, which accepts a void pointer and length, properly defines the length parameter as the length of the buffer in bytes.

My question: Is the meaning of the iov_len field well-defined in these cases where it's defined as the length of a buffer?


Solution

  • I believe you're just overthinking here. "Size" here definitely means "size in bytes", and in the cases you mention I don't think there's any meaningful difference between the words "size" and "length". There's no subtlety that you're missing.

    It might be a tiny bit sloppy that they didn't say "in bytes" as they do in many other places, and you could file a defect report if you really want, but I don't see much room for confusion as there's really no other sensible interpretation.

    Nobody is claiming that void has a size, but it's very common to use void * as a pointer to a generic buffer whose size is denominated in bytes; using void * instead of unsigned char * makes it convenient for you to pass a pointer to some other type without casting.