Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rationale for system calls that allow request of size_t but result of only ssize_t?

Tags:

c

linux

unix

Consider:

ssize_t write(int fd, const void *buf, size_t count);

The result has to be signed to account for -1 on error, etc., and is thus ssize_t. But why then allow for the request to be an unsigned amount (twice as large) when the result of asking for more than ssize_t is undefined?

Is there a significant optimization in the kernel by virtue of not checking for signedness of the count parameter? Or something else?

like image 613
ValenceElectron Avatar asked May 02 '11 16:05

ValenceElectron


1 Answers

According to the documentation for ssize_t write(int fildes, const void *buf, size_t nbyte)

If the value of nbyte is greater than {SSIZE_MAX}, the result is implementation-defined.

So each particular implementation may handle this situation differently. I would not be surprised if some implementations simply set EFBIG.

As for the rationale, perhaps size_t is simply the best type to represent the size of the buffer, semantically? It states that this argument is a nonnegative size, and nothing else.

like image 161
Cubbi Avatar answered Sep 29 '22 06:09

Cubbi